【问题标题】:Error while persisting data into database in symfony2在 symfony2 中将数据持久保存到数据库时出错
【发布时间】:2015-05-12 18:08:10
【问题描述】:

我创建了一个更新密码表单,每当我尝试提交数据时,我都会收到类似The class XXX was not found in the chain configured namespaces 的错误。我在这里做错了什么?当我尝试将数据保存在数据库中时发生错误。我已经尝试过implementing update password的答案 这是我的代码。 这是我的 ChangePasswordType.php

<?php

namespace RetailMapping\Bundle\UserBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ChangePasswordType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('oldPassword', 'password');
        $builder->add('newPassword', 'repeated', array(
            'type' => 'password',
            'invalid_message' => 'The password fields must match.',
            'required' => true,
            'first_options'  => array('label' => 'Password'),
            'second_options' => array('label' => 'Repeat Password'),
        ));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
                'data_class' => 'RetailMapping\Bundle\UserBundle\Form\Model\ChangePassword',
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'retailmapping_bundle_userbundle_password_chnage';
    }
}

这是我的 ChangePassword.php

<?php
namespace RetailMapping\Bundle\UserBundle\Form\Model;

use Symfony\Component\Security\Core\Validator\Constraints as SecurityAssert;
use Symfony\Component\Validator\Constraints as Assert;

class ChangePassword
{
     /**
      * @SecurityAssert\UserPassword(
      *     message = "Wrong value for your current password"
      * )
      */
     protected $oldPassword;

     /**
      * @Assert\Length(
      *     min = 6,
      *     minMessage = "Password should by at least 6 chars long"
      * )
      */
     protected $newPassword;

    public function setOldPassword($oldPassword)
    {
        $this->oldPassword = $oldPassword;
    }

    public function getOldPassword()
    {
        return $this->oldPassword;
    }

    public function setNewPassword($newPassword)
    {
        $this->newPassword = $newPassword;
    }

    public function getNewPassword()
    {
        return $this->newPassword;
    }
}

这是我的 UserController.php

<?php
namespace RetailMapping\Bundle\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use RetailMapping\Bundle\UserBundle\Form\ChangePasswordType;
use Symfony\Component\HttpFoundation\Request;
use RetailMapping\Bundle\UserBundle\Form\Model\ChangePassword;

class UserController extends Controller
{
public function editPasswordAction(Request $request)
    {
        $form = $this->createForm(new ChangePasswordType(), new ChangePassword());
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();
            $form->getData();
            $em = $this->getDoctrine()->getManager();
            $em->persist($data);
            $em->flush();
            dump($form->getData());
            die;
        }

        return $this->render('User/editPassword.html.twig', [
                'form' => $form->createView(),
        ]);
    }
}

【问题讨论】:

  • 问题解决了。我添加了这段代码。 if ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) { $data = $form-&gt;getData(); $user = $this-&gt;getUser(); $user-&gt;setPassword($data-&gt;getNewPassword()); $em = $this-&gt;getDoctrine()-&gt;getManager(); $em-&gt;persist($user); $em-&gt;flush(); }

标签: php forms symfony doctrine


【解决方案1】:

默认情况下,您的实体映射到 Entity 命名空间。

DoctrineExtension code on github

protected function getMappingObjectDefaultName()
{
    return 'Entity';
}

您需要将ChangePassword.php 放在Entity 文件夹中。

更新:

类似问题 - you can look at

【讨论】:

  • 将 changepassword.php 文件放在实体文件夹中,但出现 Class "RetailMapping\Bundle\UserBundle\Entity\ChangePassword" 之类的错误不是有效的实体或映射的超类。我要在我的代码中添加 getMappingObjectDefaultName() 函数吗??
  • @RajShakya 你在课堂上缺少/** * @ORM\Entity * @ORM\Table(name="YOUR_TABLE_NAME") */
  • 不,我已经检查了链接,这与我的问题无关
  • @RajShakya 看看这个答案 - stackoverflow.com/questions/7820597/…
  • @RajShakya 运行此命令php app/console doctrine:mapping:info 并显示结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-24
  • 1970-01-01
  • 2011-04-13
  • 2021-06-24
  • 2014-05-16
相关资源
最近更新 更多