Doctrine & oneToOne association & class inheritance

时间:2018-06-04 16:52:53

标签: php symfony doctrine-orm doctrine symfony-3.4

I have two classes:

class BaseIndividualIdentity {
    /**
     * @JMS\Exclude()
     * @ORM\Id
     * @ORM\Column(type="integer", nullable=false)
     * @ORM\GeneratedValue
     */
    protected $id;

    /**
     * @var IdentityDocument
     *                       
     * @ORM\OneToOne(targetEntity="IdentityDocument", orphanRemoval=true, cascade={"persist"})     
     * @ORM\JoinColumn(name="join_main_identity_document_id", referencedColumnName="id")          
     */
    protected $mainIdentityDocument;

and

class IndividualIdentity extends BaseIndividualIdentity {
}

Problem is that symfony/doctrine autogenerated postgressql code (by doctrine:schema:update) looks like this:

CREATE TABLE var.base_individual_identity (id INT NOT NULL, join_main_identity_document_id INT DEFAULT NULL, PRIMARY KEY(id));
CREATE TABLE var.individual_identity (id INT NOT NULL, PRIMARY KEY(id));

Similar (wrongly) result for mysql.

Code for individual_identity miss join_main_identity_document_id.

But for example when i try persist IndividualIdentity i get error:

 An exception occurred while executing 'INSERT INTO var.individual_identity (id, join_main_identity_document_id) VALUES (?, ?)' with params [1, 1]:       

  SQLSTATE[42703]: Undefined column: 7 ERROR:  column "join_main_identity_document_id" of relation "individual_identity" does not exist

So in that context seems that inheritance works well. So my question is, what i do wrong? ;)

1 个答案:

答案 0 :(得分:0)

You could use the Inheritance Mapping with annotations

/** @MappedSuperclass */
class MappedSuperclassBase
{
    /** @Column(type="integer") */
    protected $mapped1;
    /** @Column(type="string") */
    protected $mapped2;
    /**
     * @OneToOne(targetEntity="MappedSuperclassRelated1")
     * @JoinColumn(name="related1_id", referencedColumnName="id")
     */
    protected $mappedRelated1;

    // ... more fields and methods
}

See this

I hope it helps