0

テーブルにレコードを挿入しようとすると、次のエラーが発生します。

エラー: SQLSTATE[23000]: 整合性制約違反: 1452 子行を追加または更新できません: 外部キー制約が失敗しました ( invoice. quotes, CONSTRAINT quotes_ibfk_1FOREIGN KEY ( contacts_id) REFERENCES Contacts( id))

「連絡先」と「見積もり」の 2 つのテーブルの間に関係を設定しています。「quotes」には、contacts_id を設定する外部キーがあります。

引用符コントローラーの Add メソッドは次のようになります。

public function add() {
        $this->log('Quote Controller --> Add Method...1');
        $this->log($this->request->data);

        if ($this->request->is('post')) {                   
            $this->Quote->create(); // This line writes the details to the database.
            if ($this->Quote->save($this->request->data)) {             
                $this->Session->setFlash('Your quote has been saved.');
                $this->redirect(array('action' => 'index'));
            } else {                
                $this->Session->setFlash('Unable to add your quote.');
            }            
        }
    }

どんな助けでも感謝します。

4

1 に答える 1

0

データを保存しようとしたときに正しい外部キーがない場合、エラーが発生します。

$this->request->data 内の "contacts_id" 値が null / 空であると推測しています。debug($this->request->data); を追加するとよいでしょう。$this->Quote->create(); の後

また、CakePHP の規則に従っていないようです。contact_id フィールドは contact_id である必要があります。それは問題を解決するのに役立つかもしれません。

于 2013-05-20T01:49:34.347 に答える