当存在关系时 CakePHP 连接表
我有一个 hasMany Librarian
的 Message
模型。
我遇到的问题是,当我尝试将表连接到 Librarian
表时,该表尚未连接 - 即我创建的连接出现在关系之前加入已创建。
$this->Message->find('all', array(
'joins' => array(
array(
'table' => 'users',
'alias' => 'User',
'conditions' => array('User.id = Librarian.id')
)
)
));
这会生成一个查询:
SELECT `Message`.`id`, `Message`.`librarian_id`,
`Message`.`Librarian`.`id`, `Librarian`.`user_id`
FROM `contact_messages` AS `Message`
INNER JOIN users AS `User` ON (`User`.`id` = `Librarian`.`user_id`)
LEFT JOIN `librarians` AS `Librarian`
ON (`Message`.`librarian_id` = `Librarian`.`id`)
WHERE `Message`.`id` = 3
我收到错误
“on 子句”中的未知列“Librarian.user_id”
如何在已将 hasMany 表包含在构建查询中之后加入该表?
干杯
I have a Message
model that hasMany Librarian
.
The problem I am having is that when I try to join a table to the Librarian
table, that table has not been joined yet - i.e. the join I create appears before the relationship join is created.
$this->Message->find('all', array(
'joins' => array(
array(
'table' => 'users',
'alias' => 'User',
'conditions' => array('User.id = Librarian.id')
)
)
));
This generates a query along these lines:
SELECT `Message`.`id`, `Message`.`librarian_id`,
`Message`.`Librarian`.`id`, `Librarian`.`user_id`
FROM `contact_messages` AS `Message`
INNER JOIN users AS `User` ON (`User`.`id` = `Librarian`.`user_id`)
LEFT JOIN `librarians` AS `Librarian`
ON (`Message`.`librarian_id` = `Librarian`.`id`)
WHERE `Message`.`id` = 3
I get the error
Unknown column 'Librarian.user_id' in 'on clause'
How can I join to a hasMany table after it has already been included in the build query?
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我倾向于通过实际将关系绑定到代码中来解决这个问题。这是一种很老套的方法,而且非常古老,非常 1.2。
这是使用
bindModel()
,您可以在此处阅读,http://book.cakephp.org/view/3/The-Manual#!/view/78/Associations-Linking-Models-TogetherAssociations 模型方法的文档在这里, http://api12.cakephp.org/class/model #method-ModelbindModel
这个想法基本上是,即使不存在关系,或者它们有较远的关系,您也可以临时将两个模型绑定在一起。我已经做到了,而且确实有效。我在某处有一些代码,但不在手边。
另外,如果您使用的是较新的东西,请务必查看
Containable()
,因为据我了解,它包含更多的模型绑定。 http://book.cakephp.org/#!/view/1323/ContainableI tended to tackle this by actually binding a relationship into the code as I went along. It's a hacky method and quite old, very 1.2.
This was using
bindModel()
which you can read about on here, http://book.cakephp.org/view/3/The-Manual#!/view/78/Associations-Linking-Models-TogetherThe documentation for the model method is here, http://api12.cakephp.org/class/model#method-ModelbindModel
The idea basically being that you can temporarily bind two models together even if no relationship exists, or if they have a distant relationship. I have done it, and it does work. I have some code somewhere, but not to hand.
Also if you are using newer stuff, be sure to check out
Containable()
as this, as I understand it, encompasses a little more of the model's bindings. http://book.cakephp.org/#!/view/1323/Containable我不确定我自己解释得如何。
我可能应该将问题分解为确切的关系:
消息 hasMany 图书馆员
Librarian belongsTo User
这意味着我可以设置
recursive = 2
来获取User em> 数据。当然,这很昂贵,但在这种情况下是很自然的事情。
I'm not sure how well I explained myself.
I should probably have broken down the problem into the exact relationships:
Message hasMany Librarian
Librarian belongsTo User
which means i'm able to set
recursive = 2
to get User data.Granted this is expensive but is the natural thing to do in this case.