在幕后:ORM如何“想”? [英] Behind the scenes: How does an ORM "think"?

查看:149
本文介绍了在幕后:ORM如何“想”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Rails ActiveRecord,PHP的Doctrine(和类似的ORM)中的一些设计感兴趣。




  • ORM设法完成诸如链接访问器的功能,以及他们通常期望工作的深度?

  • ORM构造如何在内部查询

  • ORM管理查询,同时维持所有这些查询的任意性质?



显然这是一个学术问题,但所有欢迎回答的性质!



(我的选择语言是OO PHP5.3!)

解决方案

链接方法调用与ORM问题正交,它们在OOP的所有位置都被使用。一个可链接的方法只需返回一个对当前对象的引用,允许调用返回值。在PHP中

  class A {
public function b(){
...
返回$ this
}

public function c($ param){
...
return $ this;
}
}


$ foo = new A();
$ foo-> b() - > c('one');
// chaining is equivilant to
// $ foo = $ foo-> b();
// $ foo = $ foo-> c();

至于如何构建查询,有两种方法。在ActiveRecord像ORMs中有代码来检查数据库的元数据。大多数数据库都有一些类似SQL或SQL的命令来查看这个元数据。 (MySQL的 DESCRIBE TABLE ,Oracle的 USER_TAB_COLUMNS 表等)



你有一些ORM用中性语言描述你的数据库表,例如YAML。其他人可能会从您创建对象模型的方式推断数据库结构(我想说Django这样做,但是从我看过它已经有一段时间了)。最后还有一种混合方法,其中使用了前两种技术之一,但是提供了一个单独的工具来自动生成YAML / etc。或类文件。



一个表的名称和数据类型是已知的,务实地编写一个返回所有行的SQL查询或一个特定的



对于您最后一个问题,


ORM如何管理查询,而
可以维持所有
的任意性质?




我认为答案是不是很好。一旦您超越了单表,单一对象的隐喻,每个ORM都有一种不同的方法,关于如何使用SQL查询来建模对象。在抽象中,它只是添加基于ORM的假设构建查询的新方法(即Zend_Db_Table的findManyToManyRowset方法)


I'm interested in some of the design behind Rails ActiveRecord, Doctrine for PHP (and similar ORMs).

  • How does an ORM manage to accomplish features like chained accessors and how deep are they typically expected to work?
  • How does an ORM construct queries internally?
  • How does an ORM manage the queries while sustaining the arbitrary nature of all that is expected of it?

Obviously this is an academic question, but all natures of answers are welcome!

(My language of choice is OO PHP5.3!)

解决方案

Chained method calls are orthogonal to the ORM question, they're used all over the place in OOP. A chain-able method simply returns a reference to the current object, allowing the return value to be called. In PHP

class A {
    public function b() {
        ...
        return $this;
    }

    public function c($param) {
        ...
        return $this;
    }       
}


$foo = new A();
$foo->b()->c('one');
// chaining is equivilant to
// $foo = $foo->b();
// $foo = $foo->c();

As for how queries are constructed, there are two methods. In ActiveRecord like ORMs there's code that examines the Database's meta-data. Most databases has some kind of SQL or SQL like commands to view this meta-data. (MySQL's DESCRIBE TABLE, Oracle's USER_TAB_COLUMNS table, etc.)

Some ORMs have you describe your database tables in a neutral language such as YAML. Others might infer a database structure from the way you've created your Object models (I want to say Django does this, but it's been a while since I looked at it). Finally there's a hybrid approach, where either of the previous two techniques are used, but a separate tool is provided to automatically generate the YAML/etc. or class files.

One the names and data-types of a table are known, it's pretty easy to pragmatically write a SQL query that returns all the rows, or a specific set of rows that meet a certain criteria.

As for your last question,

How does an ORM manage the queries while sustaining the arbitrary nature of all that is expected of it?

I'd argue the answer is "not very well". Once you move beyond the one-table, one-object metaphor, each ORM has a different approach an philosophy as to how SQL queries should be used to model objects. In the abstract though, it's just as simple as adding new methods that construct queries based on the assumptions of the ORM (i.e. Zend_Db_Table's "findManyToManyRowset" method)

这篇关于在幕后:ORM如何“想”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆