symfony 2主义关系一个人 [英] symfony 2 doctrine relation onetoone

查看:164
本文介绍了symfony 2主义关系一个人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取和设置实体女巫一样的关系,就像我的例子。



我有错误:


实体类型 Miejsce\ObiektyBundle\Entity\UsersInformatio n缺少字段' user_id '的分配ID。该实体的标识符生成策略要求在调用 EntityManager#persist()之前填充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对象

[user_id :protected] => 9
[information:protected] => Miejsce\ObiektyBundle\Entity\UsersInformation对象

[user_id:protected] => 5
[user:protected] =>
[email] => test3@a.pl
[gender] =>
[company] =>


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

所以不工作,
但是当删除* @ ORM\GeneratedValue(strategy =AUTO)



获取错误:
类型为Miejsce\ObiektyBundle\Entity\UsersInformation的实体缺少字段user_id的分配的ID。该实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果您想要自动生成的标识符,则需要相应地调整元数据映射。

  Miejsce\ObiektyBundle\Entity\Userstest对象

[user_id:protected] = > 9
[information:protected] => Miejsce\ObiektyBundle\Entity\UsersInformation对象

[user_id:protected] => 5
[用户:protected] =>
[email] => test3@a.pl
[gender] =>
[company] =>


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

实体:

 <?php 
命名空间Miejsce\ObiektyBundle\\ \\实体;

使用Doctrine\ORM\Mapping作为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 
命名空间Miejsce\ObiektyBundle\Entity;

使用Doctrine\ORM\Mapping作为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\ GeneratedValue(strategy =AUTO)到您的 UserInformation 类中的 $ user_id 使用 setUserId 为您的实体设置一个显式ID。



说明: p>

这个错误告诉你,Doctrine需要一个ID(您的实体的主键)才能将该实体持久化到数据库。所以你必须设置一个id,或者让doctrine为你生成一个id。使用烦恼 @ ORM\GeneratedValue(strategy =AUTO)你告诉Doctrine为这个实体生成一个适当的id,而不必担心。 / p>

编辑:



如果要实现 Userstest 用户信息具有相同的身份,您可以这样做:

  $ 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()); //设置用户ID

$ product-> setInformation($ information);

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


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;

解决方案

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

这篇关于symfony 2主义关系一个人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆