Symfony 2主键与父表是一对一关系

bn31dyow  于 2022-12-27  发布在  其他
关注(0)|答案(1)|浏览(204)

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

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' => 'Сохранить'));
}
czq61nw1

czq61nw11#

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

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

您还可能需要在持久化User之前删除UserProfile

// 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();

相关问题