Skip to content

Loading…

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

Closed
doctrinebot opened this Issue · 2 comments

2 participants

@doctrinebot

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

@doctrinebot

Comment created by @beberlei:

Fixed

@doctrinebot

Issue was closed with resolution "Fixed"

@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot added this to the 2.2.3 milestone
@doctrinebot doctrinebot closed this
@doctrinebot doctrinebot added the Bug label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.