【问题标题】:Symfony 2 Primary key is one to one relation with parent tableSymfony 2主键与父表是一对一的关系
【发布时间】:2014-01-13 04:47:20
【问题描述】:

我有两个实体:用户和用户配置文件。在用户实体主键是自动增量并命名为 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' => 'Сохранить'));
}

【问题讨论】:

  • 如果没有您的表单,很难确切知道这里发生了什么,但您似乎没有在 UserProfile 上设置 intuserid 字段。如果不清楚,可以张贴您的表格,以便我们为您提供更多帮助。

标签: php symfony orm doctrine-orm schema.yml


【解决方案1】:

您已在 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();

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

【讨论】:

  • 当我删除 intuserid 声明时 - 教义抛出错误:MappingException:没有为实体“Project\UsersBundle\Entity\UserProfile”指定标识符/主键。每个实体都必须有一个标识符/主键。
  • 更新了我的答案以设置 UserProfile intuserid 字段
  • 感谢您的解决方案。从 oneToOne 部分删除用户后,它可以工作。我编辑了你的答案
猜你喜欢
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-14
  • 2020-01-08
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
相关资源
最近更新 更多