0

Contact と Quote の 2 つのテーブルがあります。これは 1 対多の関係です (つまり、1 つの連絡先に複数の見積もりを含めることができます)。外部キーはすべて正しく設定されています。

新しい見積もりを作成するときに、連絡先のドロップダウン リストから選択できるようにしたいと考えています。

私のコードは次のようになります。

連絡先モデル:

class Contact extends AppModel {        
    public $hasMany = array('Quote' => array('className' => 'Quote', 'foreignKey' => 'contact_id'));
}

見積もりモデル

class Quote extends AppModel {      
    public $belongsTo = array('Contact' => array('className' => 'Contact', 'foreignKey' => 'contact_id'));

    public $validate = array(
            'name' => array(
                    'rule' => 'notEmpty'
            ),
            'amount' => array(
                    'rule' => 'notEmpty'
            )
    );      
}

QuotesController にメソッドを追加します。

public function add() {
    // TODO: Update this so the user can select the id from a drop down list.  
    $this->request->data['Quote']['contact_id'] = '1';

    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.');
        }            
    }
}

ご覧のとおり、私は現在、追加プロセスの一部としてユーザー ID をハードコーディングしています。

4

1 に答える 1