更新多个模型

发布于 2025-01-04 12:14:58 字数 1806 浏览 0 评论 0原文

我有两个模型,我想在一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2025-01-11 12:14:58

在 php 5.3.0 上尝试一下(类常量

public function loadModel($id,$modelName){
    $o=call_user_func(array($modelName,'model'));
    $model = $o->findByPk($id);
    if($modelByPk===null)
        throw new CHttpException(404,'The requested page does not exist.');
    return $modelByPk;
}

try this on php 5.3.0 (Class Constants)

public function loadModel($id,$modelName){
    $o=call_user_func(array($modelName,'model'));
    $model = $o->findByPk($id);
    if($modelByPk===null)
        throw new CHttpException(404,'The requested page does not exist.');
    return $modelByPk;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文