Jump to content
Sign in to follow this  
NotionCommotion

Creating Doctrine inherited class

Recommended Posts

I am trying to class type inheritance with Doctrine.  Eventually, I will be defining things using XML, but all the examples are done using doctags.  I found the following and it seems to work.

<?php
use Doctrine\ORM\Mapping as ORM;

/**
 * Inventory entity
 *
 * @ORM\Entity(repositoryClass="Inventory")
 * @ORM\Table(name="inventory")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discriminator_column", type="string")
 * @ORM\DiscriminatorMap({"inventory" = "AbstractItem", "inventory_room" = "Room"})
 */
abstract class AbstractItem
{
    /**
     * @ORM\Id @ORM\Column(name="inventory_id") @ORM\GeneratedValue
     * @var int
     */
    protected $inventory_id;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $category;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $part_number;
 }


use Doctrine\ORM\Mapping as ORM;

/**
 * Inventory Room entity
 * @ORM\Entity
 * @ORM\Table(name="inventory_room")
 */
class Room extends AbstractItem
{
    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $room_type;

    /**
     * @ORM\Column(type="string")
     * @var string
     */
    protected $rack_limit;

 

But then I convert it to XML or YAML, and I get a Entity class 'Room' used in the discriminator map of class 'AbstractItem' does not exist error when trying to build the classes.  I've also tried changing the type to mappedSuperclass, but no success.

Any ideas?

Also, what is the purpose of the DiscriminatorColumn?

Thanks

 

AbstractItem:
    type: entity
    table: inventory
    inheritanceType: JOINED
    discriminatorColumn:
        name: discriminator_column
        type: string
    discriminatorMap:
        inventory: AbstractItem
        inventory_room: Room
    repositoryClass: Inventory
    id:
        inventory_id:
            type: string
    fields:
        category:
            type: string
        part_number:
            type: string


Room:
    type: entity
    table: inventory_room
    fields:
        room_type:
            type: string
        rack_limit:
            type: string



 

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity repository-class="Inventory" name="AbstractItem" table="inventory" inheritance-type="JOINED">
    <discriminator-column name="discriminator_column" type="string" length="255"/>
    <discriminator-map>
      <discriminator-mapping value="inventory" class="AbstractItem"/>
      <discriminator-mapping value="inventory_room" class="Room"/>
    </discriminator-map>
    <id name="inventory_id" type="string" column="inventory_id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="category" type="string" column="category" precision="0" scale="0" nullable="false"/>
    <field name="part_number" type="string" column="part_number" precision="0" scale="0" nullable="false"/>
  </entity>
</doctrine-mapping>


<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="Room" table="inventory_room">
    <id name="inventory_id" type="string" column="inventory_id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="room_type" type="string" column="room_type" precision="0" scale="0" nullable="false"/>
    <field name="rack_limit" type="string" column="rack_limit" precision="0" scale="0" nullable="false"/>
    <field name="category" type="string" column="category" precision="0" scale="0" nullable="false"/>
    <field name="part_number" type="string" column="part_number" precision="0" scale="0" nullable="false"/>
  </entity>
</doctrine-mapping>

 

Share this post


Link to post
Share on other sites

Figured it out.  Needed to put an empty class called "Room" in the directory where I was creating the classes.  Don't know why I had to do so, however...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.