我试图将数据保存到两个不同的模型在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