연락처 및 견적 테이블이 두 개 있습니다. 일대 다 (1 개의 연락처에 많은 인용 부호가있을 수 있음) 관계입니다. 외래 키가 모두 올바르게 설정되었습니다.CakePHP 내 테이블에 다른 테이블에 액세스
새 견적을 만들 때 연락처 드롭 다운 목록에서 선택할 수 있기를 원합니다.
이연락 모델 :
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를 코딩합니다.
스티븐, 목표를 명확히 할 의향이 있습니까? 무슨 일이 일어나기를 기대합니까, 현재 일어나고있는 일이며, 당신이 그것을 성취하는 것을 방해하는 어떤 문제가 있습니까? –