S
S
Sergey2015-08-10 14:22:36
Windows
Sergey, 2015-08-10 14:22:36

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;

I add data like this:
$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();

The bottom line is that the id of the main entity (Users) is set, and the User_info id is set to null and the PG refuses to execute such a request, they say, id cannot be null , although, judging by the symfony profiler, the doctrine fulfills both requests:
SELECT NEXTVAL('users_id_seq');
SELECT NEXTVAL('user_info_id_seq');

What can be wrong?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Konstantin Tsvetkov, 2019-01-27
@Kennius

1C .

D
Denis, 2015-08-10
@prototype_denis

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

S
Sergey, 2015-08-10
Protko @Fesor

read about cascading persist . And yes, I would try to expand the connection if I were you. Better yet, upgrade to Doctrine 2.5 and use embeddable instead of one-to-one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question