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'
]);
}
}