如何添加項目並將其插入到2個表格中?CakePHP - 將項目添加到2個模型
所以我有一個'Type'表和'SpecificType'表。
類型具有字段'id'和一些其他常見字段。
SpecificType具有字段'id','type_id'和一些其他不常見的字段。
當我去/ specific_types /添加和我提交,理想情況下,我想先將其添加到'類型',然後將其添加到'SpecificType'。
這就是我現在擁有的。
在SpecificType型號
var $belongsTo = array(
'Type' => array(
'className' => 'Type',
'foreignKey' => 'type_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
在SpecificType控制器
var $uses = ('Type', 'SpecificType');
function add() {
if (!empty($this->data)) {
$this->Type->create();
if ($this->Type->save($this->data)) {
$this->SpecificType->create();
if ($this->SpecificType->save($this->data)) {
$this->Session->setFlash(__('The SpecificType has been saved', true));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The SpecificType could not be saved. Please, try again.', true));
}
} else {
$this->Session->setFlash(__('The Type could not be saved. Please, try again.', true));
}
}
}
在SpecificType add.ctp
echo $form->input('Type.data1');
echo $form->input('title');
所以,現在,這樣可以節省Type.data1但標題是不是得到保存。 我錯過了什麼?
感謝,
三通
附加信息: 當我打開MeioUpload第二屆模式不僅節省。
感謝泰勒。除了插入的數據似乎有缺陷之外,這幾乎是工作。例如'我的標題'變成'2y標題'。爲什麼第一個字符是隨機替換的? – teepusink 2011-04-19 04:14:09