1

I have a problem with Doctrine 2 and OneToOne relation when it's the time to insert new infos in the database.

Model: User.php

<?php

/**
 * @Entity @Table(name="user")
 **/
class User
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="string") **/
    protected $lastname;
    /** @Column(type="string") **/
    protected $firstname;
    /** @Column(type="string") **/
    protected $email;
    /** @Column(type="integer") **/
    protected $status;
    /** @Column(type="string") **/
    protected $created;
    /** @Column(type="integer") **/
    protected $created_by;

   /**
    * @OneToOne(targetEntity="UserAuth", mappedBy="user")
    */
    protected $auth;

    public function getAuth()
    {
        return $this->auth;
    }

    public function setAuth($auth)
    {
        $this->auth = $auth;
    }
    // + all setters and getters...
}

Model: UserAuth.php

<?php

/**
 * @Entity @Table(name="user_auth")
 **/
class UserAuth
{
    /** @Id @Column(type="integer") @GeneratedValue **/
    protected $id;
    /** @Column(type="integer") **/
    protected $user_id;
    /** @Column(type="string") **/
    protected $username;
    /** @Column(type="string") **/
    protected $password;
    /** @Column(type="string") **/
    protected $role;

    /**
     * @OneToOne(targetEntity="User", inversedBy="auth")
     * @JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    public function getUser()
    {
        return $this->user;
    }
    // + all setters and getters...
}

No problem to retrieve the data and update them:

$user = $orm->find('User', 2);

echo $user->getAuth()->getUsername();

$user->getAuth()->setUsername('jprlauzier');

$orm->persist($user);

$orm->flush();

But, impossible to create a new one:

$user = $orm->find('User', 3);

$auth = new UserAuth();
$auth->setUsername('newusername');
$auth->setPassword('51abb9636078defbf888d8457a7c76f85c8f114c');
$auth->setRole('user');

$orm->persist($auth);

$user->setAuth($auth);

$orm->persist($user);

$orm->flush();

The errors:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity         constraint violation: 1048 Column 'user_id' cannot be null' in     /Applications/MAMP/htdocs/test/library/Doctrine/DBAL/Statement.php:131 Stack trace: #0     /Applications/MAMP/htdocs/test/library/Doctrine/DBAL/Statement.php(131): PDOStatement-    >execute(NULL) #1 /Applications/MAMP/htdocs/test/library/Doctrine/ORM/Persisters/BasicEntityPersister.php(239): Doctrine\DBAL\Statement->execute() #2 /Applications/MAMP/htdocs/test/library/Doctrine/ORM/UnitOfWork.php(870): Doctrine\ORM\Persisters\BasicEntityPersister->executeInserts() #3 /Applications/MAMP/htdocs/test/library/Doctrine/ORM/UnitOfWork.php(304): Doctrine\ORM\UnitOfWork->executeInserts(Object(Doctrine\ORM\Mapping\ClassMetadata)) #4 /Applications/MAMP/htdocs/test/library/Doctrine/ORM/EntityManager.php(355): Doctrine\ORM\UnitOfWork->commit(NULL) #5 /Applications/MAMP/htdocs/test/application/Bootstrap.php(106): Doctrine\ORM\EntityManager->flush() #6 /Applications/MAMP/htdocs/test/application/Bo in /Applications/MAMP/htdocs/test/library/Doctrine/DBAL/Statement.php on line 131

Doctrine doesn't find the user_id to create a new entry in the table user_auth. But, I indicated the @JoinColumn(name="user_id", referencedColumnName="id") in my model...for your help! I searched all day for this.

Peter O.
  • 32,158
  • 14
  • 82
  • 96

2 Answers2

4

When you create the User instance, the instance of User::id is still null. It is why the doctrine return the constraint violation.

see here http://docs.doctrine-project.org/en/2.0.x/reference/working-with-associations.html#transitive-persistence-cascade-operations

yoshi
  • 41
  • 2
  • Working link: http://docs.doctrine-project.org/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations – Dalius Oct 23 '14 at 11:53
0

Remove:

/** @Column(type="integer") **/
protected $user_id;

And update the database structure.

meze
  • 14,975
  • 4
  • 47
  • 52