我有一个名为:
的表客户端(id,别名) 帖子(id,subject) post_client(id,post_id,client_id)
许多客户都可以加入帖子。
使用Zend DB表摘要我已经开始构建一个模型,这里是类:
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'
)
);
}
我希望todo调用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;
感谢你们的建议,你们让我走上了正确的道路
答案 0 :(得分:3)
在你的ORM_Post_Client
中应该是
'client' => array(
'columns' => 'client_id',
'refTableClass' => 'ORM_Client', //instead of ORM_Post_Client
'refColumns' => 'id'
)
refTableClass =>父表的类名。使用该课程 name,而不是SQL表的物理名称(documentation)
我认为你的循环应该是:
foreach ($results as $result){
$row = $results->current();
$clients = $row->findDependentRowset('ORM_Post_Client','post');
}
因为您正在寻找帖子的客户,这意味着帖子是您的rule
($row->findDependentRowset($table, [$rule]);
)
答案 1 :(得分:0)
这显示不起作用,老实说它毫无意义。
$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');
}