我有兩個表:cars和car_types。汽車「hasOne」型和Type「hasMany」汽車。我已經在我的模型中定義了這個約束,但是我怎樣才能讀取我的控制器或視圖中的值,而不會像下面那樣得到錯誤信息?CakePHP 3 - 1054列未找到
MySQL錯誤信息:
"Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CarTypes.type_id' in 'on clause'"
我這樣做時,我CarsController裏面我得到這個錯誤:
public function index() {
$query = $this->Cars->find('all', ['contain' => [
'CarTypes'
]]);
debug($query->toArray());
}
汽車表:
id - type_id - method_id
Car_types表:
id - type
CarTypes型號:
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'
]);
}
更改'CarTypes'的'Cars.CarTypes', –
這會給出同樣的錯誤。 – CodeWhisperer