Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:08:57+00:00 2026-05-29T12:08:57+00:00

I have two models that I would like to present in a single Yii

  • 0

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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:09:01+00:00Added an answer on May 29, 2026 at 12:09 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models that I would like to save in the same table.
Say that I have two models- Users and Accounts. Each account can have at
I have two different models that can show up in a category. In my
I have two ways of doing a similar thing and would like to know:
I have two models that are associated with each other. Customer has_one :primary_contact And
If I have two models that are guaranteed to have a one-to-one correspondence, i.e.
Let's assume I have two gmdistibution models that i obtained using modeldata1=gmdistribution.fit(data1,1); modeldata2=gmdistribution.fit(data2,1); Now
I have two models -- User and Entry -- that are related through a
I have two models (questions and answers) that slightly follow the popular Railscasts :
I have two models User and Admin(with RailsAdmin) that use Devise. I sign in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.