Access related records CActiveDataProvider

Another most likely newbie question, but I cant get to the related records that I’m fetching with the “with” in the dataprovider.

Here my code:

Controller




$vendors=new MDataProvider('Vendors', array(

			'criteria'=>array(

				'with'=>array(

					'actions'=>array(

						'condition'=>'actions.users_id=:you',

						'params'=>array(':you'=>Yii::app()->user->id),

						'joinType'=>'INNER JOIN'

					)

				),

				'condition'=>'t.status_id=:new',

				'params'=>array(':new'=>Vendors::STATUS_NEW),

			),

			'pagination'=>array(

				'pageSize'=>10

			),

		));

		$vendors->joinAll = true;



View




if($vendors->actions){

		$this->widget('zii.widgets.CListView', array(

	'dataProvider'=>$vendors->actions,

	'itemView'=>'/actions/_view',

	'template'=> '{summary}{sorter}<table width="100">

<thead>

	<tr>

		<th>Who</th>

		<th>What</th>

		<th>To/From</th>

		<th>When</th>

	</tr>

</thead>

<tbody>{items}</tbody>

</table>{pager}'

));			

	}




The error:


Property "MDataProvider.actions" is not defined.

EDIT: $vendors->actions as dataProvider for ClistView

Hmmm, I don’t recognize “MDataProvider”, but assume it’s like the other ones.

Calling the return of your dataprovider “$vendors” is probably a little confusing; I’d instead call it $vendorDataProvider, and you can’t use it to access individual attributes of an individual member, such as $vendors->actions.

What are you trying to accomplish with the if (&#036;vendors-&gt;actions) test?

Hi Steve, Thanks for fast response.

MDataProvider is extending CActiveDataProvider according to another topic here on the forum, to make joins better i think.

I need to list out the actions for each vendor (entered by user) like i would be able to in Vendors::model()->with(‘actions’)->findAll() or whatever, is there a way to do that with the dataprovider?

Thanks again

I’m not 100% clear on exactly which of several possibilities you might mean here: what is the user experience y7ou mean to achieve?

"User clicks on a vendor link, calls a controller/action with that vendor ID, they see the list of actions associated with that one vendor" ?

"User clicks a menu item that lists all vendors along with the actions for each" ?

etc.

I’m sure we can help with whatever one you’re trying to achieve.

Thanks steve, Its the second one i’m trying to achieve. I created a minimal dashboard to show new vendors along with actions that other users have taken (emails, phonecall etc.)

Ok, this will be straightforward - I’ll leave it as a skeleton showing how it works, you can expand it as you need to. There are three parts:




// in your controller


    public function actionVendorActions()

    {

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

            'dataProvider' => new MActiveDataProvider('Vendor')

        ) );

    }


// in your view    protected/views/SOMETHING/vendoractions.php


$this->widget('zii.widgets.CListView', array(

    'dataProvider' => $dataProvider,

    'itemView'     => '_view'

) );




// in the partial view:  protected/views/SOMETHING/_view.php


<div class="view">

    <?php

        // here you do whatever you like to show the vendor and actions

        echo "Vendor name:  " . CHtml::encode($data->name} . "\n";

        echo "Vendor phone: " . CHtml::encode($data->phone) . "\n";


        if ( count($data->actions) == 0 )

        {

            echo "    No actions for his vendor\n";

        }

        else

        {

            echo CHtml::openTag('ul', array() );


            foreach ($data->actions as $action)

            {

                // add what you need here

                echo CHtml::tag('li', array(), "Action: " . CHtml::encode($action->name) );

            }

            echo CHtml::closeTag('ul');

        }

        

    ?>

</div>



The main point is that in your Item View (the last one), you can do whatever you like, and assuming you set up a &#036;vendor-&gt;actions relation in the Vendor model, this should work just fine.

Note: I haven’t tried any of this, so I might be missing a comma or a parenthesis or something. I hope it helps convey the idea.

I tried it and it works, but its not returning any actions, it doesnt throw any errors but actions comes back NULL no matter what i do…???