PHP ORM 具有完整的复合主/外键支持
我正在寻找 PHP5 ORM,它完全支持基于复合主键和外键的复合(多列)关系。
我希望教义 2 能够解决这个问题,但事实并非如此。这是关系数据建模中的一个基本功能,但据我所知,没有一个 PHP ORM 软件支持它。
我最近发现 SQLAlchemy 具有完全支持,但我需要 PHP 的东西,而不是 Python。
I'm looking for PHP5 ORM which fully supports composite (multi-column) relations based on composite primary keys and foreign keys.
I hoped that Doctrine 2 would solve this problem but it doesn't. It's a basic feature in relational data modelling but none of PHP ORM software I know supports it.
I've recently found that SQLAlchemy has full support but I need something for PHP, not Python.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您从数据库角度处理应用程序时,诸如 Doctrine2 (PHP) 或 Hibernate (Java) 之类的“派生”应用程序是不合适的。当您想要相反时(即“我有一个 OO 域模型并且需要将其保存在关系数据库中”,而不是“我有一个关系数据库并且想要一个(生成的?)OO 接口”),这些更适合为此,无需在数据库方面做出任何妥协”)。如果尽管这些方法存在重要差异,但您仍然使用它们,并且您更喜欢关系模式而不是对象模型,那么您只会感到沮丧。
区分不同类别的 ORM 工具非常重要,因为它们往往侧重于不同的开发模型。
也许可以尝试 Propel 或 RedBeanPHP,它们似乎适合数据库驱动的开发模型,几乎没有进行映射,只是简单生成(通常是愚蠢的)“数据对象”,没有太多抽象。外键字段只是直接放入这些解决方案等中的对象中,因此它们可能“支持”您想要的所有复合内容。
像 Doctrine2 或 Hibernate 这样的解决方案不鼓励使用复合键,因此只支持它们到一定程度(Hibernate 已经走得很远,但仍然不建议使用它们)。
就我个人而言,我不喜欢复合键(除了没有附加数据的纯多对多链接表),因为我倾向于从应用程序/域模型/对象方面更多地处理事情,并且有复合键即使底层映射技术支持它们,映射到对象的数据通常使用起来很痛苦。代理键(或至少是单字段自然键)使事情变得更加简单。我不是一个关系数据库规范化迷恋者,从我的角度来看,“更好的性能”是非常值得怀疑的。如果系统中存在真正的性能问题,保存偶尔的连接并不能拯救您。
When you're approaching the application from the database perspective something like Doctrine2 (PHP) or Hibernate (Java) from which it is "derived", is not appropriate. These are much more suited when you want to go the other way around (i.e. "I have an OO domain model and need to persist it in a relational db", not "I have a relational db and want a (generated?) OO interface for it, without making any compromises whatsoever on the database side"). If you use them despite this important differences in the approaches and you love your relational schema more than your object model you will just get frustrated.
It is really important to differentiate between different categories of ORM tools as they tend to focus on different development models.
Maybe try out Propel or RedBeanPHP, which seem suited for a database-driven development model with almost no mapping going on, just plain generated (and usually dumb) "data objects" and not much abstraction. Foreign key fields are just put into objects directly in these solutions, etc. so they might "support" all the composite stuff you want.
Solutions like Doctrine2 or Hibernate discourage use of composite keys and hence only support them up to a certain level (Hibernate goes pretty far but nevertheless they are not recommended to use).
Personally I'm not a fan of composite keys (with the exception of pure many-to-many link tables with no additional data) because I tend to approach things much more from the application/domain-model/object side and there composite keys that are mapped to objects are often a pain to work with, even if the underlying mapping technology supports them. Surrogate keys (or at least single-field natural keys) make things much simpler. I'm not a relational-database-normalization-fetishist and the "better performance" is highly questionable from my point of view. Saving an occasional join won't save you if you have real performance problems in a system.
教义2.1彻底解决了这个问题。
Doctrine 2.1 solves this problem completely.
来自Doctrine2参考:
From Doctrine2 reference:
您可以尝试 LEAP ORM,它是用 PHP 5 编写的。它可以在 github 上找到:https://github.com/spadefoot/kohana-orm-leap。
Leap ORM 完全支持复合键,包括主键和外键。同样,它适用于非整数主/外键。在 ORM 模型中,您可以创建字段别名、字段适配器和关系。
尽管它是为 Kohana PHP 框架 编写的,但您只需添加一个简单的自动加载即可轻松使其与任何 PHP 框架一起使用函数给你的代码。 Leap 适用于以下数据库:DB2、Drizzle、Firebird、MariaDB、MS SQL、MySQL、Oracle、PostgreSQL 和 SQLite。它还提供查询生成器和数据库连接池。
在 ORM 的网站上,有很多很好的示例和教程来帮助您了解如何使用它。
You can try the LEAP ORM, which is written in PHP 5. It is available on github at https://github.com/spadefoot/kohana-orm-leap.
The Leap ORM fully supports composite keys, both for primary keys and foreign keys. Likewise, it works with non-integer primary/foreign keys. Within the ORM models, you can create field alias, field adapters, and relations.
Although it is written for the Kohana PHP Framework, you can easily make it work with any PHP framework by just adding a simple autoload function to you code. Leap works with the following databases: DB2, Drizzle, Firebird, MariaDB, MS SQL, MySQL, Oracle, PostgreSQL, and SQLite. It also provides both a query builder and a database connection pool.
On the ORM's Website, there are a lot of good examples and tutorials to help you understand how to use it.