CActiveDataProvider and with -> conditon

I have searched for a while now and the docs aren’t really clear to me and i couldn’t find any example either.

Using a simple relation on a class called World




        public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'players' => array(self::HAS_MANY, 'Player', 'worldid'),

			'users'=>array(self::MANY_MANY, 'User',

                'Player(userid, worldid)'),

		);

	}



then when i construct a model everything still works:




$myworlds=World::model()->with(array(

    			'users'=>array('condition'=>'userid='.Yii::app()->user->getId()),

			))->together()->findAll();



but now i wanted to do that in CActiveDataProvider

i tried this code, but it didn’t go as expected, it trew me this error:

relation "userid=1" is not defined in active record class "World".




		$myworlds=new CActiveDataProvider('World', array(

			    'criteria'=>array(

			        'with'=>array(

			        	'condition'=>'userid='.Yii::app()->user->getId()

			        ),

			    ),

			    'pagination'=>array(

			        'pageSize'=>10,

			    ),

			));



Can any1 explain me how it works ?

Hi Mukke,

The good thing about defining relations is that the class records them and can generate the SQL to join tables itself using them Once you’ve defined a relation, all you need to do is to call it by name in your ‘with’ array.

I.E.:




    $myworlds=new CActiveDataProvider('World', array(

        'criteria'=>array(

            'with'=>array('users','players'),

            'together'=>true,

        ),

        'pagination'=>array(

            'pageSize'=>10,

        ),

    ));



Similarly, your myworlds model array could be constructed like so:


    $myworlds = World::model()->with('users','players')->together()->findAll();

i’m using 1.1.3

updating to 1.1.4 now since CDbCriteria together attribute requires 1.1.4

it wasn’t the with giving the problem, but the condition

i guess this is because i didn’t join the tables

will update and tell you the results

Well, Together is not actually required. I included it as your working code seemed to.

It merely changes how the content is loaded, which could potentially improve performance. It’s not required, however, and might not be optimal depending on the query. Using ‘with’ isn’t strictly required either, it just tells Yii to load all of the related records in one go… If you try and access a related record that hasn’t been loaded, Yii will load it lazily, one record at a time.

Its not perfect, but unless your tables are massive, these optimisations are unlikely to matter. If your tables are massive then I’d suggest benchmarking before assuming that any given option should be used… Once memory limitations come into play servers can grind to a halt.

Also, is there a particular reason you used a many-many relationship for Users? Did you know you could define a relation in Player and load it using with?

yup works like a train

that’s so awesome, wasted 3h looking for this, and i just needed to update

Because i have Users who are unique

and i have mutiple worlds

users can join mutiple worlds and worlds can have multiple users so is MANY_MANY

but since each user can have different settings in each world and i needed to create a relation table for the MANY_MANY relation anyway i made it Player

and i store those settings in Player aswell, saves me a table

hello,

i have apply above code that defined.

but it generate error

CDbException

Relation "date" is not defined in active record class "Events". (D:\Wu\xampp\htdocs\yii\framework\db\ar\CActiveFinder.php:214)

#0 D:\Wu\xampp\htdocs\yii\framework\db\ar\CActiveFinder.php(308): CActiveFinder->buildJoinTree(Object(CJoinElement), ‘date’, Array)

#1 D:\Wu\xampp\htdocs\yii\framework\db\ar\CActiveFinder.php(51): CActiveFinder->buildJoinTree(Object(CJoinElement), Array)

#2 D:\Wu\xampp\htdocs\yii\framework\db\ar\CActiveRecord.php(1260): CActiveFinder->__construct(Object(Events), Array)

#3 D:\Wu\xampp\htdocs\yii\framework\db\ar\CActiveRecord.php(1380): CActiveRecord->query(Object(CDbCriteria), true)

#4 D:\Wu\xampp\htdocs\sattavis\protected\controllers\EventsController.php(134): CActiveRecord->findAll()

#5 D:\Wu\xampp\htdocs\yii\framework\web\actions\CInlineAction.php(50): EventsController->actionIndex()

#6 D:\Wu\xampp\htdocs\yii\framework\web\CController.php(300): CInlineAction->runWithParams(Array)

#7 D:\Wu\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(133): CController->runAction(Object(CInlineAction))

#8 D:\Wu\xampp\htdocs\yii\framework\web\filters\CFilter.php(41): CFilterChain->run()

#9 D:\Wu\xampp\htdocs\yii\framework\web\CController.php(1122): CFilter->filter(Object(CFilterChain))

#10 D:\Wu\xampp\htdocs\yii\framework\web\filters\CInlineFilter.php(59): CController->filterAccessControl(Object(CFilterChain))

#11 D:\Wu\xampp\htdocs\yii\framework\web\filters\CFilterChain.php(130): CInlineFilter->filter(Object(CFilterChain))

#12 D:\Wu\xampp\htdocs\yii\framework\web\CController.php(283): CFilterChain->run()

#13 D:\Wu\xampp\htdocs\yii\framework\web\CController.php(257): CController->runActionWithFilters(Object(CInlineAction), Array)

#14 D:\Wu\xampp\htdocs\yii\framework\web\CWebApplication.php(328): CController->run(’’)

#15 D:\Wu\xampp\htdocs\yii\framework\web\CWebApplication.php(121): CWebApplication->runController(‘events’)

#16 D:\Wu\xampp\htdocs\yii\framework\base\CApplication.php(155): CWebApplication->processRequest()

#17 D:\Wu\xampp\htdocs\sattavis\index.php(13): CApplication->run()

#18 {main}

my code is like…

$dataProvider=Events::model()->with(array(

                    'date'=>array('condition'=>'date='.$date1),


                    ))->together()->findAll();

it generate error

please give response as soon as possible.

thank you

Hardik Chvda

(PHP Developer in Rajkot)

Did you define date in the relations() method of the class Events?

/Tommy