如何獲取和設置實體女巫onetoone關係像我的例子。symfony 2學說關係onetoone
我有錯誤:
Entity of type
Miejsce\ObiektyBundle\Entity\UsersInformatio
n is missing an assigned ID for field 'user_id
'. The identifier generation strategy for this entity requires the ID field to be populated beforeEntityManager#persist()
is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.
在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] => [email protected]
[gender] =>
[company] =>
)
[username:protected] => test3
[password:protected] => 123456
)
這樣不行, 但當刪除* @ORM \ GeneratedValue(策略= 「AUTO」)
得到錯誤: 實體類型Miejsce \ ObiektyBundle \實體\ UsersInformation是缺少分配的ID字段'user_id'。此實體的標識符生成策略要求在調用EntityManager#persist()之前填充ID字段。如果您想要自動生成標識符,則需要相應地調整元數據映射。
Miejsce\ObiektyBundle\Entity\Userstest Object
(
[user_id:protected] => 9
[information:protected] => Miejsce\ObiektyBundle\Entity\UsersInformation Object
(
[user_id:protected] => 5
[user:protected] =>
[email] => [email protected]
[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;
但我需要Userstest.user_id == UsersInformation.user_id所以當我把新用戶新UsersInformation從用戶獲取ID - 如何做到這一點? –
編輯我的答案...檢查是否可以解決您的問題。 – Syjin
我編輯問 - 兩種方式不要像我需要的工作 - user_id用戶信息未填充 - 我必須先保存Userstest然後添加UsersInformation獲取ID?是不可能的,這是自動值? –