当我删除分类时,出现此错误
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'CalendarsClassification.id' in 'field list'
SQL Query: SELECT `CalendarsClassification`.`id` FROM `calendars_classifications` AS `CalendarsClassification` WHERE `CalendarsClassification`.`calendar_id` = 221
CalendarsClassification
.id
不存在,但我不知道如何改变它。
分类控制器
public function delete($id = null) {
if (!$id) {
$this->flash(__('Invalid Classification', true), array('controller'=>'clients','action'=>'index'));
}
$this->request->data = $this->Classification->read(null, $id);
if ($this->Classification->delete($id, true)) {
$this->flash(__('Classificació esborrada.', true), array('controller'=>'blocks','action'=>'view',$this->data['Classification']['block_id']));
}
}
型号分类
class Classification extends AppModel {
var $name = 'Classification';
var $validate = array(
'long_name' => array(
'rule' => array('minLength', 1)
)
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'Block' => array('className' => 'Block',
'foreignKey' => 'block_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
var $hasMany = array(
'Line' => array('className' => 'Line',
'foreignKey' => 'classification_id',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'dependent' => true,
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
var $hasAndBelongsToMany = array(
'Calendar' => array('className' => 'Calendar',
'joinTable' => 'calendars_classifications',
'foreignKey' => 'calendar_id',
'associationForeignKey'=> 'classification_id',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'unique' => true,
'finderQuery' => '',
'deleteQuery' => '',
)
);
}
如果表中有字段"id",但仍然无法删除该行,则应清除缓存,然后尝试删除该行。
以下是缓存的路径:''应用''tmp''缓存''模型
和
''app''tmp''cache''persistent
注意:请勿删除该文件夹。
希望这有帮助
我认为它应该是"calendar_id"而不是"id"。根据查询反映。
选择CalendarsClassification
。id
从calendars_classifications
CalendarsClassification
CalendarsClassification
的地方。calendar_id
= 221
如果您的表没有列id
那么您需要定义相关模型中的主键列。例如:-
class CalendarsClassification extends AppModel {
public $primaryKey = 'example_id';
}
模型delete()
方法使用主键作为要删除的第一个参数,因此在使用此方法之前,如果模型与 Cake 约定不同,则必须设置模型的primaryKey
。
您确实应该为每个模型提供一个主键,建议坚持 Cake 约定并在可能的情况下将其命名id
因为这将大大简化您的代码。
如果不尝试按主键删除记录,则希望使用deleteAll()
,您可以将删除条件作为第一个参数传递。像这样的东西:-
$this->CalendarsClassification->deleteAll(['example_id' => $id]);