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



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

用户实体的 YML 映射:

ProjectUsersBundleEntityUser:
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: ProjectUsersBundleEntityUserProfile
        cascade: ["all"]
        joinColumn:
            name: intuserid
            referencedColumnName: intUserID
lifecycleCallbacks: {  }

YML 表单用户配置文件:

ProjectUsersBundleEntityUserProfile:
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: ProjectUsersBundleEntityUser
        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。删除第一个声明:

# remove this part
oneToOne:
user:
    targetEntity: ProjectUsersBundleEntityUser
    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();

相关内容

  • 没有找到相关文章

最新更新