【发布时间】:2011-06-27 17:06:28
【问题描述】:
我在尝试实施 CTI 时遇到很多问题
首先我为我的实体类使用自定义加载器
class My_AutoLoader implements Zend_Loader_Autoloader_Interface
{
public function autoload($class)
{
$class = trim(str_replace('\\', '/', $class), '/');
if (@include(APPLICATION_PATH . '/Entities/' . $class . '.php')) {
return $class;
} else {
throw new Zend_Loader_Exception('Cannot load ' . $class . '.');
}
}
}
这个想法是对没有命名空间的类使用application\Entities
喜欢$user = new Users();
然后我定义了类继承
Profiles:
type: entity
table: profiles
repositoryClass: Repositories\Base
inheritanceType: JOINED
discriminatorColumn:
name: profiletype
type: integer
length: 11
discriminatorMap:
1: Personal
2: Work
3: Business
id:
id:
type: integer
generator:
strategy: AUTO
fields:
firstname:
type: string
length: 255
fixed: false
nullable: true
...
Work:
type: entity
table: work
repositoryClass: Repositories\Base
fields:
position:
type: string
length: 255
fixed: false
nullable: true
然后我手动创建了 Work 类来扩展 Profiles
class Work extends Profiles
{
}
第一个问题从 2.0.0 (2.0.1) 开始,当我使用控制台工具的 generate-entities 时,我收到 我没有 @987654327 的 id 的错误@ class,这很奇怪,因为恕我直言,它与 Work 扩展 Profiles 和 id 已经定义的想法相矛盾。
但是,我尝试为Work 类添加一个列id,但随后我收到一条消息,指出我已经有一个列id。哦!
我尝试为 PK 添加一些其他列名,但实际上我得到了一个 不必要的额外列,因为还创建了正确的继承列 id。在 CTI 中,我应该有一个 FK 列,并且没有其他 PK 具有自动生成的值。
所以我做了坏事来破解教义类并删除对丢失 id 的检查。丑陋但有效。实体开始正常生成,数据库结构也很好。
我后来发现所有这些奇怪的行为都是由于教义 2 中的一个错误,它已在 2.0.5 中修复。
好吧,我尝试了 2.0.5 并且 遇到了完全相同的问题,所以我认为错误出在我的代码中。
我在学说的 jira 中提交了一个错误,我得到的回答是我的定义是错误的,我需要子类的 id(并参考了我们所知道的文档很差,尤其是对于 YAML映射)。我放弃并坚持我的黑客。
后来我尝试使用 2.0.6 和 2.1,但在这些版本中,我的实体不再更新,但 每次我使用 generate-entities 时,新的类定义都会附加到末尾,因此会有重复。
我的问题是:
这是教义的问题还是我做错了?
如果在我身上,映射 CI 的正确方法是什么
【问题讨论】:
-
我已经成功地使用了 CTI 从 2.0.4 到 2.1 的注释。实体子类不需要 ID 字段,你可以确定。尽管如此,Doctrine 在子类 DB 表中自动创建了 FK/PK id 字段。尝试按照以下说明以编程方式创建模式:doctrine-project.org/docs/orm/2.0/en/reference/…。如果您认为有帮助,我可以发布我自己的代码(但它带有注释,而不是 yaml)。
-
实际上我经常使用注释作为参考来进行更复杂的定义,但似乎我这样做是正确的,可能错误在生成实体中,并且可能被我的自动加载器混淆了我正在使用的唯一奇怪的东西(这就是我也提供它的原因)。顺便说一句,如果我修复我的实体(删除重复项),那么创建 db 模式就很好。
标签: zend-framework doctrine doctrine-orm class-table-inheritance