Yii Framework Forum: Pass FindAll output to $model - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Pass FindAll output to $model Rate Topic: -----

#1 User is offline   jiann 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 12-July 12

Posted 12 August 2012 - 02:03 AM

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
0

#2 User is offline   jiann 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 12-July 12

Posted 12 August 2012 - 02:48 AM

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
0

#3 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 586
  • Joined: 27-December 10
  • Location:china

Posted 12 August 2012 - 03:14 AM

try CActiveDataProvider
Searching and sorting by related model in CGridView

and do you read the guide : RAR Relational Active Record
0

#4 User is offline   jiann 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 12-July 12

Posted 12 August 2012 - 04:59 AM

View Postyiqing95, on 12 August 2012 - 03:14 AM, said:



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
0

#5 User is offline   jiann 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 12-July 12

Posted 12 August 2012 - 05:22 AM

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 :)
0

#6 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 586
  • Joined: 27-December 10
  • Location:china

Posted 13 August 2012 - 05:29 PM

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 .
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users