开发者

PHP ORM with full composite primary/foreign key support

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 somet开发者_如何学Pythonhing for PHP, not Python.


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.


Doctrine 2.1 solves this problem completely.


From Doctrine2 reference:

Doctrine 2 allows to use composite primary keys. There are however some restrictions opposed to using a single identifier. The use of the @GeneratedValue annotation is only supported for simple (not composite) primary keys, which means you can only use composite keys if you generate the primary key values yourself before calling EntityManager#persist() on the entity.

To designate a composite primary key / identifier, simply put the @Id marker annotation on all fields that make up the primary key.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜