我有三张桌子:
1个lab_categories(列包括id
,category
)
class LabCategoriesTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('lab_categories');
$this->displayField('id');
$this->primaryKey('id');
$this->hasMany('LabTests', [
'foreignKey' => 'lab_category_id'
]);
$this->belongsToMany('Laboratories', [
'foreignKey' => 'lab_category_id',
'targetForeignKey' => 'laboratory_id',
'joinTable' => 'laboratories_lab_categories'
]);
}}
2个实验室(列包括id
,name
)
class LaboratoriesTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('laboratories');
$this->displayField('name');
$this->primaryKey('id');
$this->hasMany('LabRefValues', [
'foreignKey' => 'laboratory_id'
]);
$this->belongsToMany('LabCategories', [
'foreignKey' => 'laboratory_id',
'targetForeignKey' => 'lab_category_id',
'joinTable' => 'laboratories_lab_categories'
]);
}}
3个laboratories_lab_categories(列包括id
,laboratory_id
,lab_category_id
)
class LaboratoriesLabCategoriesTable extends Table
{
public function initialize(array $config)
{
$this->table('laboratories_lab_categories');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Laboratories', [
'foreignKey' => 'laboratory_id',
'joinType' => 'INNER'
]);
$this->belongsTo('LabCategories', [
'foreignKey' => 'lab_category_id',
'joinType' => 'INNER'
]);
}}
我希望能够选择与指定lab_categories.name
相关联的laboratory.id
,以便能够将其添加到我创建的ajax调用中。
我想生成一个类似于此查询将生成的结果;
选择lab_categories.id,类别
来自lab_categories
加入laboratories_lab_categories
其中laboratories_lab_categories.laboratory_id = $ id
AND laboratories_lab_categories.lab_category_id = lab_categories.id
我正在使用cakephp 3.1
并且桌子被烘焙了。我搜索了相关问题,但非适用于我。