DDC-1895: update an entity with an ID column which is a relation instead of a normal field #2390

Closed
opened 2026-01-22 13:51:28 +01:00 by admin · 2 comments
Owner

Originally created by @doctrinebot on GitHub (Jun 27, 2012).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user burgov:

I got this error when trying to update an entity with an ID column which is a relation instead of a normal field: https://gist.github.com/3399c0ad5e0a44a29f98

Here is the relevant mapping:

<?php

namespace Roompot\TRSBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Samson\Bundle\TRSBundle\Entity\Registrar;

/****
 * @ORM\Entity
 */
class RegistrarDepartmentMapping
{
    /****
     * @ORM\ManyToOne(targetEntity="RoompotRegistrar")
     * @ORM\JoinColumn(referencedColumnName="person_id")
     * @ORM\Id
     */
    private $registrar;

    /****
     * @ORM\ManyToOne(targetEntity="Department")
     * @ORM\Id
     */
    private $department;

    /****
     * @ORM\Column(type="boolean")
     */
    private $head = false;

    public function getRegistrar()
    {
        return $this->registrar;
    }

    public function setRegistrar(Registrar $registrar)
    {
        if (null !== $this->registrar) {
            throw new \RuntimeException('Cannot change registrar! Remove this entity and create a new one');
        }
        $this->registrar = $registrar;
    }

    public function getDepartment()
    {
        if (null !== $this->registrar) {
            throw new \RuntimeException('Cannot change department! Remove this entity and create a new one');
        }
        return $this->department;
    }

    public function setDepartment(Department $department)
    {
        $this->department = $department;
    }

    public function isHead()
    {
        return $this->head;
    }

    public function setHead($head)
    {
        $this->head = $head;
    }
}
<?php

namespace Roompot\TRSBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Samson\Bundle\TRSBundle\Entity\Registrar;

/****
 * @ORM\Entity
 */
class RoompotRegistrar extends Registrar
{
    [...]
}
<?php

namespace Samson\Bundle\TRSBundle\Entity;

use Samson\Bundle\AddressBookBundle\Entity\Person;
use Doctrine\ORM\Mapping as ORM;

/****
 * @ORM\Entity 
 * @ORM\InheritanceType("SINGLE_TABLE")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 */
abstract class Registrar
{
    /****
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="Samson\Bundle\AddressBookBundle\Entity\Person", cascade={"persist"})
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
     */
    private $person;

    [...]
}
<?php

namespace Samson\Bundle\AddressBookBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/****
 * @ORM\Entity(repositoryClass="Samson\Bundle\AddressBookBundle\Entity\PersonRepository")
 */
class Person implements [...]
{
    /****
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

    [...]
}

I was able to fix the error by updating BasicEntityPersister: https://github.com/SamsonIT/doctrine2/compare/fetchingid_column_ifrelation

Originally created by @doctrinebot on GitHub (Jun 27, 2012). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user burgov: I got this error when trying to update an entity with an ID column which is a relation instead of a normal field: https://gist.github.com/3399c0ad5e0a44a29f98 Here is the relevant mapping: ``` <?php namespace Roompot\TRSBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Samson\Bundle\TRSBundle\Entity\Registrar; /**** * @ORM\Entity */ class RegistrarDepartmentMapping { /**** * @ORM\ManyToOne(targetEntity="RoompotRegistrar") * @ORM\JoinColumn(referencedColumnName="person_id") * @ORM\Id */ private $registrar; /**** * @ORM\ManyToOne(targetEntity="Department") * @ORM\Id */ private $department; /**** * @ORM\Column(type="boolean") */ private $head = false; public function getRegistrar() { return $this->registrar; } public function setRegistrar(Registrar $registrar) { if (null !== $this->registrar) { throw new \RuntimeException('Cannot change registrar! Remove this entity and create a new one'); } $this->registrar = $registrar; } public function getDepartment() { if (null !== $this->registrar) { throw new \RuntimeException('Cannot change department! Remove this entity and create a new one'); } return $this->department; } public function setDepartment(Department $department) { $this->department = $department; } public function isHead() { return $this->head; } public function setHead($head) { $this->head = $head; } } ``` ``` <?php namespace Roompot\TRSBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Samson\Bundle\TRSBundle\Entity\Registrar; /**** * @ORM\Entity */ class RoompotRegistrar extends Registrar { [...] } ``` ``` <?php namespace Samson\Bundle\TRSBundle\Entity; use Samson\Bundle\AddressBookBundle\Entity\Person; use Doctrine\ORM\Mapping as ORM; /**** * @ORM\Entity * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn(name="discr", type="string") */ abstract class Registrar { /**** * @ORM\Id * @ORM\OneToOne(targetEntity="Samson\Bundle\AddressBookBundle\Entity\Person", cascade={"persist"}) * @ORM\JoinColumn(nullable=false, onDelete="CASCADE") */ private $person; [...] } ``` ``` <?php namespace Samson\Bundle\AddressBookBundle\Entity; use Doctrine\ORM\Mapping as ORM; /**** * @ORM\Entity(repositoryClass="Samson\Bundle\AddressBookBundle\Entity\PersonRepository") */ class Person implements [...] { /**** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue */ private $id; [...] } ``` I was able to fix the error by updating BasicEntityPersister: https://github.com/SamsonIT/doctrine2/compare/fetching*id_column_if*relation
admin added the Bug label 2026-01-22 13:51:28 +01:00
admin closed this issue 2026-01-22 13:51:29 +01:00
Author
Owner

@doctrinebot commented on GitHub (Jul 5, 2012):

Comment created by @beberlei:

Fixed

@doctrinebot commented on GitHub (Jul 5, 2012): Comment created by @beberlei: Fixed
Author
Owner

@doctrinebot commented on GitHub (Jul 5, 2012):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Jul 5, 2012): Issue was closed with resolution "Fixed"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#2390