【发布时间】:2017-07-23 15:46:09
【问题描述】:
我有两个表:cars 和 car_types。汽车“hasOne”型和“hasMany”型汽车。我已经在我的模型中定义了这个约束,但是我怎样才能读取控制器或视图中的值而不会收到如下错误消息?
Mysql 错误信息:
“错误:SQLSTATE[42S22]:未找到列:1054 未知列 'on 子句'中的'CarTypes.type_id'"
当我在 CarsController 中执行此操作时出现此错误:
public function index() {
$query = $this->Cars->find('all', ['contain' => [
'CarTypes'
]]);
debug($query->toArray());
}
汽车表:
id - type_id - method_id
Car_types 表:
id - type
车型:
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('car_types');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->hasMany('Cars', [
'foreignKey' => 'type_id'
]);
}
车型:
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换成CarTypes, -
这将给出同样的错误。
标签: mysql cakephp cakephp-3.0