Zend Framework 模型关系和访问相关记录

发布于 2024-10-18 05:05:32 字数 1386 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓 2024-10-25 05:05:32

你的课程是混合的。每个实体都应该有 2 个类......一个 EntityTable (您的表网关)和一个 Entity (您的行网关)。因此类声明应该类似于:

class User extends Zend_Db_Table_Row

class FbUser extends User

class UserTable extends Zend_Db_Table_Abstract

class UserItem extends Zend_Db_Table_Row

class UserItemTable extends Zend_Db_Table_Abstract

行类是您的模型(或者根据您想要的连接方式链接到模型),而不是表类。

findDependentRowset 方法位于 Zend_Db_Table_Row 类上,这就是您收到错误的原因...您以某种方式扩展了不正确的类。

在某种程度上,我的意思是您的表定义是正确的,但您正在尝试使用像行这样的表实例。您可以按照上面的建议添加/更改类用法,也可以将 user 的 wor 实例传递给表类作为刷新参数:

public function refresh(Zend_Db_Table_Row $user)
{
   $items = $user->findDependentRowset('UserItem', 'items');
   // do stuff with each item
   print_r($items);
   die();
}

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:

class User extends Zend_Db_Table_Row

class FbUser extends User

class UserTable extends Zend_Db_Table_Abstract

class UserItem extends Zend_Db_Table_Row

class UserItemTable extends Zend_Db_Table_Abstract

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:

public function refresh(Zend_Db_Table_Row $user)
{
   $items = $user->findDependentRowset('UserItem', 'items');
   // do stuff with each item
   print_r($items);
   die();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文