Zend_Db_Table_Row:为什么我必须使用 createRow()? [英] Zend_Db_Table_Row: Why do I have to use createRow()?

查看:22
本文介绍了Zend_Db_Table_Row:为什么我必须使用 createRow()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么在 Zend_Db 中你必须使用 createRow 方法来获取一个新的、空的 Row 对象,而不是直接实例化 Row 对象.

I'm wondering why in Zend_Db you have to use the createRow method to get a new, empty Row object, rather than just instantiating the Row object directly.

例如,如果我扩展了默认类来创建我自己的 User 表和行类,那么为了创建一个新的行对象,我总是必须执行以下操作来创建一个新的 Row 对象:

For instance, if I've extended the default classes to create my own User table and row classes, then in order to create a new row object I always have to do the following to create a new Row object:

$userTable = new App_Table_User();
$userRow = $userTable->createRow();
$userRow->setName('bob');
$userRow->save();

但对我来说,只说:

$userRow = new App_Row_User();
$userRow->setName('bob');
$userRow->save();

我知道我总是可以将此功能构建到扩展 Zend_Db_Table_Row_Abstract 的 Row 类中,但我想知道 Zend 框架团队不只是将其设为默认值是否有特定原因?

I understand that I can always build this functionality into my Row classes that extend Zend_Db_Table_Row_Abstract, but I'm wondering if there's a specific reason that the Zend Framework team didn't just make this the default?

推荐答案

可以直接使用 new 实例化您的 Row 类.

You can instantiate your Row class directly with new.

但是 Row 对象需要知道哪些列对于该行数据有效.

But the Row object needs to know what columns are valid for that row of data.

将行的类名映射到其对应的表类名并没有什么神奇之处.所以 Row 本身并不知道它属于哪个表.因此它无法猜测如何编写 SQL 来执行插入或更新操作;它从相应的 Zend_Db_Table 对象中获取元数据.您必须告诉 Row 使用哪个 Table.

There is no magic to map a row's class name to its corresponding table class name. So the Row doesn't know by itself what table it belongs to. Therefore it can't guess how to write SQL to perform insert or update operations; it gets this metadata from the corresponding Zend_Db_Table object. You have to tell the Row which Table to use.

如果使用 new 构造行对象,则必须将配置数组传递给构造函数,包括表类名称或该表类的对象实例.

If you construct a row object with new, you must pass a config array to the constructor including either the table class name, or else an object instance of that table class.

$userRow = new App_Row_User( array('table' => 'App_Table_User') );

$userTable = new App_Table_User();
$userRow = new App_Row_User( array('table' => $userTable) );

如果没有有效的表对象或类名,使用 new 实例化 Row 对象将引发异常.

Without a valid table object or class name, instantiating a Row object with new will throw an exception.

同样,save() 使用 table 对象,调用方法 insert()update().如果没有有效的表对象,Row 的 save() 方法将抛出异常.

Likewise, save() uses the table object, calling methods insert() or update(). Without a valid table object, the Row's save() method will throw an exception.

这篇关于Zend_Db_Table_Row:为什么我必须使用 createRow()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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