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

时间:2015-09-01 22:20:25

标签: php mysql cakephp cakephp-3.0

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 个答案:

答案 0 :(得分:1)

是的,有可能。只需使用数组来表示应匹配的列:

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

这将匹配key1 = fk1key2 = fk2