Need some help with accessing Active Records.

Hi everyone, I need some help to access the data retrieve from Active Records.

What I want to do is,

  1. check login

  2. retrieve userID

  3. Check a Many to Many table(userReports model) between userID and ReportID to see which reports this user is authorized to view

  4. Each report has parameters(parameters model)

So from the Users model, I have added a many many relation with userReports, and from reports, it has a Has Many relation with parameters.

Firstly I'll present the beautiful models.



class reports extends CActiveRecord


....


public function relations()


	{


		return array(


			'userList'=>array(self::MANY_MANY, 'Users', 'userreports(userID,reportID)'),


			'parameterList'=>array(self::HAS_MANY, 'Parameters', 'reportID'),


		);


	}





class parameters extends CActiveRecord


...


	public function relations()


	{


		return array(


			'report'=>array(self::BELONGS_TO, 'Reports', 'reportID')


		);


	}





In reportsController





$userID=user()->getState('userID');


		


$criteria=new CDbCriteria;


$criteria->condition='userID='.$userID;


$form = new ParameterForm;


$users=Users::model()->with(


'reportList',


'reportList.parameterList'


)->findAll($criteria);


When I print_r($users) I can see that all the information has been retrived from the database it's in one big array, but I'm stuck on how I should access, the columns from reports and also parameters?

I'll also need to perform a loop on the parameters to generate the appropriate fields.

Can anyone point me in the right directions? Thanks a lot!

Try this, IIRC it should work.



<?php foreach($user-reportList as $n=>$report): ?>


  <?php foreach($report->parameterList as $m=>$param): ?>


...


      <td><?php echo CHtml::encode($report->someattribute); ?></td>


      <td><?php echo CHtml::encode($param->someattribute); ?></td>


...


  <?php endforeach; ?>


<?php endforeach; ?>


/Tommy

Thanks Tommy,

Got it now