CodeWhisperer
CodeWhisperer

Reputation: 1195

CakePHP 3 table relations

I have the following tables: cars, car_types and car_methods. My issues with this are the relations. The car has one type and one method.

My goal is to show cars and his attachment "car type" and "car_method".

Cars table:

id - type_id - method_id

Car_types table:

id - type

Car_methods table:

id - method

How can I set this up inside my Model tables so that I can do inside my controller something like:

$this->Cars->CarTypes->find('all');

I tried as follow but it will give a No associated error:

Cars model:

class CarsTable extends Table {
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('cars');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->hasOne('CarTypes', [
        'className' => 'Cars.CarTypes',
        'foreignKey' => 'type_id',
        'propertyName' => 'type'
    ]);

    $this->hasOne('CarMethods', [
        'className' => 'Cars.CarMethods',
        'foreignKey' => 'method_id',
        'propertyName' => 'method'
    ]);
}
}

CarTypes model:

class CarTypesTable extends Table {

public function initialize(array $config) {
    parent::initialize($config);

    $this->setTable('car_types');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->hasMany('Cars', [
        'foreignKey' => 'type_id'
    ]);
}
}

CarMethots model:

class CarMethodsTable extends Table {

public function initialize(array $config) {
    parent::initialize($config);

    $this->setTable('car_method');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->hasMany('Cars', [
        'foreignKey' => 'method_id'
    ]);
}
}

Upvotes: 0

Views: 1860

Answers (2)

Disorder
Disorder

Reputation: 430

The belongsTo association is a natural complement to the hasOne and hasMany associations: it allows us to see the data from the other direction.

belongsTo: the current model contains the foreign key.

If you have foreign key in your table, then that table will belong to the associated table. So, in your case :

public function initialize(array $config) {
     $this->belongsTo('CarTypes', [
        'foreignKey' => 'type_id',
        'joinType' => 'LEFT'
     ]);

     $this->belongsTo('CarMethods', [
         'foreignKey' => 'method_id',
         'joinType' => 'LEFT'
     ]);
}

After this you can use $this->Cars->CarTypes->find('all');

Upvotes: 0

beta-developper
beta-developper

Reputation: 1774

Your two last relations are wrong, here is the good ones

CarTypes model:

public function initialize(array $config) {
    $this->hasMany('Cars', [  //A CarType hasMany Cars
        'foreignKey' => 'type_id'
    ]);
}

CarMethods model:

public function initialize(array $config) {
    $this->hasMany('Cars', [  //A CarMethod hasMany Cars
        'foreignKey' => 'method_id'
    ]);
}

Controller action

And for your query, add this two relations to the contain option :

//Get all cars with type and method
$this->Cars->find('all',
     [
        'contain' => ['CarTypes', 'CarMethods'] 
     ]
);

Upvotes: 2

Related Questions