Zend 框架 1.11 +学说 2 +生成实体的类继承映射 (YAML) 问题

发布于 2024-11-17 21:15:30 字数 2216 浏览 1 评论 0原文

我在尝试实现 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) 开始,当我使用控制台工具的生成实体我收到错误我没有Work类的id,这很奇怪,因为恕我直言,它与这个想法Work 扩展了 Profilesid 已定义。

但是,我尝试为 Work 类添加一列 id ,但随后我收到一条消息,表明我已经有一列 id 。卫生部!

我尝试为 PK 添加一些其他列名称,但实际上我得到了一个不必要的额外列,因为还创建了正确的继承列id。在 CTI 中,我应该有一个 FK 列,并且没有其他具有自动生成值的 PK。

所以我做了一件坏事来破解学说类并删除对丢失id的检查。丑陋但它有效。实体开始正确生成,数据库结构也很好。

我后来发现所有这些奇怪的行为都是由于原则 2 中的错误造成的,并且在 2.0.5 中已修复

好吧,我尝试了 2.0.5 并且遇到了完全相同的问题,所以我认为错误出在我的代码中。

我在粘性的 jira 中提交了一个错误,我得到的答复是我的定义是错误的,我需要子类的 id(并且参考了我们所知道的非常差的文档,特别是对于 YAML 映射)。我放弃了并坚持我的黑客。

后来我尝试使用 2.0.6 和 2.1,但在这些版本中,我的实体不再更新,但每次我使用生成实体时,新的类定义都会附加到末尾,因此存在重复

我的问题是:

这是教义的问题还是我做错了?

如果是我的话,映射 CI 的正确方法是什么

I get number of problems trying to implement CTI

First of all I use a custom loader for my entity classes

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 . '.');
        }
    }

}

The idea is to use application\Entities for classes that have no namespace
like $user = new Users();

Then I have defined class inheritance

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

then I have manually created the class Work to extend Profiles

class Work extends Profiles
{
}

The first problem begun with 2.0.0 (2.0.1), when I use console tool's generate-entities I get the error that I don't have id's for the Work class, it is odd because IMHO it contradicts with the idea that Work extends Profiles and id is already defined.

However I tried to add a column id for the Work class, but then I get a message that I already have a column id. DOH!

I tried to add some other column name for PK but I actually get an extra column that is unnecessary because the proper inherited column id is also created. In CTI i should have a single FK column and there are no other PK's with auto-generated values.

So I did the bad thing to hack the doctrine classes and remove the checks for missing id's. Ugly but it worked. The entities start to generate properly and the db structure is just fine.

I later found that all that strange behavior is due to a bug in doctrine 2 and it is fixed in 2.0.5.

Well, I tried 2.0.5 and had exactly the same problem, so I thought the mistake is in my code.

I filed a bug in doctrine's jira and I got answered that my definitions are wrong and I need id's for subclasses (and got referred to the documentation that all we know is quite poor, especially for YAML mapping). I gave up and sticked with my hack.

Later I tried with 2.0.6 and 2.1 but with those versions my entities are no longer updated but each time i use generate-entities the new class definitions are being append to the end so there are duplicates.

My question is:

Is this a problem with doctrine or I am doing it wrong?

If it is in me what is the proper way of mapping CI

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

舂唻埖巳落 2024-11-24 21:15:30

摘自你的问题:

更新:
我发现问题实际上是 Doctrine 中的一个错误,它在更新实体时总是在命名空间中添加“\”前缀,而我的自定义自动加载器仅加载没有命名空间的类。此外,还有一个继承属性(ids)的错误

两者都将在 2.1.1 中得到修复

Taken from your question:

Update:
I found that the problem is actually a bug in Doctrine which always prefix the namespace with "\" when updating entities and my custom autoloader loads only classes without namespace. In addition there is a bug with inheriting properties (ids)

Both will be fixed in 2.1.1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文