2016-03-02 141 views
0

當我刪除一個分類,我得到這個錯誤Cakephp。 SQLSTATE [42S22]:列未找到:在 '字段列表' 1054未知列 'CalendarsClassification.id'

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 

CalendarsClassificationid不存在,但我不知道如何改變它。

ClassificationsController

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' => '', 
     ) 
); 

}

+0

'CalendarsClassification'表中存在'id'字段..? – Yash

+0

試試這個$ this-> Classification-> delete(array('CalendarsClassification.calendar_id'=> $ id),false); – Yash

回答

0

我相信它應該是 「CALENDAR_ID」,而不是 「ID」。 根據查詢反映。

選擇CalendarsClassificationid FROM calendars_classifications AS CalendarsClassification WHERE CalendarsClassificationcalendar_id = 221

0

如果你的表沒有列id那麼你需要define which column is the primary key在相關模型中。例如: -

class CalendarsClassification extends AppModel { 

    public $primaryKey = 'example_id'; 

} 

模型delete()方法使用主鍵刪除與所以你必須設置primaryKey的模型,如果是使用此方法之前,從蛋糕約定不同的第一個參數。

對於每個模型,您都應該有一個主鍵,建議您堅持使用Cake約定並將其命名爲id,因爲這樣會顯着簡化您的代碼。

如果您不想通過主鍵刪除記錄,那麼您希望使用deleteAll(),您可以爲其刪除條件作爲第一個參數。事情是這樣的: -

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

如果在表中的字段「ID」和你仍然無法刪除該行,那麼你應該清除緩存,然後嘗試刪除該行。

這裏是緩存路徑: \程序\ tmp目錄\緩存\型號

\程序\ tmp目錄\緩存\持久

注意:不要刪除該文件夾。

希望這會有幫助

相關問題