CakePHP的。 SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'CalendarsClassification.id'

时间:2016-03-02 10:25:40

标签: php mysql cakephp cakephp-2.0 cakephp-2.3

当我删除分类时,我收到此错误

class

module.exports = { fun: function(){}, "class": MyClass } 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 不存在,但我不知道如何更改它。

ClassificationsController

CalendarsClassification

模型分类

id

}

3 个答案:

答案 0 :(得分:1)

如果有字段' id'在表中仍然无法删除该行,然后您应该清除缓存然后尝试删除该行。

这是缓存的路径: \应用\ TMP \缓存\模型

\应用\ TMP \缓存\持久

注意:请勿删除文件夹。

希望这有帮助

答案 1 :(得分:0)

我相信它应该是" calendar_id"而不是" id"。 根据查询反映。

SELECT CalendarsClassificationid FROM calendars_classifications AS CalendarsClassification WHERE CalendarsClassificationcalendar_id = 221

答案 2 :(得分:0)

If you're table doesn't have a column id then you need to define which column is the primary key in the relevant model. For example:-

class CalendarsClassification extends AppModel {

    public $primaryKey = 'example_id';

}

The model delete() method uses the primary key as the first parameter to delete with so you must set the primaryKey for the model if it is different from Cake conventions before using this method.

You really should have a primary key for every model and it is advisable to stick with Cake conventions and name it id if possible as this will simplify your code significantly.

If you are not trying to delete a record by the primary key then you want to be using deleteAll() for which you can pass conditions for the deletion as the first parameter. Something like this:-

$this->CalendarsClassification->deleteAll(['example_id' => $id]);