Filtering view's

hi - in need of some help here, i’m new to the whole web framework thing so this should be a simple question.

i’ve run through Larry Ullman’s Yii Tutorial in getting a basic webapp up and running and tinkered with some of the features like breadcrumbs and the menu’s. I moved on to making my own app.

At this point I’ve created my database and used gii to create the basic model/controller/crud(view) and started to customise some of the views.

To get things started im interested in the following 2 tables

Company - id, name

Staff - id , companyId, name

What I want to do is view a company and then be able to view a staff list based on the company.

If you set up your relations() correctlly (Company HAS_MANY Staff) you can do this:




$company = Company::model()->findXXX('Wahtever you want');

$stf = $company->staff; // staff is the name of the relation in Company model.



After that $stf is an array (0, 1 or more Staff AR objects) containing the staff of the $company you find.

Thanks for you reply - its taken me a little while to get back to this

I ended up with the following code in the controller




$company = Company::model()->findByPK($_GET['id']);

$stf = $company->staffs;

$this->render('staff',array('company' => $company, 'stf' => $stf,));



Worked out I needed the render line otherwise I ended up with a blank page.

the module had the the relation predefined -


'staffs' => array(self::HAS_MANY, 'Staff', 'companyId')

My next issue is that while I can display the company name/id using


<?php echo $company->id ?>

The same fails to work with the $stf - probably becuase its an array as mentioned.

How do I display the data from $stf

Just to follow up - I managed to get the output that I was after. (and it is simple)

I ended up using the ‘CActiveDataProvider’ - and not in the company controller.

So as noted in the first post I used Gii to create the models/controllers/views (and it did a pretty good job)

I added to /protected/controllers/StaffController.php


	

public function actionIndexc()

	{

		$dataProvider=new CActiveDataProvider('Staff', array(

			'criteria'=>array(

				'condition'=>'companyId=:vid',

				'params'=>array(':vid'=>$_GET['id']),

			),		

		));

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

			'dataProvider'=>$dataProvider,

		));

	}



Being new to this and having the code already pregenerated I copied the /staff/index.php and renamed to indexc.php - no additional code changes required, may need to make changes to this later but for now it works.

Gii auto generated the accessRules() in the StaffController.php - make sure to add ‘indexc’ to the access rules otherwise you get access denied

In /views/company/view.php I added the following line to the menu array


array('label'=>'Staff', 'url'=>array('/staff/indexc', 'id'=>$model->id)),

Something I found along the way - Blank pages occour for various reasons the more noteable ones I know off is if you

dont have the $this->render

brackets ( ) - forget to put one in, Have an extra one somewhere

and code that Yii has no idea how to handle such as trying to put $_GET into the condition statement directly