2014-07-09 72 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

相关问题