how to mix different models in a single page.

how do you mix/put different/multiple models in a single page? :blink:

single page = single view? Please clarify. If that is the case you could instantiate different models in the your action in the controller class such as:>


public function actionMyaction()

{

$model1 = new Model1();

$model2 = new Model2();


...do some more stuff....


$this->render('myaction', array('model1'=>$model1, 'model2'=>$model2));

}

In your view file you could access $model1 and $model2 depending on what they return in terms of value.

This works for me. Hope it helps

yeah that’s what i was looking for… lemme check that out and give you a feedback. thanks. :D

still doesn’t show… no error’s no anything… just what it looked like before putting those things… ???




public function actionList()

	{

		$criteria=new CDbCriteria;


		

		$pages=new CPagination(Event::model()->count($criteria));

		$pages->pageSize=self::PAGE_SIZE;

		$pages->applyLimit($criteria);


		// Is the event published or not?

		$criteria->condition = "publish = 1";

		

		$models=Event::model()->findAll($criteria);

		$model = new Banners;

		


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

			'models'=>$models,

			'pages'=>$pages,

			'model'=>$model

		));

	}




What would you expect to be returned. Are you sure that your queries return data? try to print_r/var_dump your ActiveRecords to see if they indeed return data.

they actually return data…

okay let me explain a bit further.

i have two models, Event and Banners. Of which, event is the defaultController.

now as i said above, event is the default controller and when i visit my site, it displays the events perfectly.

then i change the default controller to banners and it displays banners now, but just the banners, i mean i manipulate their respective data via an admin section which i developed myself. but that’s out of the question.

now i want my site to have the events, as it is seen by default, and the banners at the side, co-existing with the event model.

darn this is hard as digging through a concrete wall with your bare hands. :wacko:

which part doesn’t show? $model or $models? Is the actionList() the same for both controllers?

i actually have the slightest idea… but this is the one inside my BannersControllers




/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Banners');

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

			'dataProvider'=>$dataProvider,

		));

	}




and this is the one in my default controller




/**

	 * Lists all models.

	 */

	public function actionList()

	{

		$criteria=new CDbCriteria;

		$banner_model = new Banners();

		$banner = $banner_model->findAll();

		$limit = 3;

		$pages=new CPagination(Event::model()->count($criteria));

		$pages->pageSize=self::PAGE_SIZE;

		$pages->applyLimit($criteria);

                $criteria->condition = "publish = 1";

		

		$models=Event::model()->findAll($criteria);

		$banner=Banner::model()->findAll($criteria);

		


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

			'models'=>$models,

			'banner'=>$banner,

			


			'pages'=>$pages

			

		));


          }






can i just include() the other class in my main controller? coz now that i did that, it asks for the inclusion of Banner.php which is a model. and when i include it it somehow returns an error due to an invalid directory inside the include.

this is what shows up after including the Banner model




PHP Error


Description


include(Banner.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory


Source File


C:\xampp\yii\framework\yiilite.php(192)


00180:         else

00181:             self::$_aliases[$alias]=rtrim($path,'\\/');

00182:     }

00183:     public static function autoload($className)

00184:     {

00185:         // use include so that the error PHP file may appear

00186:         if(isset(self::$_coreClasses[$className]))

00187:             include(YII_PATH.self::$_coreClasses[$className]);

00188:         else if(isset(self::$_classes[$className]))

00189:             include(self::$_classes[$className]);

00190:         else

00191:         {

00192:  /* this is highlighted*/ include($className.'.php');

00193:             return class_exists($className,false) || interface_exists($className,false);

00194:         }

00195:         return true;

00196:     }

00197:     public static function trace($msg,$category='application')

00198:     {

00199:         if(YII_DEBUG)

00200:             self::log($msg,CLogger::LEVEL_TRACE,$category);

00201:     }

00202:     public static function log($msg,$level=CLogger::LEVEL_INFO,$category='application')

00203:     {

00204:         if(self::$_logger===null)

Stack Trace


#0 C:\xampp\yii\framework\yiilite.php(192): autoload()

#1 unknown(0): autoload()

#2 C:\xampp\htdocs\viasatondemand\protected\controllers\EventController.php(276): spl_autoload_call()

#3 C:\xampp\yii\framework\yiilite.php(3430): EventController->actionList()

#4 C:\xampp\yii\framework\yiilite.php(3002): CInlineAction->run()

#5 C:\xampp\yii\framework\yiilite.php(5655): EventController->runAction()

#6 C:\xampp\yii\framework\web\widgets\COutputCache.php(147): CFilterChain->run()

#7 C:\xampp\yii\framework\yiilite.php(5652): COutputCache->filter()

#8 C:\xampp\yii\framework\yiilite.php(5664): CFilterChain->run()

#9 C:\xampp\yii\framework\yiilite.php(3334): CAccessControlFilter->filter()

#10 C:\xampp\yii\framework\yiilite.php(5697): EventController->filterAccessControl()

#11 C:\xampp\yii\framework\yiilite.php(5652): CInlineFilter->filter()

#12 C:\xampp\yii\framework\yiilite.php(2992): CFilterChain->run()

#13 C:\xampp\yii\framework\yiilite.php(2977): EventController->runActionWithFilters()

#14 C:\xampp\yii\framework\yiilite.php(1542): EventController->run()

#15 C:\xampp\yii\framework\yiilite.php(1434): CWebApplication->runController()

#16 C:\xampp\yii\framework\yiilite.php(1049): CWebApplication->processRequest()

#17 C:\xampp\htdocs\viasatondemand\index.php(11): CWebApplication->run()






Are you sure is not a typo?

In your code above you wrote Banners, and the error is about Banner.

strange, my model is Banners.php not Banner.php that’s why i used Banners.

what is the name of the model file and what is the name of the class in it?

@kael

Check your code carefully…




/**

* Lists all models.

*/

public function actionList()

{

    $criteria=new CDbCriteria;

    $banner_model = new Banners();

	$banner = $banner_model->findAll();    //   <--- variable $banner " not used anywhere!!! "

	$limit = 3;

	$pages=new CPagination(Event::model()->count($criteria));

	$pages->pageSize=self::PAGE_SIZE;

	$pages->applyLimit($criteria);

	$criteria->condition = "publish = 1";

                

	$models=Event::model()->findAll($criteria);

	$banner=Banner::model()->findAll($criteria); 	// <-- here you call model Banner and "again" assign to variable $banner

                

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

         	'models'=>$models,

         	'banner'=>$banner,

         	'pages'=>$pages

  	));

}



Look at the "red" comments I inserted

ara?.. yeah… that was stupidly done… lemme check on everything

well the model file is Banners.php and the class name is Banners.

okay lemme explain a bit more…

i wanna put model Banners into my default controller, since a web app can’t have two different controllers at the same time ayt?. so basically, i’m tryin to put my main model, and banners, actually I plan to put other models in, so trying to figure out how to put just one will be a great jumpstarter.

You got the error


include(Banner.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory

Because you used


$banner=Banner::model()->findAll($criteria);

  • I commented that in the previous post…

If your model is Banners then you should use


$banner=Banners::model()->findAll($criteria);

don’t have an error anymore… my problem now is how to make it show up in the page… coz id doesn’t.

$banner gets a result of findAll() - http://www.yiiframework.com/doc/api/CActiveRecord#findAll-detail

so for example if you have a property name in banners you can use:




foreach($banner as $ban)

   echo $ban->name;



problem solved! i just created a widget so i can use it anywhere! :D thanks a lot!