Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending an entity class in doctrine

I'm trying to extend a class used as a doctrine entity, but for some reason I keep getting the error:

There is no column with name 'location_id' on table 'admin_subdivisions'

When I say extend, I mean at the php level NOT the database level. I simply want to create another table, with an extra column. I have several entities which extend the following abstract class

abstract class LocationProxy
{
    /**
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="Location", cascade={"ALL"}, fetch="LAZY")
     * @ORM\JoinColumn(name="location_id", referencedColumnName="location_id", nullable=false)
     * 
     * @var Location
     */
    protected $location;
}

None of these second level classes give me any problems. Now, I want to extend this second level class

/**
 * @ORM\Entity()
 * @ORM\Table(name="admin_divisions")
 */
class AdminDivision extends LocationProxy
{
}

with this

/**
 * @ORM\Entity()
 * @ORM\Table(name="admin_subdivisions")
 */
class AdminSubDivision extends AdminDivision
{
}

but, it produces the error. Can anybody point out what I am doing wrong?

here is the Location class definition

/**
 * @ORM\Entity()
 * @ORM\Table(name="locations")
 */
class Location
{
    /**
     * @ORM\Id
     * @ORM\Column(name="location_id", type="integer", options={"unsigned"=true})
     * 
     * @var int
     */
    private $id;
}
like image 259
Twifty Avatar asked Nov 09 '22 17:11

Twifty


1 Answers

You must specify the inheritence type so that doctrine knows how to build the tables for the subclasses: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#mapped-superclasses

In you case you will need to add the following Annotations to your abstract LocationProxy Class:

 @ORM\Entity
 @ORM\InheritanceType("JOINED")
 @ORM\DiscriminatorColumn(name="discr", type="string")

Or choose a different inheritance type

So the whole class will look like this:

/**
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 */
abstract class LocationProxy {
    /**
     * @ORM\Id
     * @ORM\OneToOne(targetEntity="De\Gregblog\Receipts\Location", cascade={"ALL"}, fetch="LAZY")
     * @ORM\JoinColumn(name="location_id", referencedColumnName="location_id", nullable=false)
     * 
     * @var Location
     */
    protected $location;
}
like image 57
gregorius90 Avatar answered Nov 15 '22 05:11

gregorius90