- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将接口(interface)用作“targetEntity”。
简单的代码应该解释我打算做什么
界面:
namespace Project\Entity;
interface AnimalInterface{
}
namespace Project\Entity;
use Doctrine\ORM\Mapping as ORM;
use Project\Entity\AnimalInterface;
/**
* Represents an Invoice.
*
* @ORM\Entity
* @ORM\Table(name="Cat")
*/
class Cat implements AnimalInterface {
/**
* @var int
* @ORM\Id @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue
*/
protected $id;
}
namespace Project\Entity;
use Doctrine\ORM\Mapping as ORM;
use Project\Entity\AnimalInterface;
/**
* @ORM\Entity
* @ORM\Table(name="Dog")
*/
class Dog implements AnimalInterface {
/**
* @var int
* @ORM\Id @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue
*/
protected $id;
}
namespace Project\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="AnimalFarm")
*/
class AnimalFarm {
/**
*
* @var int
* @ORM\Id @ORM\Column(type="integer", name="id")
* @ORM\GeneratedValue
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Project\Entity\AnimalInterface")
* @var AnimalInterface
*/
protected $animal;
public function setAnimal(AnimalInterface $animal){
$this->animal = $animal;
}
}
$em = $doctrine->getEntityManager();
$evm = $em->getEventManager();
$listener = new \Doctrine\ORM\Tools\ResolveTargetEntityListener();
$listener->addResolveTargetEntity(
'Project\Entity\AnimalInterface',
'Project\Entity\Dog',
array()
);
$listener->addResolveTargetEntity(
'Project\Entity\AnimalInterface',
'Project\Entity\Cat',
array()
);
$evm->addEventListener(Doctrine\ORM\Events::loadClassMetadata, $listener);
最佳答案
这取自 doctrine2 docs .您只能使用此方法解析一个对象。
/**
* An interface that the invoice Subject object should implement.
* In most circumstances, only a single object should implement
* this interface as the ResolveTargetEntityListener can only
* change the target to a single object.
*/
interface InvoiceSubjectInterface
{
// List any additional methods that your InvoiceModule
// will need to access on the subject so that you can
// be sure that you have access to those methods.
/**
* @return string
*/
public function getName();
}
关于zend-framework - Doctrine targetEntity 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14359765/
当我尝试执行 ./app/console doctrine:migrate:diff 时出现以下错误: [Doctrine\ORM\Mapping\MappingException]
我在用户实体的创建 View 中为关系定义了一个 targetEntity,但是当列表加载实体时,api 调用包括默认分页,因此列表不完整。 nga.field('CompanyId', 'refer
我最近试图为我的一些 hibernate 映射实体引入接口(interface),但不知道如何配置映射。 当我在没有任何进一步声明的情况下使用接口(interface)时,我总是得到以下异常: org
我有一个枚举类RoleType public enum RoleType { SYSTEM_ADMIN, PROJECT_ADMIN, USER; } 在我的 User 实体类中,我有以下枚举
当我设置多对一映射时,虽然两个类都在同一个命名空间中,但它可以工作。 但是如果这两个类在不同的命名空间中,它将不起作用? /** * @ORM\ManyToOne(targetEntity="OP\
我正在尝试将接口(interface)用作“targetEntity”。 简单的代码应该解释我打算做什么 界面: namespace Project\Entity; interface AnimalI
我想在另一个 bundle “targetEntity”属性中使用实体,但它生成了错误... 此类之间: namespace Tgb\CoreBundle\Entity; use Doctrine\O
我正在尝试使用 Hibernate 和 javax.persistence 注释开发一个应用程序。我在映射接口(interface)时遇到了一些麻烦。 我已经将我的接口(interface)映射为@M
我正在为 ORM 使用 hibernate 和注释。 我有所有 bean 的接口(interface),并且我使用 targetEntity 来建立关系: hibernate 无法派生类型,因为 ge
我是一名优秀的程序员,十分优秀!