Can someone please derive a concrete example from the following:

有人可以从以下内容中得出一个具体的例子:

http://www.urdalen.com/blog/?p=210

..that shows how to deal with one-to-many and many-to-many relationships?

..这表明如何处理一对多和多对多的关系?

I've emailed the author some time ago but received no reply. I like his idea, but can't figure out how to implement it beyond simple single table relations.

我不久前给作者发了电子邮件,但没有得到回复。我喜欢他的想法,但无法弄清楚如何在简单的单表关系之外实现它。

Note: I don't want to use a full-blown ORM. I like doing my SQL by hand. I would like to improve the design of my app code though. Right now each domain object has its own class full of queries wrapped in static methods. They just return scalar, 1d-array (record) or 2d-array (recordset) depending on the query.

注意:我不想使用完整的ORM。我喜欢手工做我的SQL。我想改进我的应用程序代码的设计。现在,每个域对象都有自己的类,其中包含用静态方法包装的查询。它们只是返回标量,1d数组(记录)或2d数组(记录集),具体取决于查询。

3 个解决方案

#1


6

The problem of ORM's (The impedance mismatch, as it's called) is precisely with relations. In an object graph (In-memory objects), relationships are pointers to other objects. In a relational database, relationships are reversed; This makes it impossible to do a simple mapping between the two models, and that is why ORM's are so complex.

ORM的问题(阻抗不匹配,正如它所称)恰好与关系有关。在对象图(内存中对象)中,关系是指向其他对象的指针。在关系数据库中,关系是相反的;这使得无法在两个模型之间进行简单的映射,这就是ORM如此复杂的原因。

If you want to stay close to the database (avoiding an ORM), then you shouldn't try to abstract relationships away. The way I write datamappers is something like the following:

如果你想要靠近数据库(避免使用ORM),那么你就不应该试图去除关系。我编写datamappers的方式如下:

$car42 = $car_gateway->fetch(42);
$wheels = $wheel_gateway->selectByCar($car42);

In contrast to the ORM way:

与ORM方式相反:

$car42 = $car_gateway->fetch(42);
$wheels = $car42->selectWheels();

This does mean that the gateways end up in your client-code, but it also keeps things very transparent and close to the database.

这意味着网关最终会出现在客户端代码中,但它也会使事情保持透明并且靠近数据库。

更多相关文章

  1. PHP的语言特性-面向对象和C++/java/python的相似之处
  2. php面向对象之抽象类和接口理解
  3. 或者不使用Laravel的有说服力的关系查询
  4. php和apache的关系和作用()
  5. Kohana和ORM的关系has_many_through
  6. 键入提示 - 指定对象数组
  7. PHP 创建对象的两种方法
  8. Python:我如何从datetime.timedelta对象中获取时间?
  9. php如何以一个对象作为数组下标?

随机推荐

  1. 详细介绍XML代码编写的编码与验证问题
  2. XSLT语法—在.net中使用XSLT转换xml文档
  3. XML中的DTD文档类型定义详细介绍
  4. XmlSerializer 对象的Xml序列化和反序列
  5. XML编程中的模式定义XSD示例代码详解
  6. XML中的树形结构与DOM文档对象模型的示例
  7. XPath语法: 在C#中使用XPath示例的具体代
  8. 详解通过XmlDocument读写Xml文档的示例代
  9. 详细介绍使用XmlWriter写Xml的示例代码
  10. XML—尝试对一个XML文档进行增删查改编程