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

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

问题描述

我摆弄codeIgniter和所遇到的活动记录的第一次。起初,我被解雇是因为东西的人谁真的不知道该怎么写SQL语句。我现在,我的分析是有缺陷的认识,并主动记录是pretty的突出,尤其是在Rails的。

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.

但是,什么样的目的做的活动记录持有?它是抽象掉不同的RDBMS个性。如果是的话我认为这不正是SQL的意思做。此外什么是最好的做法,我应该用这些?

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?

在此先感谢

推荐答案

活动记录模式正在成为大多数编程框架的核心部分。它使简单的CRUD(创建,更新,读取,删除)任务,更快地实现。例如,而不是写了许多sql语句插入,更新和删除许多常见和简单的数据对象,它可以让你简单地将值赋给数据对象和运行一个命令,例如$对象 - >保存()时,SQL编译并执行你。

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.

大多数框架还各自的活动记录模式,可以大大简化访问关系到你的对象数据中实现数据的关系。例如,在codeIgniter,如果你指定一个种类有很多产品,然后从数据库加载类对象之后,你可以列出其子产品与code简单的一行。

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;
}

活动记录的另一个好处是,正如你所说的,它使你的code很容易地移植到不同的数据库平台(只要您使用了您所选择的数据库驱动程序的框架),虽然这是不太可能,现在看来很重要,但我必须在日后更大的价值,如果你的应用程序变得流行!

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!

希望这将有助于。维基百科介绍活动记录以及( http://en.wikipedia.org/wiki/Active%5Frecord %5Fpattern )和codeIgniter文档会以及。就个人而言,我用KohanaPHP( http://www.kohanaphp.com ),这是$ C的唯一叉PHP5 $ cIgniter,我发现它的ORM的模型是非常有用的!

Hopefully this will have helped. Wikipedia describes Active Record well (http://en.wikipedia.org/wiki/Active%5Frecord%5Fpattern) 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天全站免登陆