gpt4 book ai didi

zend-framework - Zend_Db_Table_Abstract 加载连接模型

转载 作者:行者123 更新时间:2023-12-04 05:59:06 26 4
gpt4 key购买 nike

我有一个名为:

客户(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_客户端
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;

谢谢你们的建议,你们让我走上了正轨

最佳答案

在您的 ORM_Post_Client它应该是

'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)



我也认为你的循环应该是:
foreach ($results as $result){
$row = $results->current();
$clients = $row->findDependentRowset('ORM_Post_Client','post');
}

因为您正在寻找帖子的客户,这意味着该帖子是您的 rule
( $row->findDependentRowset($table, [$rule]); )

关于zend-framework - Zend_Db_Table_Abstract 加载连接模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9135015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com