我有以下表:绑定程序、文档、用户、docs_users。医生belongsTo绑定,医生hasAndBelongsToMany用户。
我希望为当前登录的用户( docs_users表中的关联docs_users)获取绑定程序及其相关文档。
我尝试过用联接、条件等来控制和查找(“all”),但是我想不出如何从docs_users表中没有关联的用户中删除这些文档。
此代码不起作用:
$binders = $this->Binder->find(
'all',
array(
'joins' => array(
array(
'table' => 'binders_users',
'alias' => 'BindersUser',
'type' => 'inner',
'foreignKey' => false,
'conditions'=> array(
'BindersUser.binder_id = Binder.id',
'BindersUser.user_id = ' . $this->Auth->user('id')
)
),
array(
'table' => 'docs',
'alias' => 'Doc',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array(
'Doc.binder_id = Binder.id',
)
),
array(
'table' => 'docs_users',
'alias' => 'DocsUser',
'type' => 'left',
'foreignKey' => false,
'conditions'=> array(
'DocsUser.doc_id = Doc.id',
'DocsUser.user_id = ' . $this->Auth->user('id')
)
)
),
'recursive'=>0
)
);
$this->set('binders', $binders);这也不是:
$this->Binder->recursive = 2;
$this->Binder->Behaviors->attach('Containable');
$this->Binder->contain(array(
'Branch',
'Doc' => array(
'User' => array(
'DocsUser' => array(
'conditions' => array('id = "17"')
)
)
)
));
$binders = $this->Binder->find('all');如果你有经验丰富的专业人士的帮助,那就太好了!谢谢!
替代/简化解决方案?
如果我只想获得用户拥有权限的绑定器,这是可行的。又短又甜。但是,它仍然会发送所有相关的文档,这不是我想要的行为。它只需要传递用户拥有权限的文档(如前所述)。
$binders = $this->Binder->find(
'all',
array(
'joins' => array(
array(
'table' => 'binders_users',
'alias' => 'BindersUser',
'type' => 'inner',
'foreignKey' => false,
'conditions'=> array(
'BindersUser.binder_id = Binder.id',
'BindersUser.user_id = ' . $this->Auth->user('id')
)
)
)
)
);发布于 2011-03-11 21:53:46
这是最后的解决方案,我想出的基础上,所有伟大的反馈我得到。我认为这是一个优雅的解决方案,可以在任何需要深度关联的场景中重用。
在binder_controller中,我解除了Doc模型的绑定,并使用finderQuery将其绑定回只选择用户有权查看的文档。然后,在联接binders_users表中,只选择用户有权限的绑定。
谢谢大家的帮助!
$this->Binder->unbindModel(array('hasMany' => array('Doc')));
$this->Binder->bindModel(
array('hasMany' => array(
'Doc' => array(
'className' => 'Doc',
'foreignKey' => 'binder_id',
'dependent' => false,
'finderQuery' => 'SELECT Doc.* FROM docs AS Doc INNER JOIN docs_users AS DocsUser ON DocsUser.doc_id = Doc.id AND DocsUser.user_id = ' . $this->Auth->user('id')
)
)
)
);
$binders = $this->Binder->find(
'all',
array(
'joins' => array(
array(
'table' => 'binders_users',
'alias' => 'BindersUser',
'type' => 'inner',
'foreignKey' => false,
'conditions'=> array(
'BindersUser.binder_id = Binder.id',
'BindersUser.user_id = ' . $this->Auth->user('id')
)
)
)
)
);关于绑定/非绑定模型的更多信息
发布于 2011-03-11 11:03:00
以下是在CakePHP中对数据进行深度查找时可用的几个选项:
发布于 2011-03-11 11:08:34
在这一行中,您需要告诉Cake您所指的是哪个型号的id:
'conditions' => array('id = "17"')例如DocsUser.id
...and --您不使用可包含的递归。把它处理掉。
https://stackoverflow.com/questions/5266626
复制相似问题