我試圖將數據保存到兩個不同的模型在Yii中的兩個數據庫表。我已經諮詢了wiki:http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/和http://www.yiiframework.com/forum/index.php/topic/52109-save-data-with-two-models/,但我仍然無法將數據保存到兩個表中。我有兩個表sales_rep_min_margin
和sales_rep_min_margin_history
:將數據保存到兩個數據庫表中有兩個模型在Yii
CREATE TABLE `sales_rep_min_margin` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(32) NOT NULL,
`domestic` int(2) NOT NULL,
`overseas` int(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
歷史表:
CREATE TABLE `sales_rep_min_margin_history` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`min_margin_id` int(11) NOT NULL,
`from` int(11) DEFAULT NULL,
`to` int(11) DEFAULT NULL,
`update_username` varchar(32) NOT NULL,
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `min_margin_id` (`min_margin_id`),
CONSTRAINT `sales_rep_min_margin_history_ibfk_1` FOREIGN KEY (`min_margin_id`) REFERENCES `sales_rep_min_margin` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
My SalesRepMinMarginController code (right now) is:
public function actionCreate() {
$model = new SalesRepMinMargin;
$model2 = new SalesRepMinMarginHistory;
//Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model);
if (isset($_POST['SalesRepMinMargin'])) {
$model->attributes = $_POST['SalesRepMinMargin'];
if ($model->save())
$this->redirect(array('view', 'id' => $model->id));
}
if (isset($_POST['SalesRepMinMarginHistory'])) {
$model2->attributes = $_POST['SalesRepMinMarginHistory'];
$model2->save();
$this->render('create', array(
'model' => $model,
));
}
}
and 'SalesRepMinMarginHistoryController':
public function actionUpdate($id)
{
$model=$this->loadModel($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['SalesRepMinMarginHistory']))
{
$model->attributes=$_POST['SalesRepMinMarginHistory'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('update',array(
'model'=>$model,
));
}
我只是需要將數據保存到表,但我不需要在視圖中的「歷史」表中的數據。任何幫助都大大提升!有人給了我下面的代碼,但它不工作:
public function actionCreate() {
$model = new SalesRepMinMargin;
$model2 = new SalesRepMinMarginHistory;
//Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model);
if (isset($_POST['SalesRepMinMargin'])) {
$model->attributes = $_POST['SalesRepMinMargin'];
if ($model->save()) {
if (isset($_POST['SalesRepMinMarginHistory'])) {
$model2->attributes = $_POST['SalesRepMinMarginHistory'];
$model2->save();
}
$this->redirect(array('view', 'id' => $model->id));
}
}
$this->render('create', array(
'model' => $model, 'model2' => $model2,
));
}
感謝您的回覆。這是第二種模式,是否需要改變這一點? \t public function actionCreate() \t { \t \t $ model = new SalesRepMinMarginHistory; \t \t //如果需要AJAX驗證,請取消註釋以下行 \t \t // $ this-> performAjaxValidation($ model); \t \t如果(isset($ _ POST [ 'SalesRepMinMarginHistory'])) \t \t { \t \t \t $模型 - >屬性= $ _ POST [ 'SalesRepMinMarginHistory']; \t \t \t如果($模型 - >保存()) \t \t \t \t $這 - >重定向(陣列( '觀看', 'ID'=> $模型 - > ID)); \t \t} \t \t $這 - >呈現( '創建',陣列( \t \t \t '模式'=> $模型, \t \t)); \t} – DR1
嘗試過的代碼,但它不工作。想知道問題出在我的SQL上,因爲我在那裏接收並出錯。在這裏發佈的問題:http://stackoverflow.com/questions/24679010/yii-mysql-foreign-key-constraint-fails – DR1
actionCreate()函數沒問題。只需替換 - $ model-> save(false); – TBI