【发布时间】:2014-03-05 16:00:41
【问题描述】:
我应该如何注释继承才能找到子类? 我当前的注释:
<?php
// Product.php
namespace Entity;
/**
* @Entity @Table(name="products")
**/
class Product
{
/**
* @var int
* @Id @Column(type="integer") @GeneratedValue
*/
protected $id;
}
<?php
namespace Entity;
// Coffee.php
/**
* @Entity
*/
class Coffee extends Product
{
protected $taste;
}
当我坚持新实体但无法通过 Id 获取现有实体时,它就像一个魅力
$entityManager->find('Entity\\Coffee', 1)
有错误:
找不到列:1054 'where 子句'中的未知列't0.id'
我不能将MappedSuperclass 用于产品。
更新:
Product::taste 不是数据库列。这是一个POPO字段。我必须将自己与 Doctrine ORM 继承混淆,后者旨在扩展模型以保留扩展实体的额外数据。在我的情况下,子类没有额外的列,而是定义了基类的不同行为。此外,两者都是可以互换的,所以我希望
$coffee = new Entity\Coffee();
$entityManager->persist($coffee);
$entityManager->flush();
/* @var Entity\Product $product */
$product = $entityManager->find('Entity\\Product', $coffee->getId());
/* @var Entity\Coffee $coffee */
$coffee = $entityManager->find('Entity\\Coffee', $product->getId());
看来我真的不需要 ORM 继承,而只是将 products 表中的数据水合到基础对象或子对象中。但不确定是否可能。
【问题讨论】:
-
只需将带有正确注释的受保护ID添加到子类中...没有注释学说就无法调用存储库上的find方法。
-
@Rufinus 谢谢,但这无济于事。 AFAIK AnnotationDriver 从父类获取注释,我的问题在于元数据工厂。它为子类创建1个别名't0'并在
where子句中使用它,然后创建另一个别名't1'并在from子句中使用。 -
@bspellmeyer 这是一个单表。我尝试显式添加
@InheritanceType("SINGLE_TABLE"),但没有任何效果。不确定鉴别器是否可以解决问题,但如果可能的话,我想避免在数据库中使用无意义的列。
标签: php inheritance orm doctrine-orm