Passing multiple values from Controller to View

I have this problem, I need to pass multiple values from controller to view (form)

for example following is my action

public function actionCreate()


{


	$model=new ProductCondition;





	// Uncomment the following line if AJAX validation is needed


	// $this->performAjaxValidation($model);





	if(isset($_POST['ProductCondition']))


	{


		$model->attributes=$_POST['ProductCondition'];


		if($model->save())


			$this->redirect(array('view','id'=>$model->id));


	}





	$productCategories = CHtml::listData(ProductCategory::model()->findAll(), 'id','name');


	


	[b]$model->productCatList = $productCategories;[/b]


	


	$this->render('create',array(


		'model'=>$model,


		[b]'catList'=>$model->productCatList,[/b]


	));


}

Here I’m passing ‘model’ and ‘catList’ just to verify I pass catList as a member property of the model as well.

In the view which is a form to create new object.

<div class="row">


	<?php echo $form->labelEx($model,'productCategory'); ?>		


	[b]<?php echo $form->listBox($model, 'productCategory', $catList, array('multiple'=>"multiple")); ?>[/b]


	 		


	<?php echo $form->error($model,'productCategory'); ?>


</div>	

Here it throws an exception saying "Invalid argument supplied for foreach()" which is that $catList is null,

but if I replace $catList with following

<?php echo $form->listBox($model, ‘productCategory’, $model->productCatList, array(‘multiple’=>“multiple”));

Then I can see listbox and the page with items? Why cannot access the $catList directly, is form views support only one varaible from controller to view? What am I missing here, Please help.

You can place as many variables as stated on the array, but there is something I really do not understand:




$productCategories = CHtml::listData(ProductCategory::model()->findAll(), 'id','name');


$model->productCatList = $productCategories;


$this->render('create',array(

'model'=>$model,

'catList'=>$model->productCatList,

));




Why dont you do it this way?

on your view directly::::





<div class="row">

<?php echo $form->labelEx($model,'productCategory'); ?>	

<?php echo $form->listBox($model, 'productCategory', CHtml::listData(ProductCategory::model()->findAll(), 'id','name'), array('multiple'=>"multiple")); ?>


<?php echo $form->error($model,'productCategory'); ?>

</div>	



or in your controller::::




$this->render('create',array(

'model'=>$model,

'catList'=>CHtml::listData(ProductCategory::model()->findAll(), 'id','name'),

));




I suspect that the problem is that you are trying to attach an arbitrary value to a model.




$model->productCatList = $productCategories;



To do this you would have to define a public variable in the model called $productCatList

Unless you have a specific reason I would recommend you either pass the list separately or generate it inline as suggested by Antonio.

I have this problem, I need to pass multiple values from controller to view (form)

for example following is my action

public function actionCreate()


{


	&#036;model=new ProductCondition;





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['ProductCondition']))


	{


		&#036;model-&gt;attributes=&#036;_POST['ProductCondition'];


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;id));


	}





	&#036;productCategories = CHtml::listData(ProductCategory::model()-&gt;findAll(), 'id','name');


	


	[b]&#036;model-&gt;productCatList = &#036;productCategories;[/b]


	


	&#036;this-&gt;render('create',array(


		'model'=&gt;&#036;model,


		[b]'catList'=&gt;&#036;model-&gt;productCatList,[/b]


	));


}

Here I’m passing ‘model’ and ‘catList’ just to verify I pass catList as a member property of the model as well.

In the view which is a form to create new object.

&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'productCategory'); ?&gt;		


	[b]&lt;?php echo &#036;form-&gt;listBox(&#036;model, 'productCategory', &#036;catList, array('multiple'=&gt;&quot;multiple&quot;)); ?&gt;[/b]


	 		


	&lt;?php echo &#036;form-&gt;error(&#036;model,'productCategory'); ?&gt;


&lt;/div&gt;	

Here it throws an exception saying "Invalid argument supplied for foreach()" which is that $catList is null,

but if I replace $catList with following

<?php echo $form->listBox($model, ‘productCategory’, $model->productCatList, array(‘multiple’=>“multiple”));

Then I can see listbox and the page with items? Why cannot access the $catList directly, is form views support only one varaible from controller to view? What am I missing here, Please help.

I’m new at this, but a thought:




'model'=>$model,

array('catList'=>'$model->productCatList,



While typing this I had another thought:

How about using $model->productCatList directly in the view.




<?php echo $form->listBox($model, 'productCategory', $model->productCatList, array('multiple'=>"multiple")); ?>



I also remember in the blog demo that they use a dropdownList for selecting the post Status. And I have seen/tried code in (I think) in the phonebook demo) that used a department name listing for a column input of an int type, and I think that the code was actually put in the model, but not sure of that.

Hope this at least points you in the right direction. JK

You don’t have to do this. You can simply send only model to a view like that:


$model->productCatList = $productCategories;

   	

$this->render('create',array('model'=>$model));

And in your view instead of using $catList simply use $model->productCatList. Assuming, you have such property (variable) declared in your model class.

Thanks for the reply, that was my whole point and that’s what I mentioned above if I pass just the $model and use it property in view that works without problem, but this catList is not part of the $model logically so I don’t want to pass it as member of $model. just to illustrate it works that way I used it as member, what I wanted to know is do somehting like this

$catList = ‘get cat list somehow’

$this->render(‘create’,array(

‘model’=>$model,

‘catList’=>$catList, // pass catList instead of attaching it to $model

));

and then in the view I need to access catList as below

<?php echo $form->listBox($model, ‘productCategory’, $catList, array(‘multiple’=>“multiple”)); ?>

instead of

<?php echo $form->listBox($model, ‘productCategory’, $model->productCatList, array(‘multiple’=>“multiple”)); ?>

I have done similar thing in displaying (i.e. actionView ) by passing multiple values but here it didn’t work?

Actually this is my question all about, I wanted to do this way, I want to pass catList seperate from model as it has not relationship to it. But it doesn’t pass correctly to the View and listBox logic throwing an error because its is null

But if I pass through the $model as a member variable then it can be accessed, that is my question all about. I want to pass it as separate to the model

I have mentioned it again in this thread I don’t know how two forum threads initiated by single post

http://www.yiiframework.com/forum/index.php?/topic/14752-passing-multiple-values-from-controller-to-view/page__p__73385__fromsearch__1#entry73385

Let me make some further investigations. Your problem sounds interesting. I never found such and never heard that there are any problems sending two or more variables to a view. If I find anything interesting I’ll let you know.

You mean that you have tried this way already and it doesnt work?




$this->render('create',array(

'model'=>$model,

'catList'=>CHtml::listData(ProductCategory::model()->findAll(), 'id','name'),

));



and also this way on your view and fails?




<div class="row">

<?php echo $form->labelEx($model,'productCategory'); ?> 

<?php echo $form->listBox($model, 'productCategory', CHtml::listData(ProductCategory::model()->findAll(), 'id','name'), array('multiple'=>"multiple")); ?>


<?php echo $form->error($model,'productCategory'); ?>

</div>  



I do this all the time and have no poblems at all.

By the way and out of the record, if you wish to set your listbox as multiple, shouldn’t you also set its size?

Same here, just as Antonio wrote you. Either I’m (we’re) missing something or there have to be an error in your own code.

Case one: Controller code:


public function actionView($id)

{

    	$model = $this->loadModel($id);


    	$this->render('view', array('model'=>$model));

}

View code:


<?php echo('$model->BCODE = '.$model->BCODE); ?>

Case two: Controller code:


public function actionView($id)

{

    	$model = $this->loadModel($id);


    	$this->render('view', array('model'=>$model, 'bcode'=>$model->BCODE));

}

View code:


<?php echo('$model->BCODE = '.$bcode); ?>

Both cases (solutions, approaches) works fine, without error and without change.

You can even use in a controller:


public function actionView($id)

{

    	$model = $this->loadModel($id);

    	$bcode = $model->BCODE;


    	$this->render('view', array('model'=>$model, 'bcode'=>$bcode));

}

if you wish. No difference, as this is just simple variable manipulation, nothing else.

So, as you can see, there is no difference in either if you send only model to a view or a particular property of that model. Double check your code.

Exactly, that’s why in my first post I mentioned, in bold and even though I assign it to property of $model I pass it using different element in the array as below.

$model->productCatList = $productCategories;

$this->render(‘create’,array(

‘model’=>$model,

‘catList’=>$model->productCatList,

));

}

and accessing it in the view as below

<div class="row">

<?php echo $form->labelEx($model,‘productCategory’); ?>

<?php echo $form->listBox($model, ‘productCategory’, $catList, array(‘multiple’=>“multiple”)); ?>

<?php echo $form->error($model,‘productCategory’); ?>

</div

Then it gives the error but without changing any thing if I do following it works

<?php echo $form->listBox($model, ‘productCategory’, $model->productCatList, array(‘multiple’=>“multiple”));

I’ll try again with a new webapp, and see how it goes, thank you very much for your kind help. and I’ll come up with very simple code possible if it failse again

Thank you very much for the support.

Try to echo or use var_dump or print_f inside a view to see what exactly what do you get in $catList? To make yourself sure if the problem exists in rendering view and passing variables to it or if in $form->listBox?

Sorry to say but you didnt do what I or Trej said, in your first post you set the variable catList to a property on your Model:




[b]$model->productCatList = $productCategories;[/b]


$this->render('create',array(

'model'=>$model,

[b]'catList'=>$model->productCatList,[/b]

));

}



What we are saying is if you have tried differently. Just for curiosity my friend,

  1. could you forget about the code on controller just pass the $model variable.

  2. on your view, make sure you dont have any $catList references and use this:




<div class="row">

<?php echo $form->labelEx($model,'productCategory'); ?> 

<?php echo $form->listBox($model, 'productCategory', CHtml::listData(ProductCategory::model()->findAll(), 'id','name'), array('multiple'=>"multiple")); ?>


<?php echo $form->error($model,'productCategory'); ?>

</div>  



and we work from there… if that fails, please activate your weblogroute and tell us what is the error. As Trej said, drop here your error.

Cheers

Ok I guess I found something interesting, in your example you did this in actionView and access it in the view.php but I did it in the actionCreate and wants it in _form.php, I think that’s the difference, To illustrate this I’m going to attach few screenshots, please refer it and tell me is this a limitation of actionCreate

Plese refer following images in order, and that’s why I mentioned in my initial post I have done this in actionViews before but this time it is actionCreate.

Thanks

Really, really strange thing! I don’t know anything about any limitations or differences between create and index action. Action is an action and there should be any difference. To be honest I’m running out of ideas why do you have these variables nulled in one view and not nulled in another one. Maybe Antonio or someone else will be able to help. Sorry, but it seems that I can’t! :[

Sorry if I’m making some trouble, Yes if I do like this, it works, but I don’t want to do that way as it directly accessing my Model logic, I want to pass my data through the controller, my humble question is, is it possible to pass multiple items from actionCreate to the _form.php instead of directly accessing the Model logic or only by using $model properties

Thanks again for your help, and I please refer my reply with some screenshots

finally I found why it is not working in actionCreate.

By default the code generated by Yii for create and update are structured as partial views. so you have create.php as follows




<?php

$this->breadcrumbs=array(

	'Product Conditions'=>array('index'),

	'Create',

);


$this->menu=array(

	array('label'=>'List ProductCondition', 'url'=>array('index')),

	array('label'=>'Manage ProductCondition', 'url'=>array('admin')),

);

?>


<h1>Create ProductCondition</h1>


<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>




So what happen here is even though we pass additional items in array in this generated code it only let $model to pass into _form.php limiting the rest, so if we change the above line to pass all our data as below




...

<h1>Create ProductCondition</h1>


<?php echo $this->renderPartial('_form', array('model'=>$model, 'catList' => $catList)); ?>



Then in the _form.php we could access both $model and $catList without any issue.

Thanks for all giving me help in finding the issue, I really love Yii :slight_smile:

And what is the feeling of finding solution yourself, when other was not able to help? :] Pretty good, huh? :}

BTW: We also LOVE Yii! :]

Yes it is really great feeling, but it is not right to say when other was not able to help, you all helped me every possible way, to be honest as I mentioned in different thread, that it is not more than few weeks I actually started learning PHP, and just two days learning Yii, so I didn’t most of the PHP syntax and available functions. But I have good background in concepts. So I must thank you one of the very useful function you mentioned var_dump()

Once again it is really nice to have bunch of supporting people like you when learning something new