2014-07-09 73 views
0

我試圖將數據保存到兩個不同的模型在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_marginsales_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, 
     )); 

} 

回答

1

首先使用模型

$model = new SalesRepMinMargin; 
$model->attributes = $_POST['SalesRepMinMargin']; 
if($model->save(false)) { 
    /* use second model */ 
    $model2 = new SalesRepMinMarginHistory; 
    $model2->attributes = $_POST['SalesRepMinMarginHistory']; 
    $model2->save(); 
    $this->redirect(array('view', 'id' => $model->id)); 
} 
+0

感謝您的回覆。這是第二種模式,是否需要改變這一點? \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

+0

嘗試過的代碼,但它不工作。想知道問題出在我的SQL上,因爲我在那裏接收並出錯。在這裏發佈的問題:http://stackoverflow.com/questions/24679010/yii-mysql-foreign-key-constraint-fails – DR1

+0

actionCreate()函數沒問題。只需替換 - $ model-> save(false); – TBI

0

我不是專家,但也許你可以創建一個觸發器來實現這種變化?無論何時在sales_rep_min_margin表中獲取新表時,都可以將數據轉儲到sales_rep_min_margin_history表中。

+0

感謝您的答覆保存在第一個表中的數據。這似乎是一個簡單的解決方法,我所研究的參考文獻提供的代碼似乎可以做到,但我還沒有找到錯誤。 – DR1

1

第二個模型的保存功能不會影響,因爲保存第一個模型後會調用重定向功能。

看看維基頁面。

if($valid) 
{ 
    // use false parameter to disable validation 
    $a->save(false); 
    $b->save(false); 
    // ...redirect to another page 
} 

應該在兩個模型成功保存後調用重定向函數。

+0

感謝您的回覆。這是第二種模式,是否需要改變這一點? \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

相關問題