Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate with multiple Models
    primarykey
    data
    text
    <p>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.</p> <p>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. </p> <p>This is my actionUpdate from RecipesController.php</p> <pre><code>public function actionUpdate($id) { $model=$this-&gt;loadModel($id); $recipeSteps=$this-&gt;loadModelSteps($id); // Uncomment the following line if AJAX validation is needed // $this-&gt;performAjaxValidation($model); if(isset($_POST['Recipes']) &amp;&amp; isset($_POST['RecipeSteps'])) { $model-&gt;attributes=$_POST['Recipes']; $recipeSteps-&gt;attributes=$_POST['RecipeSteps']; if($model-&gt;save()) $recipeSteps-&gt;recipe_id = $model-&gt;recipe_id; $recipeSteps-&gt;save(); $this-&gt;redirect(array('view','id'=&gt;$model-&gt;recipe_id)); } $this-&gt;render('update',array( 'model'=&gt;$model, 'recipeSteps'=&gt;$recipeSteps, )); } </code></pre> <p>And this is my loadModel. </p> <pre><code>public function loadModel($id) { $model=Recipes::model()-&gt;findByPk($id); if($model===null) throw new CHttpException(404,'The requested page does not exist.'); return $model; } </code></pre> <p>And here is the second one that I made for the other Model that I would like to condense into a single model. </p> <pre><code>public function loadModelSteps($id) { $recipeSteps=RecipeSteps::model()-&gt;findByPk($id); if($recipeSteps===null) throw new CHttpException(404,'The requested page does not exist.'); return $recipeSteps; } </code></pre> <p>So what I would like to do is figure out how to only have one "LoadModel" that works for both models in actionUpdate. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload