Zend_Db_Table_Abstract 加载连接模型

发布于 2025-01-02 16:07:52 字数 2029 浏览 0 评论 0原文

的表

我有一个名为: client(id,别名) 帖子(ID、主题) post_client (id, post_id, client_id)

许多客户端可以加入到一个帖子中。

使用 Zend DB Table 抽象我开始构建一个模型,以下是类:

ORM_Post

class ORM_Post extends Zend_Db_Table_Abstract {

    protected $_name = 'Post';
    protected $_dependentTables = array('ORM_Post_Client');

}

ORM_Client

class ORM_Client extends Zend_Db_Table_Abstract {


    protected $_name = 'Client';
    protected $_dependentTables = array(
        'ORM_Post_Client'
    );
}

ORM_Post_Client

class ORM_Post_Client extends Zend_Db_Table_Abstract {

    protected $_name = 'Post_Client';
    protected $_referenceMap    = array(
        'post' => array(
            'columns'           => 'post_id',
            'refTableClass'     => 'ORM_Post',
            'refColumns'        => 'id'
        ),
        'client' => array(
            'columns'           => 'client_id',
            'refTableClass'     => 'ORM_Post_Client',
            'refColumns'        => 'id'
        )

    );
}

我希望做的是调用 Post 的实例,然后加载关联的客户端以及加载客户端的实例并加载所有相关帖子。

所以我这样做了:

    $post = new ORM_Post();
    $results = $post->fetchAll();

    foreach ($results as $key => $result){
        $row = $results->current();
        $client = $row->findDependentRowset('ORM_Post_Client','client');
    }

我得到了 引用规则“client”不引用表 ORM_Post

我已经为此奋斗了几个小时,但看不出哪里出了问题。我是否还要在客户端和帖子模型中声明 Post_Client 连接?

编辑

这就是我想要的:

    $post = new ORM_Post();
$results = $post->fetchAll();
$return = array();

    foreach ($results as $result){
        $row = $post->find($result->id)->current();
        $return[$result->id] = $row->toArray();
        $return[$result->id]['clients'] = $row->findManyToManyRowset('ORM_Client', 'ORM_Post_Client')->toArray();
}

return $return;

谢谢你们的建议,你们让我走上了正确的道路

I have a tables named:

client (id, alias)
post (id, subject)
post_client (id, post_id, client_id)

Many clients can be joined to a post.

Using Zend DB Table abstract I have started to build a model, here are the classes:

ORM_Post

class ORM_Post extends Zend_Db_Table_Abstract {

    protected $_name = 'Post';
    protected $_dependentTables = array('ORM_Post_Client');

}

ORM_Client

class ORM_Client extends Zend_Db_Table_Abstract {


    protected $_name = 'Client';
    protected $_dependentTables = array(
        'ORM_Post_Client'
    );
}

ORM_Post_Client

class ORM_Post_Client extends Zend_Db_Table_Abstract {

    protected $_name = 'Post_Client';
    protected $_referenceMap    = array(
        'post' => array(
            'columns'           => 'post_id',
            'refTableClass'     => 'ORM_Post',
            'refColumns'        => 'id'
        ),
        'client' => array(
            'columns'           => 'client_id',
            'refTableClass'     => 'ORM_Post_Client',
            'refColumns'        => 'id'
        )

    );
}

What I was hoping todo is call an instance of the Post and then load the clients associated aswell as loading an instance of the client and load all posts associated.

So I did this:

    $post = new ORM_Post();
    $results = $post->fetchAll();

    foreach ($results as $key => $result){
        $row = $results->current();
        $client = $row->findDependentRowset('ORM_Post_Client','client');
    }

and I get
Reference rule "client" does not reference table ORM_Post

I have battled with this for hours and cannot see where I'm going wrong. Am I to declare the Post_Client joins inside the client and post model also?

EDIT

Here is what I was after:

    $post = new ORM_Post();
$results = $post->fetchAll();
$return = array();

    foreach ($results as $result){
        $row = $post->find($result->id)->current();
        $return[$result->id] = $row->toArray();
        $return[$result->id]['clients'] = $row->findManyToManyRowset('ORM_Client', 'ORM_Post_Client')->toArray();
}

return $return;

Thanks for the advice guys, you put me on the right track

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

浮云落日 2025-01-09 16:07:52

在你的 ORM_Post_Client 中它应该是

'client' => array(
            'columns'           => 'client_id',
            'refTableClass'     => 'ORM_Client',  //instead of ORM_Post_Client 
            'refColumns'        => 'id'
        )

refTableClass =>;父表的类名。使用类
名称,而不是 SQL 表的物理名称(文档)

我也认为你的循环应该是:

foreach ($results as $result){
        $row = $results->current();
        $clients = $row->findDependentRowset('ORM_Post_Client','post'); 
  }

因为你正在寻找一个帖子的客户,这意味着该帖子是你的规则

$row->findDependentRowset($table, [$rule]); )

in your ORM_Post_Client it should be

'client' => array(
            'columns'           => 'client_id',
            'refTableClass'     => 'ORM_Client',  //instead of ORM_Post_Client 
            'refColumns'        => 'id'
        )

refTableClass => The class name of the parent table. Use the class
name, not the physical name of the SQL table (documentation)

also i think your loop should be :

foreach ($results as $result){
        $row = $results->current();
        $clients = $row->findDependentRowset('ORM_Post_Client','post'); 
  }

because you are looking for clients of a post which means that post is your rule

($row->findDependentRowset($table, [$rule]); )

感情旳空白 2025-01-09 16:07:52

所提出的这是行不通的,老实说这没有任何意义。

$post = new ORM_Post();
    $results = $post->fetchAll();

    foreach ($results as $key => $result){
        //$row is assigned to the whole fetchall result!
        $row = $results->current();
        //in this context $client cannot call a dependent rowset.
        $client = $row->findDependentRowset('ORM_Post_Client','client');
    }

MMc 是正确的,因为您引用的表定义不正确,但是您的代码也存在一些问题。也许尝试这样的事情:

 $post = new ORM_Post();

    $results = $post->fetchAll();

    //unless your are going to use the 'key' for something you don't need it
    foreach ($results as $result){

        //you need each row object in order to call findDependentRowset in a one to many relationship.
        $row = $post->find($result->id)->current();
        //unless you have multiple rules set up for each table class pair you don't need to specify the rule.
        $client = $row->findDependentRowset('ORM_Post_Client');
    }

This as presented won't work, honestly it makes no sense.

$post = new ORM_Post();
    $results = $post->fetchAll();

    foreach ($results as $key => $result){
        //$row is assigned to the whole fetchall result!
        $row = $results->current();
        //in this context $client cannot call a dependent rowset.
        $client = $row->findDependentRowset('ORM_Post_Client','client');
    }

MMc is correct in that you reference table definition was incorrect however your code has some issues as well. Maybe try something like:

 $post = new ORM_Post();

    $results = $post->fetchAll();

    //unless your are going to use the 'key' for something you don't need it
    foreach ($results as $result){

        //you need each row object in order to call findDependentRowset in a one to many relationship.
        $row = $post->find($result->id)->current();
        //unless you have multiple rules set up for each table class pair you don't need to specify the rule.
        $client = $row->findDependentRowset('ORM_Post_Client');
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文