How to get and set entity which 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 beforeEntityManager#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] => [email protected]
[gender] =>
[company] =>
)
[username:protected] => test3
[password:protected] => 123456
)
It does not work
but when I remove * @ORM\GeneratedValue(strategy="AUTO")
I get this 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] => [email protected]
[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;