原则坚持继承学说实体的php类 [英] doctrine persist php class which inherit doctrine entity

查看:64
本文介绍了原则坚持继承学说实体的php类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能扩展一个学说实体的php类并持久化?



示例:

  / ** 
* @Entity
* /
class A {
/ **
* @ ORM\\ \\ ManyToMany(targetEntity =C,mappedBy =parents)
* /
protected $ children;
}

class B扩展A {
...
}

class C {
/ **
* @ ORM\ManyToMany(targetEntity =A,inversedBy =children)
* /
protected $ parents;
}

$ b = new B();
$ em-> persist($ b);


解决方案

是的,这被称为继承映射,但是子类必须明确声明为 @Entity ,并且必须明确定义其映射(如果子类添加额外的属性)。



最常见的继承映射形式是单表继承,下面是Doctrine手册中的这种映射示例:



< pre class =lang-php prettyprint-override> 命名空间MyProject\Model;

/ **
* @Entity
* @InheritanceType(SINGLE_TABLE)
* @DiscriminatorColumn(name =discr,type =string)
* @DiscriminatorMap({person=Person,employee=Employee})
* /
class Person
{
//。 ..
}

/ **
* @Entity
* /
class员工扩展Person
{
// ...
}


Is it possible to have a php class which extends a doctrine entity and to persist it ?

example:

/**
 * @Entity
 */
class A {
  /**
   * @ORM\ManyToMany(targetEntity="C", mappedBy="parents")
   */
  protected $children;
}

class B extends A {
...
}

class C {
  /**
   * @ORM\ManyToMany(targetEntity="A", inversedBy="children")
   */
  protected $parents;
}

$b = new B();
$em->persist($b);

解决方案

Yes it's possible, this is called inheritance mapping, but the child class has to be explicitly declared as @Entity and its mapping has to be explicitly defined as well (if the child class adds extra properties).

The most common form of inheritance mapping is single table inheritance, here is an example of such mapping from the Doctrine manual:

namespace MyProject\Model;

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"person" = "Person", "employee" = "Employee"})
 */
class Person
{
    // ...
}

/**
 * @Entity
 */
class Employee extends Person
{
    // ...
}

这篇关于原则坚持继承学说实体的php类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆