1

我有三个捆绑包:

  • MainBundle - 包含实体和所有通用功能
  • BundleA 和 BundleB 将扩展 MainBundle 中的实体,并且每个都将在该包中实现接口。

使用此解决方案,我希望 MainBundle 不知道其他捆绑包,并且仅具有实体应具有的默认功能。其他两个捆绑包知道 MainBundle(但彼此不知道)并具有一些扩展功能。

MainBundle 中的示例实体:

<?php
namespace Random\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

class Person
{
  protected $id;

  protected $first_name;

  protected $last_name;

  public function getId()
  {
    return $this->id;
  }

  public function getFirstName()
  {
    return $this->first_name;;
  }

  public function getLastName()
  {
    return $this->last_name;;
  }
}

BundleA 中的示例类扩展 MainBundle 中的实体:

<?php
namespace Random\BundleA\Model;
use Random\MainBundle\Entity\Person as BasePerson;
use Random\BundleA\Model\PersonInterface;

class Person extends BasePerson implements PersonInterface
{
  public function foo()
  {
    $data = array(
        'object_id' = $this->getId(),
        'name' = $this->getFirstName(),
        'extra' = 'foo'
    );
    return json_encode($data);
  }
}

BundleB 中的示例类扩展 MainBundle 中的实体:

<?php
namespace Random\BundleB\Model;
use Random\MainBundle\Entity\Person as BasePerson;
use Random\BundleB\Model\PersonInterface;

class Person extends BasePerson implements PersonInterface
{
  public function bar()
  {
    $data = array(
        'person_id' = $this->getId(),
        'last_name' = $this->getLastName(),
        'random' = 'bar'
    );
    return $data;
  }
}

这个设置的问题是 Symfony/Doctrine 期望 Random\MainBundle\Entity\Person 是“映射的超类”,我不希望它是。

编辑:

在 Doctrine1 中,我能够扩展任何 Doctrine_Record 而无需定义简单、具体或列聚合继承:

<?php
class Person extends BasePerson
{
  // some functionality
}

class TestPerson extends Person
{
  // additional functionality
}

在控制器中,我能够简单地:

<?php
$person = new TestPerson();
$person->save();

并且TestPerson(实际上是Person)被保存到数据库中(我后来能够使用PersonTable访问)。

有任何想法吗?

4

0 回答 0