I have three tables:
1 lab_categories (columns include 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 laboratories(columns include 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 (columns include 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'
]);
}}
I want to be able to select lab_categories.name
associated with a specified laboratory.id
so as to be able to add it to an ajax call up I created.
I want to generate a result similar to what this query will generate;
Select lab_categories.id,category
From lab_categories
Join laboratories_lab_categories
where laboratories_lab_categories.laboratory_id=$id
AND laboratories_lab_categories.lab_category_id=lab_categories.id
I am using cakephp 3.1
and the tables were baked. I have search through related question but non was applicable for me.