I have Two tables: cars and car_types. Cars "hasOne" type and Type "hasMany" cars. I've defined this constraints in my models but how can I read the values inside my controller or view without getting the error message like below?
Mysql error message:
"Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CarTypes.type_id' in 'on clause'"
I got this error when I do this inside my CarsController:
public function index() {
$query = $this->Cars->find('all', ['contain' => [
'CarTypes'
]]);
debug($query->toArray());
}
Cars table:
id - type_id - method_id
Car_types table:
id - type
CarTypes model:
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('car_types');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->hasMany('Cars', [
'foreignKey' => 'type_id'
]);
}
Cars model:
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'
]);
}
Cars.CarTypes
forCarTypes
, – Jacek B Budzynski