cakephp 一对多读取
我在工人和公司之间建立了一对多的关系。 在我的 WorkerModel 中:
var $belongsTo = '公司';
在我的公司模型中:
var $hasMany = '工人';
在我的 FirmController 中,我得到了函数 feed(id=null)。我想显示属于该公司的所有工人事件,如下所示:
http: (...)/firms/calendar/1
我得到:
函数提要($id=NULL) {
$firm = $this->Firm->read(NULL,$id) ;
$workers = $firm['工人'] ;
$conditions = array('Event.worker_id'=>$workers['0']['id'] );
$events = $this->Event->find('all',array('conditions' =>$conditions));
这很好用,但会显示与 $workers var 上的第一个工作人员相关的所有事件。 如何编写此查询来填充与属于公司的所有工人相关的所有事件?
I got relation One to many between Worker and Company.
In my WorkerModel:
var $belongsTo = 'Firm';
In my FirmModel:
var $hasMany = 'Worker';
In my FirmController I got function feed(id=null). I want to display all workers events that belong to this Firm like this:
http: (...)/firms/calendar/1
I got:
function feed($id=NULL) {
$firm = $this->Firm->read(NULL,$id) ;
$workers = $firm['Worker'] ;
$conditions = array('Event.worker_id'=>$workers['0']['id'] );
$events = $this->Event->find('all',array('conditions' =>$conditions));
This works well, but displays all events connected with first worker on $workers var.
How to write this query to populate all events connected with ALL workers that belongs to firm?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否正确设置了关系?事件属于工人吗?在读取之前查看设置
$this->Firm->recursive = 2
,如果关系设置正确,它应该会获取它们。另请查看可控制行为。
Have you setup the relationships properly? Does Event belongTo Worker? Look at setting
$this->Firm->recursive = 2
just before your read and it should fetch them if the relationships are set up correctly.Also look into the Containable behaviour.