当存在关系时 CakePHP 连接表

发布于 2024-11-04 04:10:24 字数 937 浏览 0 评论 0原文

我有一个 hasMany LibrarianMessage 模型。

我遇到的问题是,当我尝试将表连接到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是我以为 2024-11-11 04:10:24

我倾向于通过实际将关系绑定到代码中来解决这个问题。这是一种很老套的方法,而且非常古老,非常 1.2。

这是使用 bindModel() ,您可以在此处阅读,http://book.cakephp.org/view/3/The-Manual#!/view/78/Associations-Linking-Models-Together

Associations 模型方法的文档在这里, http://api12.cakephp.org/class/model #method-ModelbindModel

这个想法基本上是,即使不存在关系,或者它们有较远的关系,您也可以临时将两个模型绑定在一起。我已经做到了,而且确实有效。我在某处有一些代码,但不在手边。

另外,如果您使用的是较新的东西,请务必查看 Containable(),因为据我了解,它包含更多的模型绑定。 http://book.cakephp.org/#!/view/1323/Containable

I 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-Together

The 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

无声情话 2024-11-11 04:10:24

我不确定我自己解释得如何。

我可能应该将问题分解为确切的关系:

消息 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文