Pass FindAll output to $model

Hey guys. I need your help. I want my cActiveRecord to display all related records as specified in my criteria the problem is the findAll method doesnt seems to work. I did use find() and it only returns the first record here’s my code. Please help thanks :)




        public function actionAdmin()

	{

		$model=new MenuCategory('search');

		$model->unsetAttributes();  // clear any default values


		$this->store_name = Yii::app()->user->getState('store_id');  

		$store=Store::model()->find('id=:id', array(':id'=>$this->store_name));

		$this->store_name = $store->name." - ".$store->address." ".$store->city;

		

		if(isset($_GET['MenuCategory']))

			$model->attributes=$_GET['MenuCategory'];

		

		//get related record

		$Criteria = new CDbCriteria();

		$Criteria->condition = "store_id = $store->id";

		$model = MenuCategory::model()->findAll($Criteria);


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

			'model'=>$model,

		));

	}



this code returns a error while using find() returns 1 row

Additional info: when I count the result it returns the number of related records. i just want to display the related records in the cActiveGrid in the admin page. Thanks again

try CActiveDataProvider

Searching and sorting by related model in CGridView

and do you read the guide : RAR Relational Active Record

hey thanks man :) yeah i tested it in it does filter the records




        public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('name',$this->name,true);

		$criteria->compare('sort',$this->sort);

		$criteria->compare('visible',$this->visible,true);

		$criteria->compare('description',$this->description,true);

		$criteria->compare('store_id',$this->store_id);

		$criteria->condition = "store_id = 10001"; <--- i put the condition here.

		

		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}



One more. i need the value 10001 be dynamic. i have a yii->setState that holds the store Id can i access it in the model.php

Never mind i did this. but not sure if this is the right way to do it.

I passed the store_id from the controller to the cgridview search function.




<div id='list_wrap'>

	<?php $this->widget('zii.widgets.grid.CGridView', array(

		'id'=>'menu-category-grid',

		'pager' => array(

        'header' => '',

		),  

		'dataProvider'=>$model->search($this->store_id),

		'columns'=>array(

			'id',

			'name',

			'sort',

			'description',

			array(

			'name'=>'visible',

			'type'=>'raw',

			'value'=>array($this,'outCheckboxImage'), 

			),

			array(

				'class'=>'CButtonColumn',

				'template'=>'{update} {delete}',

			),

		),

	)); ?>

</div>



Thanks :)

it depends you :lol:

pass it from view or someAction is same strategy :





  // XXXController::actionXXX()

   

   public function actionMySearch(){

           $model = new MyModel('search');

        $model->unsetAttributes(); // clear any default values

        if (isset($_GET['MyModel']))

            $model->attributes = $_GET['MyModel'];

 

        // do it here is also ok

        $dataProvider= $model->search();

          

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

            'dataProvider' => $dataProvider,

            'model' => $model,

        ));

   }



then in your view just use $dataProvider but $model->search() . :D

you can also define a addition attribute for the MyModel class just as your other attributes(but this attribute is not the according table 's field ) and add it to “search” rules . thus you can use it as others . can pass store_id from url ($_GET[‘store_id’]);

any way there may exists not only one method to do somethings .