Symfony2 - 类型为...... \ Entity \ Users的实体缺少字段'userId'的已分配ID

时间:2013-12-31 15:56:28

标签: php entity-framework symfony orm doctrine

我总是收到这条消息:

  

Reuzze \ ReuzzeBundle \ Entity \ Users类型的实体缺少已分配的实体   字段'userId'的ID。这个标识符生成策略   实体要求之前填充ID字段   调用EntityManager#persist()。如果要自动生成   您需要调整元数据映射   相应

我试图删除:

@ORM\GeneratedValue(strategy="AUTO")

或将其更改为:

@ORM\GeneratedValue(strategy="IDENTITY")
@ORM\GeneratedValue(strategy="NONE")

我也粘贴在:

id:
        userId:
            primary: true
            autoincrement: true

但没有任何作用!

我的UserController注册操作:

public function registerAction(Request $request)
    {
        if ($this->get('security.context')->isGranted('ROLE_USER'))
        {
            return $this->redirect($this->generateUrl('reuzze_reuzze_homepage'));
        }

        $user = new Users();

        $person = new Persons();

        $address = new Addresses();

        $region = new Regions();

        $role = new Roles();

        $address->setRegion($region);

        $user->setPerson($person);
        $user->setUserRating('1');
        $user->setRole($role);

        $person->setAddress($address);

        $address->setRegion($region);

        $role->setRoleName('Student');

        $form = $this->createForm(new RegisterType(), $user);

        if ($request->getMethod() == 'POST')
        {
            $form->bind($request);

            if($form->isValid())
            {
                $factory = $this->get('security.encoder_factory');
                $encoder = $factory->getEncoder($user);
                $password = $encoder->encodePassword($user->getuserPassword(), $user->getuserSalt());
                $user->setuserPassword($password);

                $user->setRole($role);

                $date = new \DateTime('NOW');
                //$user->setuserId('1');
                $user->setuserCreated($date);

                $entityManager = $this->getDoctrine()->getManager();

                $entityManager->persist($user);
                $entityManager->persist($person);
                $entityManager->persist($address);
                $entityManager->persist($region);
                $entityManager->persist($role);

                $entityManager->flush();

                return $this->redirect($this->generateUrl('reuzze_reuzze_homepage'));
            }
        }

        return $this->render('ReuzzeReuzzeBundle:User:register.html.twig', array(
            'form' => $form->createView()
        ));
    }

我的RegisterType:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('person'     , new PersonType())
            ->add('userUsername'   , 'text', array(
                'label' => 'Username',
                'attr' => array('placeholder' => 'Username')
            ))
            ->add('userEmail'       , 'email', array(
                'label' => 'E-mailadres',
                'attr' => array('placeholder' => 'E-mail address')
            ))
            ->add('userSalt' , 'repeated', array(
                'type' => 'password',
                'first_name' => 'password',
                'second_name' => 'confirm',
                'first_options' => array(
                    'attr' => array('class' => 'form-control', 'placeholder' => 'Password'),
                    'label' => 'Password',
                ),
                'second_options' => array(
                    'label' => 'Repeat Password',
                    'attr' => array('class' => 'form-control', 'placeholder' => 'Repeat Password'),
                ),
                'invalid_message' => 'The passwords are not identical!',
            )
        );
    }

用户模型(userId)

 /**
     * @var integer
     *
     * @ORM\Column(name="user_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $userId;

/**
     * Set userId
     *
     * @param integer $userId
     * @return Users
     */
    public function setUserId($userId)
    {
        $this->userId = $userId;

        return $this;
    }

    /**
     * Get userId
     *
     * @return integer 
     */
    public function getUserId()
    {
        return $this->userId;
    }

唯一有效的方法是在Usercontroller寄存器操作中添加 $ user-> setuserId('1'); 。但显然这不是我想要的,有人有解决方法吗?

如果我在我的UserController中评论 $ entityManager-> persist($ user); ,它会创建除User之外的所有其他对象,但在这种情况下我不会收到错误。

1 个答案:

答案 0 :(得分:2)

解决方案:

删除参考资料中的所有orm.yml文件> config>教义