1

我有两个实体:用户和用户配置文件。在用户实体主键是自动增量并命名为 intUserID。UserProfile 实体具有相同的主键(但不是自动增量)并与 User 实体关联。当我尝试将所有用户数据插入此实体时,学说会引发异常:Project\UsersBundle\Entity\UserProfile 类型的实体缺少为字段“intuserid”分配的 ID

UserEntity 的 YML 映射:

Project\UsersBundle\Entity\User:
type: entity
table: user
fields:
    intuserid:
        id: true
        type: integer
        unsigned: true
        nullable: false
        column: intUserID
        generator:
            strategy: IDENTITY
    varname:
        type: string
        length: 150
        fixed: false
        nullable: false
        column: varName
    varemail:
        type: string
        length: 150
        fixed: false
        nullable: false
        column: varEmail
    varpassword:
        type: string
        length: 40
        fixed: false
        nullable: false
        column: varPassword

oneToOne:
    profile:
        targetEntity: Project\UsersBundle\Entity\UserProfile
        cascade: ["all"]
        joinColumn:
            name: intuserid
            referencedColumnName: intUserID

lifecycleCallbacks: {  }

YML 表单用户配置文件:

Project\UsersBundle\Entity\UserProfile:
type: entity
table: user_profile
fields:
    intuserid:
        id: true
        type: integer
        unsigned: true
        nullable: false
        column: intUserID
    varsurname:
        type: string
        length: 50
        fixed: false
        nullable: true
        column: varSurname
    varpatronymic:
        type: string
        length: 50
        fixed: false
        nullable: true
        column: varPatronymic

oneToOne:
    user:
        targetEntity: Project\UsersBundle\Entity\User
        inversedBy: profile
        joinColumn:
            name: intuserid
            referencedColumnName: intUserID

lifecycleCallbacks: {  }

创建动作:

public function createAction(Request $request)
{
    $user = new User();

    $form = $this->createCreateForm($user);
    $form->handleRequest($request);

    if ($form->isValid())
    {
        $user->setVarSalt(md5(microtime().$user->getVarEmail()));

        if ($user->getVarPassword())
        {
            $encoder  = $this->container->get('security.encoder_factory')->getEncoder($user);
            $password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
            $user->setVarPassword($password);
        }

        $em = $this->getDoctrine()->getManager();
        $em->persist($user);
        $em->flush();

        $this->get('session')->getFlashBag()->add('success', 'Successfully saved');

        return $this->redirect($this->generateUrl('users_edit', array('id' => $user->getIntUserID())));
    }

    return $this->render('ProjectUsersBundle:Default:edit.html.twig', array(
        'entity' => $user,
        'form' => $form->createView(),
    ));
}

表单用户

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('varname', 'text', array('label' => 'Название', 'required' => true))
        ->add('varemail', 'text', array('label' => 'E-mail', 'required' => true))
        ->add('varpassword', 'password', array('label' => 'Пароль'))
        ->add('isactive', 'checkbox', array('label' => 'Активен'))
        ->add('ispublic', 'checkbox', array('label' => 'Публичный профиль'))
        ->add('profile', new UsersProfileType())
        ->add('save', 'submit', array('label' => 'Сохранить'));
}

表单用户资料

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('varsurname', 'text', array('label' => 'Фамилия'))
        ->add('varpatronymic', 'text', array('label' => 'Отчество'))
        ->add('save', 'submit', array('label' => 'Сохранить'));
}
4

1 回答 1

1

您已在实体中intuserid两次声明该字段。UserProfile删除第一个声明:

# remove this part
oneToOne:
user:
    targetEntity: Project\UsersBundle\Entity\User
    inversedBy: profile
    joinColumn:
        name: intuserid
        referencedColumnName: intUserID

您可能还需要删除UserProfilebefore persisting User

// set the UserProfile to null (assumes null default value to setUserProfile)
$profile = $user->getUserProfile();
$user->setUserProfile();
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
$profile->setIntuserid($user->getIntuserid());
$em->persist($profile);
$em->flush();

试一试,看看它是否能解除对您的阻止。圣诞节快乐!

于 2013-12-25T15:44:29.157 回答