如何在CakePHP 3中的多个列上联接两个表? [英] How can I join two tables on multiple columns in CakePHP 3?
问题描述
我正在使用CakePHP v3
I'm using CakePHP v3
我有一个看起来像这样的表:
I have a table that looks like this:
文档:
id | section | paragraph
-------------------------
1 2 4
文字:
id | section | paragraph | theText
---------------------------------------
12 2 4 Blah blah
所以在SQL中,我可以做这样的事情;
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
如何使用ORM在CakePHP中做类似的事情?两个表中的主键都设置为id列.
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.
我已经在Cake的文档中研究了foreignKey
和binidingKey
,但是我看不到如何在其中使用多个列.
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,这是代码示例,显示了我要如何访问它们.
FWIW, here is a sample of code that shows how I want to access them.
$cond = [
'contain' => ['text']
];
$docs = $this->Documents->find('all',$cond);
推荐答案
是的,有可能.只需使用数组来表达应匹配的列即可:
Yes, it is possible. Just use arrays to express the columns that should be matched:
$this->belongsTo('Things', [
'bindingKey' => ['key1', 'ke2'],
'foreignKey' => ['fk1', 'fk2']
]);
将与key1 = fk1
和key2 = fk2
这篇关于如何在CakePHP 3中的多个列上联接两个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!