活动记录的目的是什么? [英] What is the purpose of Active Records?

查看:577
本文介绍了活动记录的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在修改CodeIgniter,第一次遇到Active Records。起初,我把它当作一些不真正知道如何写SQL的人。我现在意识到我的分析有缺陷,活动记录相当突出,特别是在Rails。



但是活动记录的目的是什么?它是抽象远离不同的RDBMS个性。如果是这样,我认为这不是SQL是什么意思。

解决方案



活动记录模式正在成为大多数编程框架的核心部分。它使得更简单的CRUD(创建,更新,读取,删除)任务更快地实现。例如,而不是必须写入许多SQL来插入,更新和删除许多常见和简单的数据对象,它允许您简单地将值分配给数据对象并运行命令。



大多数框架还在其各自的Active Record模型中实现数据关系,这可以大大简化对数据的访问与您的对象相关。例如,在CodeIgniter中,如果您指定了一个类别有多个产品,那么在从数据库加载类别对象后,您可以用一行简单的代码列出它的子产品。

  foreach($ category-> products as $ product){
echo $ product-> name;
}

Active Record的另一个好处是,正如你所说,轻松地移植到不同的数据库平台(只要你使用的框架有一个驱动程序为您选择的数据库),虽然这似乎不太可能似乎重要的现在,它我以后有更大的价值,如果你的应用程序变得流行!



希望这会有帮助。维基百科描述了活动记录( http://en.wikipedia.org/wiki/Active_record_pattern )和CodeIgniter文档也将。就个人而言,我使用KohanaPHP( http://www.kohanaphp.com ),这是一个仅PHP5的CodeIgniter分支,我发现它的ORM模型非常有用!


I'm tinkering with CodeIgniter and have come across Active Records for the first time. At first I dismissed it as something for people who don't really know how to write SQL. I realise now that my analysis was flawed and Active Records are pretty prominent, especially in Rails.

But what purpose do Active Records hold? Is it to abstract away from different RDBMS individualities. If so I thought that isn't that what SQL is meant to do. Furthermore what is best practice, should I be using these?

Thanks in advance

解决方案

The "Active Record Pattern" is becoming a core part of most programming frameworks. It makes simpler CRUD (Create, Update, Read, Delete) tasks quicker to achieve. For example rather than having to write many SQLs to Insert, Update and Delete many common and simple data objects, it allows you to simply assign the values to the data object and run a command e.g. $object->save(), the SQL is compiled and executed for you.

Most frameworks also implement data relationships within their respective Active Record models which can greatly simplify accessing data related to your object. For example, in CodeIgniter, if you specified that a Category "has many" Products then after loading the Category object from the database, you can list it's child Products with a simple line of code.

foreach ($category->products as $product) {
  echo $product->name;
}

Another benefit of Active Record is, as you say, that it makes your code easily portable to different database platforms (so long as the framework that you are using has a driver for your chosen database) and although this is not likely to seem important right now, it my have greater value at a later date if your application becomes popular!

Hopefully this will have helped. Wikipedia describes Active Record well (http://en.wikipedia.org/wiki/Active_record_pattern) and the CodeIgniter docs will aswell. Personally, I use KohanaPHP (http://www.kohanaphp.com) which is a PHP5 only fork of CodeIgniter and I find that it's ORM models are very useful!

这篇关于活动记录的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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