Zend Framework 模型关系和访问相关记录
我有一个 zend 框架模型:
class User extends Zend_Db_Table_Abstract {
protected $_name = 'users';
protected $_primary = 'id';
protected $_dependentTables = array('UserItem');
public function refresh($) {
$items = $this->findDependentRowset('UserItem', 'items');
// do stuff with each item
print_r($items);
die();
}
}
我也有相关模型:
<?php
class UserItem extends Zend_Db_Table_Abstract
{
protected $_name = 'user_items';
protected $_referenceMap = array(
'items' => array(
// user_id is the name of the field on the USER_ITEMS table
'columns' => 'user_id',
'refTableClass' => 'User',
// id is the name of the field on the USERS table
'refColumns' => 'id'
)
);
}
?>
我希望能够调用 User->refresh();
并发生一些奇特的小事情。但错误是,
Fatal error: Call to undefined method FbUser::findDependentRowset()
这告诉我,虽然我认为我根据 Zend 文档做的是正确的http://framework.zend.com/manual/en/zend.db.table.relationships.html 我错过了一些东西。
如果有什么不同,第一次运行时项目列表将为空,然后我将“更新插入”一大堆项目 - 将来的运行我将比较所有项目,仅更新不同的项目。嗯...不,这绝对不相关:)
I've got a zend framework model:
class User extends Zend_Db_Table_Abstract {
protected $_name = 'users';
protected $_primary = 'id';
protected $_dependentTables = array('UserItem');
public function refresh($) {
$items = $this->findDependentRowset('UserItem', 'items');
// do stuff with each item
print_r($items);
die();
}
}
I've also got the related model:
<?php
class UserItem extends Zend_Db_Table_Abstract
{
protected $_name = 'user_items';
protected $_referenceMap = array(
'items' => array(
// user_id is the name of the field on the USER_ITEMS table
'columns' => 'user_id',
'refTableClass' => 'User',
// id is the name of the field on the USERS table
'refColumns' => 'id'
)
);
}
?>
I'd like to me able to call User->refresh();
and have a fancy little stack of things happen. But the error is
Fatal error: Call to undefined method FbUser::findDependentRowset()
Which is telling me that although I think i'm doing it right according to the Zend documentation http://framework.zend.com/manual/en/zend.db.table.relationships.html I'm missing something.
If it makes a difference, at first run the items list will be empty, then I'll "Upsert" a whole bunch of items - future runs I'll compare all items an only update the ones that are different. Hmm... nope that's definitely not relevant :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的课程是混合的。每个实体都应该有 2 个类......一个 EntityTable (您的表网关)和一个 Entity (您的行网关)。因此类声明应该类似于:
行类是您的模型(或者根据您想要的连接方式链接到模型),而不是表类。
findDependentRowset 方法位于 Zend_Db_Table_Row 类上,这就是您收到错误的原因...您以某种方式扩展了不正确的类。
在某种程度上,我的意思是您的表定义是正确的,但您正在尝试使用像行这样的表实例。您可以按照上面的建议添加/更改类用法,也可以将 user 的 wor 实例传递给表类作为刷新参数:
You have your classses mixed. You should have 2 classes for every entity... a EntityTable (your table gateway) and an Entity (your row gateway). so the class declarations should look something like:
The row classes are your models (or are linked to models depending on how you want to wire it up), not the table classes.
The
findDependentRowset
method is on the Zend_Db_Table_Row class which is why you are getting the error... you have extended the incorrect class in a way.By in a way, i mean that your table definitions are correct, but you are trying to use the table instances like rows. You can either add/change class usage as suggested above or you can pass the wor instance of user to the table class as an arg ument to refresh: