开发者

how to model inheritance in doctrine2 with yaml?

How to declare doctrine2 inheritance the yaml way?

I don't find any code snippets, examples or cookbook art开发者_StackOverflow社区icles about this in the doctrine documentation.

When i try it the doctrine1 way, i get an error that the Entity has no primary key.

Thanks!


Try doing a simple model inheritance using the examples in the documentation (which are in @Annotations format), and converting them to yaml using the doctrine command line tool with parameters orm:convert-mapping (which converts mapping information between supported formats). More information here.


There are a few different types of inheritance in Doctrine2. Here are examples for the two most common types:


Mapped Superclass

# MyProject.Model.Person.dcm.yml
MyProject\Model\Person:
  type: mappedSuperClass
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string
      length: 50
   ...

# MyProject.Model.EmployedPerson.dcm.yml
MyProject\Model\EmployedPerson:
  type: entity
  fields:
    occupation:
      type: string
      length: 100
    ...

Then, in your PHP classes:

# Person.php

<?php
namespace MyProject\Model;
class Person
{
    private $id;
    private $name;

   // Add public getters and setters
}

# EmployedPerson.php

<?php
namespace MyProject\Model;
class EmployedPerson extends Person
{
    private $occupation;

    // Add public getters and setters
}

There are two main things you need to do to make this work: use type: mappedSuperClass instead of type: entity on the parent, and to ensure that your PHP child class extends the parent class.

You can add whatever fields and relationships you need to either class, although you should note the warning in the docs regarding the relationships you can add to the parent:

A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only). This means that One-To-Many associations are not possible on a mapped superclass at all. Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity at the moment. For further support of inheritance, the single or joined table inheritance features have to be used.


Single-Table Inheritance

Conveniently, the docs already give an example of a YAML config for Single-Table Inheritance:

MyProject\Model\Person:
  type: entity
  inheritanceType: SINGLE_TABLE
  discriminatorColumn:
    name: discr
    type: string
  discriminatorMap:
    person: Person
    employee: Employee

MyProject\Model\Employee:
  type: entity


It exist three main strategies to manage inheritance in relational database.

  • Single table inheritance (STI) (Martin fowler link)
  • Concrete table inheritance (Martin fowler link)
  • Class per table inheritance (Martin fowler link)

You can find how create each of this strategies on the Symfony website in the YAML format with Doctrine 2 : YAML Inheritance with Doctrine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜