在 Zend_Db_Table_Row 中使用关系进行设置
有没有办法使用 Zend_Db 关系来设置相关对象? 我正在寻找类似以下代码的内容:
$contentModel = new Content();
$categoryModel = new Category();
$category = $categoryModel->createRow();
$category->setName('Name Category 4');
$content = $contentModel->createRow();
$content->setTitle('Title 4');
$content->setCategory($category);
$content->save();
这提供了小型库: http://code.google.com/p/zend-framework-orm/< /a>
有人有这方面的经验吗? ZF没有类似的计划吗? 或者有什么更好用的吗? (我不想使用 ORM 或外部的东西)
谢谢
is there a way how to use Zend_Db relations for setting related objects?
I am looking for something like following code:
$contentModel = new Content();
$categoryModel = new Category();
$category = $categoryModel->createRow();
$category->setName('Name Category 4');
$content = $contentModel->createRow();
$content->setTitle('Title 4');
$content->setCategory($category);
$content->save();
this provides small library:
http://code.google.com/p/zend-framework-orm/
does somebody have experience with that? Isn't there a plan for something similar in ZF ? Or is there something better for use? (I don't wnat to use doctrine ORM or something external)
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在 Zend Framework 中设计并实现了表关系代码。
外键(示例中的
$content->category
)包含它引用的父行中主键的值。 在您的示例中,$category
尚不包含主键值,因为您尚未保存它(假设它使用自动递增伪键)。 在填充外键之前,您无法保存$content
行,因此满足引用完整性:将 Row 对象传递给
setCategory()
没有任何好处如果没有填充主键。 如果没有有效的主键值可供引用,$content->save()
将失败。由于在任何情况下都需要填充该主键字段,因此在调用
setCategory()
时访问该字段并不困难。I designed and implemented the table-relationships code in Zend Framework.
A foreign key (
$content->category
in your example) contains the value of the the primary key in the parent row it references. In your example, the$category
doesn't contain a primary key value yet because you haven't saved it (assuming it uses an auto-incrementing pseudokey). You can't save the$content
row until you populate its foreign key, so referential integrity is satisfied:It would do no good to pass the Row object to
setCategory()
if it doesn't have the primary key populated.$content->save()
will fail if it doesn't have a valid primary key value to reference.Since you need that primary key field to be populated in any case, it's not so difficult to access the field when you call
setCategory()
.我总是重写 Zend_Db_Table 和 Zend_Db_Table_Row 并使用我自己的子类。 在我的 Db_Table 类中,我有:
在我的 Db_Table_Row 中,我有以下 __get() 和 __set() 函数:
基本上,这只是告诉类查找名为 getFoo() 和 setFoo() 或其他的方法。 只要你在后面编写自己的逻辑,你几乎就可以组成自己的字段。 在你的情况下也许:
I always override Zend_Db_Table and Zend_Db_Table_Row and use my own subclasses. In my Db_Table class I have:
In my Db_Table_Row I have the following __get() and __set() functions:
Bascially that just tells the class to look for methods called getFoo() and setFoo() or whatever. You could then pretty much make up your own fields as long as your write your own logic behind. In you case maybe: