デシーズとチャップマンは正しかった。CakephpクックブックのDAta検証の章から解決策を見つけました。どうもありがとう。
以下は、私が追加した検証ルールです。
学生モデルの学生の名前:
var $validate=array(
'name'=>array(
'nameRule1'=>array(
'rule'=>array('minLength',3),
'required'=>true,
'allowEmpty'=>false,
'message'=>'Name is required!'
),
'nameRule2'=>array(
'rule'=>'isUnique',
'message'=>'Student name with the same parent name already exist!'
)
),
次に、StudentsController の add 関数で:
//checking to see if parent already exist in merry_parents table when siblings or twin are admitted.
$merry_parent_id=$this->Student->MerryParent->getMerryParentId($this->data['MerryParent']['email']);
if (isset($merry_parent_id)){
$this->data['Student']['merry_parent_id']=intval($merry_parent_id);
var_dump($this->data['Student']['merry_parent_id']);
if ($this->Student->save($this->data)){
//data is saved only to Students table and not merry_parents table.
$this->Session->setFlash(__('Your child\'s admission has been received.
We will send you an email shortly.',true));
}else
$this->Session->setFlash(__('Your admission could not be saved. Please, try again.',true));
}else{//New record. So, data is saved to Students table and merry_parents table.
if ($this->Student->saveAll($this->data)){ //save to students table and merry_parents table
$this->Session->setFlash(__('Your child\'s admission has been received.
We will send you an email shortly.',true));
}else
$this->Session->setFlash(__('Your admission could not be saved. Please, try again.', true));
}//new record end if
Chapman が述べたように、保存せずにデータを検証する必要はありませんでした。だから、私は使用しませんでした:
if ($this->Model->validates() ) {
save
} else {
error message / redirect
}