symfony 2原则关系ONETONE

symfony 2原则关系ONETONE,symfony,doctrine-orm,one-to-one,Symfony,Doctrine Orm,One To One,如何获取和设置实体,如我的示例中的一个关系 我有一个错误: Miejsce\ObiektyBundle\Entity\UsersInformation类型的实体缺少为字段“user\u ID”分配的ID。此实体的标识符生成策略要求在调用EntityManagerpersist之前填充ID字段。如果希望自动生成标识符,则需要相应地调整元数据映射 在php控制器中-我尝试以以下方式保存新项: $product = new Userstest(); $product->setUsername('

如何获取和设置实体,如我的示例中的一个关系

我有一个错误:

Miejsce\ObiektyBundle\Entity\UsersInformation类型的实体缺少为字段“user\u ID”分配的ID。此实体的标识符生成策略要求在调用EntityManagerpersist之前填充ID字段。如果希望自动生成标识符,则需要相应地调整元数据映射

在php控制器中-我尝试以以下方式保存新项:

$product = new Userstest();
$product->setUsername('aa')->setPassword('123456');
$product->setInformation((new UsersInformation())->setCompany('firma'));
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
当我以这种方式储蓄时

$code = 'test3';
    
    $product->setUsername($code)->setPassword('123456');
    $information = new UsersInformation();
    $information
        ->setEmail($code.'@a.pl')
        ->setUserId($product->getUserId())
    ;


    $product->setInformation($information);


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

 print_r($product);
而且

`* @ORM\GeneratedValue(strategy="AUTO") UsersInformation.for` `user_id`
拥有:

Miejsce\ObiektyBundle\Entity\Userstest Object
(
    [user_id:protected] => 9
    [information:protected] => Miejsce\ObiektyBundle\Entity\UsersInformation Object
        (
            [user_id:protected] => 5
            [user:protected] => 
            [email] => test3@a.pl
            [gender] => 
            [company] => 
        )

    [username:protected] => test3
    [password:protected] => 123456
)
它不起作用 但是当我删除*@ORM\GeneratedValuestrategy=AUTO时

我得到这个错误:

Miejsce\ObiektyBundle\Entity\USERS类型的实体信息为 缺少为字段“user\u ID”分配的ID。标识符生成 此实体的策略要求在 调用EntityManagerpersist。如果要自动生成 相反,您需要调整元数据映射 因此

实体:

<?php
namespace Miejsce\ObiektyBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
 
/**
 * @ORM\Entity
 * @ORM\Table(name="test_user")
 */
class Userstest
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 *
 */
protected $user_id;


/**
 * @ORM\OneToOne(targetEntity="UsersInformation", mappedBy="Users", cascade={"persist", "remove"})
 */
protected $information;


/**
 * @ORM\Column(type="string", length=255)
 */
protected $username;


/**
 * @ORM\Column(type="string", length=32)
 */
protected $password;
将@ORM\GeneratedValuestrategy=AUTO添加到UserInformation类中的$user_id中,或使用setUserId为实体设置显式id

说明:

错误告诉您,条令需要一个ID作为实体的主键,然后才能将实体持久化到数据库中。所以你必须设置一个id或者让条令为你生成一个id。使用annoation@ORM\GeneratedValuestrategy=AUTO,您可以告诉条令为该实体生成适当的id,而不必担心它

编辑:

如果要实现Userstest和Usersinformation具有相同的id,可以这样做:

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

$product = new Userstest();
$product->setUsername('aa')->setPassword('123456');

$em->persist($product);
$em->flush();

$information = new UsersInformation();
$information->setCompany('firma');
$information->setUserId($product->getUserId()); // Set the User Id

$product->setInformation($information);

$em->persist($information);
$em->persist($product);
$em->flush();
将@ORM\GeneratedValuestrategy=AUTO添加到UserInformation类中的$user_id中,或使用setUserId为实体设置显式id

说明:

错误告诉您,条令需要一个ID作为实体的主键,然后才能将实体持久化到数据库中。所以你必须设置一个id或者让条令为你生成一个id。使用annoation@ORM\GeneratedValuestrategy=AUTO,您可以告诉条令为该实体生成适当的id,而不必担心它

编辑:

如果要实现Userstest和Usersinformation具有相同的id,可以这样做:

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

$product = new Userstest();
$product->setUsername('aa')->setPassword('123456');

$em->persist($product);
$em->flush();

$information = new UsersInformation();
$information->setCompany('firma');
$information->setUserId($product->getUserId()); // Set the User Id

$product->setInformation($information);

$em->persist($information);
$em->persist($product);
$em->flush();

如果您可以根据自己的需要自由修改数据库,我会选择一对一关系的基本实现,这意味着在您的一个表中增加一列,并放弃共享ID的要求

如果您坚持,下面给出的链接可能是解决方案取决于您使用的Doctrine2版本:

文件规定:

通过外国实体的身份仅得到第2.1条原则的支持

我不确定这是否意味着它仅在版本2.1中受支持,或者是否从版本2.1开始受支持


请记住,与正常的一对一关系相比,这种解决方案的性能会受到影响。

如果您可以根据自己的需要自由修改数据库,我会选择一对一关系的基本实现,这意味着在一个表中增加一列,并放弃共享ID的要求

如果您坚持,下面给出的链接可能是解决方案取决于您使用的Doctrine2版本:

文件规定:

通过外国实体的身份仅得到第2.1条原则的支持

我不确定这是否意味着它仅在版本2.1中受支持,或者是否从版本2.1开始受支持


请记住,与正常的一对一关系相比,这种解决方案的性能会受到影响。

但我需要Userstest.user\u id==UsersInformation.user\u id,所以当我放置新用户时,new UsersInformation从user获取id-如何做?编辑了我的答案。。。检查这是否解决了您的问题。我编辑询问-两种方式都不符合我的需要-用户的用户id信息未填写-我必须先保存用户ST,然后添加用户信息以获取id?这是自动值吗?你说得对。您必须首先保存Userstest,以便它有一个id。我更新了我的代码示例。要自动分配相同的id,请查看Rene Terstegen答案中的链接。但老实说,如果可能的话,我可能会建议将这些实体合并成一个实体。我尝试切换成功:但我需要Userstest.user\u id==UsersInformation.user\u id,所以当我放置新用户时,new UsersInformation从user获取id-如何做?编辑了我的答案。。。检查这是否解决了您的问题。我编辑询问-两种方式都不符合我的需要-用户的用户id信息未填写-我必须先保存用户ST,然后添加用户信息以获取id?这是自动值吗?你说得对。您必须首先保存Userstest,以便它有一个id。我更新了我的代码示例。要自动分配相同的id,请查看Rene Terstegen答案中的链接。但老实说,我不会 如果可能,uld可能建议将这些实体合并为一个。我尝试切换成功:我尝试切换成功:我尝试切换成功:
$em = $this->getDoctrine()->getManager();

$product = new Userstest();
$product->setUsername('aa')->setPassword('123456');

$em->persist($product);
$em->flush();

$information = new UsersInformation();
$information->setCompany('firma');
$information->setUserId($product->getUserId()); // Set the User Id

$product->setInformation($information);

$em->persist($information);
$em->persist($product);
$em->flush();