更新多个模型
我有两个模型,我想在一个 Yii 控制器/视图中呈现,现在我知道如何制作它,以便它可以从两个模型创建,但是我在更新方面诉诸了“黑客”,我希望得到一些帮助没错。
我有两个模型 Recipes 和 RecipeSteps,它们在 Recipe 控制器中定义为关系。 RecipeSteps 有一个列“recipe_id”,它是外键,也是食谱的主键。两个表都是 innoDB 并且有关系设置。 RecipeSteps 具有 ON DELETE CASCADE 和 ON UPDATE RETAIN 设置。
这是来自 RecipesController.php 的 actionUpdate
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$recipeSteps=$this->loadModelSteps($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Recipes']) && isset($_POST['RecipeSteps']))
{
$model->attributes=$_POST['Recipes'];
$recipeSteps->attributes=$_POST['RecipeSteps'];
if($model->save())
$recipeSteps->recipe_id = $model->recipe_id;
$recipeSteps->save();
$this->redirect(array('view','id'=>$model->recipe_id));
}
$this->render('update',array(
'model'=>$model,
'recipeSteps'=>$recipeSteps,
));
}
,这是我的 loadModel。
public function loadModel($id)
{
$model=Recipes::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
这是我为另一个模型制作的第二个模型,我想将其压缩为一个模型。
public function loadModelSteps($id)
{
$recipeSteps=RecipeSteps::model()->findByPk($id);
if($recipeSteps===null)
throw new CHttpException(404,'The requested page does not exist.');
return $recipeSteps;
}
所以我想做的是弄清楚如何只有一个“LoadModel”适用于 actionUpdate 中的两个模型。
I have two models that I would like to present in a single Yii Controller/View now I know how to make it so that it can Create from both however I resorted to a "hack" for the update side and I would appreciate some help making it right.
I have two models Recipes and RecipeSteps that in defined as a relation in the Recipe controller. RecipeSteps has a column "recipe_id" which is a foreign key and the Primary key of Recipes. Both tables are innoDB and have relations setup. RecipeSteps has ON DELETE CASCADE and ON UPDATE RETAIN set.
This is my actionUpdate from RecipesController.php
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$recipeSteps=$this->loadModelSteps($id);
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Recipes']) && isset($_POST['RecipeSteps']))
{
$model->attributes=$_POST['Recipes'];
$recipeSteps->attributes=$_POST['RecipeSteps'];
if($model->save())
$recipeSteps->recipe_id = $model->recipe_id;
$recipeSteps->save();
$this->redirect(array('view','id'=>$model->recipe_id));
}
$this->render('update',array(
'model'=>$model,
'recipeSteps'=>$recipeSteps,
));
}
And this is my loadModel.
public function loadModel($id)
{
$model=Recipes::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
And here is the second one that I made for the other Model that I would like to condense into a single model.
public function loadModelSteps($id)
{
$recipeSteps=RecipeSteps::model()->findByPk($id);
if($recipeSteps===null)
throw new CHttpException(404,'The requested page does not exist.');
return $recipeSteps;
}
So what I would like to do is figure out how to only have one "LoadModel" that works for both models in actionUpdate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 php 5.3.0 上尝试一下(类常量)
try this on php 5.3.0 (Class Constants)