简体   繁体   English

Doctrine 1.2类表继承的解决方法?

[英]Doctrine 1.2 class table inheritance workaround?


ok, first of all, I know this is not possible with 1.2., that is why I'm looking for a workaround. 好的,首先,我知道1.2。这是不可能的,这就是为什么我正在寻找一种解决方法。
And no, unfortunately I can't use Doctrine 2, because my shared hosting server is stuck at PHP 5.2.16, and the admin refuses to install PHP 5.3 support. 不,不幸的是我不能使用Doctrine 2,因为我的共享托管服务器停留在PHP 5.2.16,并且管理员拒绝安装PHP 5.3支持。

So, here is my problem, basic parent-child issue: 所以,这是我的问题,基本的亲子问题:
Lets say I have a base class User and child classes Customer , Seller , Admin . 假设我有一个基类User和子类CustomerSellerAdmin

I have been looking at the inheritance methods: simple (pretty useless IMO), concrete and column aggregation , and even a workaround for " fake class table inheritance " found here . 我一直在研究继承方法: 简单 (相当无用的IMO), 具体列聚合 ,甚至是在这里找到的“ 假类表继承 ”的解决方法。

What I really need is " class table inheritance ", in other words User maps to separate table, which contains all shared values, and child tables define map to separate tables defining only their specific columns. 我真正需要的是“ 类表继承 ”,换句话说, User映射到包含所有共享值的单独表,子表定义映射到仅定义其特定列的单独表。

I have given up on perfection :) in PHP<5.3, I really don't care how will it be stored in the db, but my object model NEEDS to have all functionality. 我放弃了完美 :)在PHP <5.3中,我真的不在乎它将如何存储在数据库中,但我的对象模型需要具备所有功能。

So what is the best choice for a workaround? 那么解决方法的最佳选择是什么?

This is what I need. 这就是我需要的。

  • Base class definition with shared values (name, username, password, address...) 具有共享值的基类定义(名称,用户名,密码,地址......)
  • Child classes with all the values (shared and their specific) 包含所有值的子类(共享及其特定)
  • Child classes should only see their variables, not from other child classes (for instance, Customer should not have workingHours defined, which is a Seller specific value) 子类只应该看到它们的变量,而不是其他子类(例如,客户不应该定义workingHours ,这是卖方特定的值)
  • I have to be able to form relationships on the base class, and on the separate child classes, and these relationships have to be bind to the specific class (for instance User class has to have a relationship to the Address class, Restourant class has to have a relationship to the Orders class, Restourant also has to have the Address relationship defined... but Admin can't have a Orders relationship, because he has nothing to do with orders) 我必须能够在基类和单独的子类上形成关系,并且这些关系必须绑定到特定的类(例如,User类必须与Address类有关系,Restourant类必须与Orders类有关系,Restourant也必须定义Address关系......但Admin不能有Orders关系,因为他与订单无关)

Querying the base table would also be nice, but as I can see this is one of the biggest problems, it is not a must have. 查询基表也很不错,但正如我所看到的,这是最大的问题之一,它不是必须的。

I hope you understand my question. 我希望你理解我的问题。 Any advice would be appreciated, because I really don't know what to do here. 任何建议都会受到赞赏,因为我真的不知道该怎么做。

I had the very same problem a few days ago, and I chose inherinance by column aggregation. 几天前我遇到了同样的问题,我通过列聚合选择了继承。 The underlying concept is not the cleanest or most beautiful solution ever invented, but does the trick, and meets all your requirements, except one: 潜在的概念并不是有史以来最干净或最美丽的解决方案,而是诀窍,并满足您的所有要求,除了一个:

  • The base class (and table) has all the fields user by the child classes. 基类(和表)具有子类的所有用户字段。
  • The child classes have all the fields they need. 子类具有所需的所有字段。
  • The child classes can access fields belonging to other chields' field. 子类可以访问属于其他chields字段的字段。
  • You can freely create relationships in your Doctrine schema using the parent or the child classes. 您可以使用父类或子类在Doctrine模式中自由创建关系。

The one thing that fails here is that I emphasized with a bold font, but you can easily work it around. 这里失败的一件事是我用粗体字强调,但你可以很容易地解决它。 You can override the setters and getters in your child classes, like that if anyone tries to access or modify a field on a Seller object that belongs to the Customer, then you do not allow it (throw an exception, return false, etc...). 您可以覆盖子类中的setter和getter,如果有人试图访问或修改属于Customer的Seller对象上的字段,那么您不允许它(抛出异常,返回false等等)。 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM