How can I join two tables on multiple columns in CakePHP 3?

大城市里の小女人 提交于 2019-12-23 01:19:20

问题


I'm using CakePHP v3

I have a table that looks like this:

Document:

id | section | paragraph
-------------------------
1       2         4

Text:

id | section | paragraph | theText
---------------------------------------
12      2         4        Blah blah

So in SQL I could do something like this;

SELECT * FROM document 
INNER JOIN text 
ON document.section=text.section 
AND document.paragraph=text.paragraph

How can I do something like this in CakePHP using the ORM? The Primary key in both tables is set up to be the id column.

I've looked into foreignKey and binidingKey in Cake's docs, but I can't see how to use multiple columns in those.

http://book.cakephp.org/3.0/en/orm/associations.html.

FWIW, here is a sample of code that shows how I want to access them.

$cond = [
        'contain' => ['text']
      ];

$docs = $this->Documents->find('all',$cond);

回答1:


Yes, it is possible. Just use arrays to express the columns that should be matched:

$this->belongsTo('Things', [
    'bindingKey' => ['key1', 'ke2'],
    'foreignKey' => ['fk1', 'fk2']
]);

That will match key1 = fk1 and key2 = fk2



来源:https://stackoverflow.com/questions/32341773/how-can-i-join-two-tables-on-multiple-columns-in-cakephp-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!