Zend_Db_Table_Abstract 加载连接模型
的表
我有一个名为: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在你的
ORM_Post_Client
中它应该是我也认为你的循环应该是:
因为你正在寻找一个帖子的客户,这意味着该帖子是你的
规则
(
$row->findDependentRowset($table, [$rule]);
)in your
ORM_Post_Client
it should bealso i think your loop should be :
because you are looking for clients of a post which means that post is your
rule
(
$row->findDependentRowset($table, [$rule]);
)所提出的这是行不通的,老实说这没有任何意义。
MMc 是正确的,因为您引用的表定义不正确,但是您的代码也存在一些问题。也许尝试这样的事情:
This as presented won't work, honestly it makes no sense.
MMc is correct in that you reference table definition was incorrect however your code has some issues as well. Maybe try something like: