Answer the question
In order to leave comments, you need to log in
Why can't doctrine put down the correct id when creating a database entry in postgresql?
Good day.
There are two entities and there is a one-to-one relationship. These are users and user_info. I connected them like this:
/**
* Users
*
* @ORM\Table(name="users")
* @ORM\Entity
* @UniqueEntity("email")
*/
class User implements UserInterface
{
/**
* @var integer
* @ORM\Id
* @ORM\Column(nullable=false)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
// ...
/**
* @ORM\OneToOne(targetEntity="AppBundle\Entity\UserInfo", mappedBy="user", cascade={"persist"})
*/
protected $information;
/**
* UserInfo
*
* @ORM\Table(name="user_info")
* @ORM\Entity
*/
class UserInfo
{
/**
* @var integer
* @ORM\Id
* @ORM\Column(nullable=false)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="AppBundle\Entity\User", inversedBy="userInfo")
* @ORM\JoinColumn(name="id", referencedColumnName="id", nullable=false)
*/
protected $user;
$user = $form->getData();
$user->setCreated(date('Y-m-d H:i:s'));
$user->getInformation()->setBalance(0);
$user->getInformation()->setRevShare(0);
$user->getInformation()->setMessengerType('skype');
$encoder = $this->container->get('security.password_encoder');
$encoded = $encoder->encodePassword($user, $user->getPassword());
$user->setPassword($encoded);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
SELECT NEXTVAL('users_id_seq');
SELECT NEXTVAL('user_info_id_seq');
Answer the question
In order to leave comments, you need to log in
You have a UserInfo control entity, it should be the other way around.
Now we need to explicitly give userInfo to the manager
$userInfo->setUser($user);
$em->persist($user);
$em->persist($userInfo);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question