5
votes

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;
}
1
Does the referencedColumnName of your joinColumn is the primary key of the Location entity?chalasr
@chalasr, Yes, and added it to the op. I've got it working by creating an extra abstract Admin class, then extending both AdminDivision and AdminSubDivision from that. But I'd still like to know why the above code won't work.Twifty
sorry but I don't see the why, hope someone else can give it, If nobody, I'll try it on my side. Another workaround would be to use a trait and use it in your two classes and BTW make the classes able to extend another while keeping the $location property.chalasr

1 Answers

1
votes

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