简体   繁体   English

在PHP中使用PDO时,ORM如何工作?

[英]How does ORM work when using PDO in PHP?

I've heard that PDO can do some ORM. 我听说PDO可以执行一些ORM。 Is it a good idea to stick with that or should I consider some alternatives that use PDO to do ORM? 坚持这样做是一个好主意,还是我应该考虑一些使用 PDO进行ORM的替代方案?

In short, what I want to do: I want to automatically map table fields to instance variables. 简而言之,我想做什么:我想自动将表字段映射到实例变量。 Getters and Setters are created manually. 获取器和设置器是手动创建的。 However, I could also think of making an associative array as ivar and then just assign the record set to that array. 但是,我也可以考虑将关联数组设置为ivar,然后将记录集分配给该数组。 The getters/setters then just access that array with the appropriate key. 然后,获取者/设置者只需使用适当的密钥访问该数组即可。 But I wonder how PDO could help here to get that done easier? 但是我想知道PDO如何在这里帮助您更轻松地完成这项工作?

next to the suggestions made by Ahmed, you can also consider doctrine or the php active record implementation: 在艾哈迈德(Ahmed)提出的建议旁边,您还可以考虑使用教义或php活动记录实现:

doctrine: http://www.doctrine-project.org/ 学说: http : //www.doctrine-project.org/

active record: 活动记录:

I usually work with propel or active record (as part of cakePHP). 我通常使用propel或active record(作为cakePHP的一部分)。 Active record supports your associative array goal. 活动记录支持您的关联数组目标。

For Propel you can achive the same by making all (required) classes extend a certain uber class. 对于Propel,您可以通过使所有(必需)类扩展某个特定的uber类来达到相同的目的。 This is defined in your database schema and then generated into all ORM classes. 这是在您的数据库模式中定义的,然后生成到所有ORM类中。 In your uber class you can make the constructor accept associative arrays... So if this is you main objective use active record or a php framework that implements active record. 在您的uber类中,可以使构造函数接受关联数组...因此,如果这是您的主要目标,请使用活动记录或实现活动记录的php框架。

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

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