symfony 2 doctrine relation onetoone

荒凉一梦 提交于 2019-12-14 02:54:31

问题


How to get and set entity witch onetoone relation like my example.

I have error :

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.

in php controller - i try save new item in this way:

$product = new Userstest();

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

$product->setInformation((new UsersInformation())->setCompany('firma'));

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

when i save in this way

$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);

and have * @ORM\GeneratedValue(strategy="AUTO") UsersInformation.for user_id have :

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
)

so not work, but when remove * @ORM\GeneratedValue(strategy="AUTO")

get error: 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] => test3@a.pl
            [gender] => 
            [company] => 
        )

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

Entities :

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

回答1:


Add @ORM\GeneratedValue(strategy="AUTO") to the $user_id in your UserInformation class or use setUserId to set an explicit id for your entity.

Explanation:

The error tells you, that Doctrine needs an ID (the primary key of your entity) before it can persist the entity to the database. So you have to set an id or let doctrine generate an id for you. With the annoation @ORM\GeneratedValue(strategy="AUTO") you tell Doctrine to generate an appropriate id for this entity and don't have to worry about it.

Edit:

If you want to implement that Userstest and Usersinformation have the same id you can do it like that:

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



回答2:


If you have the freedom to modify the database to your needs I would go for the basic implementation of the one-to-one relationship (which means an extra column in one of your tables) and drop the requirement of sharing the ID's.

If you insist, maybe the link below gives is the solution (depends on the version of Doctrine2 you use):

http://docs.doctrine-project.org/en/latest/tutorials/composite-primary-keys.html#use-case-2-simple-derived-identity

The documentation states:

Identity through foreign entities is only supported with Doctrine 2.1

I'm not sure if this means it is only supported in version 2.1 or if it is supported since version 2.1

And remember that this kind of solutions have a performance hit compared to normal one-to-one relationships.



来源:https://stackoverflow.com/questions/20653209/symfony-2-doctrine-relation-onetoone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!