0

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.

Moppo
  • 18,797
  • 5
  • 65
  • 64
medhybrid
  • 48
  • 6
  • 2
    Possible duplicate of [find on associated model's condition CakePHP 3.0](http://stackoverflow.com/questions/26799094/find-on-associated-models-condition-cakephp-3-0) – ndm Oct 10 '15 at 23:34
  • Thank you very much, I was not adding the \Cake\ORM\Query to my function – medhybrid Oct 11 '15 at 00:29

0 Answers0