damianociarla / dynamic-discriminator-map-bundle
DCSDynamicDiscriminatorMapBundle 简化了在 Symfony2 中使用 Doctrine 单表继承映射策略。
v1.0.2
2016-03-01 11:04 UTC
Requires
- php: >=5.3
- doctrine/doctrine-bundle: >=1.2
- doctrine/orm: >=2.2.3
- symfony/symfony: >=2.3
This package is not auto-updated.
Last update: 2024-09-14 15:21:48 UTC
README
DynamicDiscriminatorMapBundle 简化了在 Symfony2 中使用 Doctrine 单表和类表继承映射策略
使用此包,您可以通过配置文件动态地将层次结构中的所有类添加到 DiscriminatorMap 的选项中。这样,您可以应用“解耦”的方法
安装
步骤 1:使用 composer 下载 DynamicDiscriminatorMapBundle
在您的 composer.json 中添加 DynamicDiscriminatorMapBundle
{
"require": {
"damianociarla/dynamic-discriminator-map-bundle": "dev-master"
}
}
步骤 2:启用包
在内核中启用包
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new DCS\DynamicDiscriminatorMapBundle\DCSDynamicDiscriminatorMapBundle(),
);
}
配置
父类
这是一个父类的示例
<?php
namespace Acme\PersonBundle\Entity;
/**
* @ORM\Entity
* @ORM\Table(name="person")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"person" = "Person"})
*/
class Person
{
// ...
}
子类
这是两个子类的示例
<?php
namespace Acme\StudentBundle\Entity;
use Acme\PersonBundle\Entity\Person;
/**
* @ORM\Entity
*/
class Student extends Person
{
// ...
}
<?php
namespace Acme\TeacherBundle\Entity;
use Acme\PersonBundle\Entity\Person;
/**
* @ORM\Entity
*/
class Teacher extends Person
{
// ...
}
配置文件
# app/config/config.yml
dcs_dynamic_discriminator_map:
mapping:
person:
entity: Acme\PersonBundle\Entity\Person
map:
student: Acme\StudentBundle\Entity\Student
teacher: Acme\TeacherBundle\Entity\Teacher