symfony 2主义关系OneToOne

标签 symfony doctrine-orm one-to-one

如何获取和设置具有 OneToOne 关系的实体,如我的示例。

我有错误:

Entity of type Miejsce\ObiektyBundle\Entity\UsersInformation is missing an assigned ID for field 'user_id'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.

在 php Controller 中 - 我尝试以这种方式保存新项目:

$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] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dca8b9afa8ef9cbdf2acb0" rel="noreferrer noopener nofollow">[email protected]</a>
            [gender] => 
            [company] => 
        )

    [username:protected] => test3
    [password:protected] => 123456
)

这不起作用 但是当我删除 * @ORM\GenerateValue(strategy="AUTO")

我收到此错误:

Entity of type Miejsce\ObiektyBundle\Entity\UsersInformation is missing an assigned ID for field 'user_id'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.

Miejsce\ObiektyBundle\Entity\Userstest Object
(
    [user_id:protected] => 9
    [information:protected] => Miejsce\ObiektyBundle\Entity\UsersInformation Object
        (
            [user_id:protected] => 5
            [user:protected] => 
            [email] => <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f185948285c2b190df819d" rel="noreferrer noopener nofollow">[email protected]</a>
            [gender] => 
            [company] => 
        )

    [username:protected] => test3
    [password:protected] => 123456
)

实体:

<?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;

<?php
namespace Miejsce\ObiektyBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;


/**
 * @ORM\Entity
 * @ORM\Table(name="test_userInfo")
 */
class UsersInformation
{

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 */
protected $user_id;

/**
 * @ORM\OneToOne(targetEntity="Userstest", inversedBy="information")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="user_id")
 */
protected $user;


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

/**
 * @ORM\Column(type="string", length=1)
 */
public $gender;

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

最佳答案

@ORM\GenerateValue(strategy="AUTO") 添加到 UserInformation 类中的 $user_id 或使用 setUserId 为您的实体设置显式 ID。

说明:

该错误告诉您,Doctrine 需要一个 ID(实体的主键)才能将实体持久保存到数据库。所以你必须设置一个id或者让doctrine为你生成一个id。通过注释@ORM\GenerateValue(strategy="AUTO"),您可以告诉 Doctrine 为该实体生成适当的 id,而不必担心它。

编辑:

如果你想实现UserstestUsersinformation具有相同的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();

关于symfony 2主义关系OneToOne,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20653209/

相关文章:

php - 在实体库或任何地方访问 Symfony 配置参数

symfony - Gedmo Timestampable 和 Blameable 在合并实体后不起作用

java - 在 Hibernate 中映射两个表 0..n

c# - AspNetUsers' ID 作为单独表中的外键,一对一关系

java - 升级后,@MapsId 在保存现有实体时抛出错误,但在其他方面工作正常

symfony - Symfony 中的数组集合

php - Twig 根据条件扩展模板

php - 图标未加载

php - 找不到实体存储库方法

php - 如何从 symfony2 中的实体生成表?