Zend_Db_Table_Row:为什么我必须使用 createRow()?
我想知道为什么在 Zend_Db 中必须使用 createRow 方法来获取新的空 Row 对象,而不是直接实例化 Row 对象。
例如,如果我扩展了默认类来创建我自己的用户表和行类,那么为了创建一个新的行对象,我总是必须执行以下操作来创建一个新的行对象:
$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 Framework 团队是否有特定原因没有这样做只是将此设置为默认值?
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.
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();
But to me it makes more sense to be able to just say:
$userRow = new App_Row_User();
$userRow->setName('bob');
$userRow->save();
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
new
直接实例化您的Row类。但 Row 对象需要知道哪些列对该行数据有效。
将行的类名映射到其对应的表类名并不神奇。所以 Row 本身并不知道它属于哪个表。因此它无法猜测如何编写SQL来执行插入或更新操作;它从相应的 Zend_Db_Table 对象获取此元数据。您必须告诉行要使用哪个表。
如果使用 new 构造行对象,则必须将配置数组传递给构造函数,其中包括表类名称或该表类的对象实例。
或者
如果没有有效的表对象或类名,使用 new 实例化 Row 对象将引发异常。
同样,
save()
使用表对象,调用方法insert()
或update()
。如果没有有效的表对象,Row 的save()
方法将引发异常。You can instantiate your Row class directly with
new
.But the Row object needs to know what columns are valid for that row of data.
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.
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.Or
Without a valid table object or class name, instantiating a Row object with
new
will throw an exception.Likewise,
save()
uses the table object, calling methodsinsert()
orupdate()
. Without a valid table object, the Row'ssave()
method will throw an exception.