我有两个表类:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
use App\Model\Entity\Comlib;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Validator;
use Cake\ORM\TableRegistry;
class ComlibsTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('questions');
// JOIN THE TABLES TOMORROW
$this->hasMany('Answers' , [
'foreignKey' => 'question_id'
]);
}
public function LFM( $live_req) {
$answers = TableRegistry::get('questions');
$query = $answers->find()
->distinct(['question.id'])
->matching('Answers', function ($q) use ($options) {
return $q->where(['Answers.id IN'=> '1']);
});
$query = $query->all();
//want Answers five of then
return $query;
}
}
?>
第二张表:
use Cake\ORM\Table;
use Cake\ORM\Query;
use Cake\ORM\TableRegistry;
class AnswersTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('answers');
}
}
在数据库中我有一个问题表和答案表,答案表有一个名为:question_id的外键。 我想要做的是我想从问题中选择所有内容并通过id,可能的内连接来加入它 我得到的错误是:
问题与答案无关
任何帮助都将不胜感激。
答案 0 :(得分:1)
您将数据库中的表与cakePHP Table
类混淆
当你调用TableRegistry::get()
时,你必须传递Table类的名称。所以正确的语法是
$answers = TableRegistry::get('Comlibs');
和cake将实例化ComlibsTable
Class
无论如何,您甚至不需要这样做,因为您已经在ComlibsTable中,并且您可以使用$this
来引用该表。所以你要做的就是
$query = $this->find()
->distinct(['question.id'])
->matching('Answers', function ($q) use ($options) {
return $q->where(['Answers.id IN'=> '1']);
})
->all();