Display Cgrid Columns In Condition


i have 4 columns: assignment, student, user, cycle

the student exist in a cycle

the user (having a type: admin, assistant or teacher) is responsible on one or more cycle(s)

an assignment is made by a user to a student.

in the views/assignment/admin.php: i need to display the list of the assignments in the cgrid columns based on the cycle’s logged in user.

if a user is responsible of the cycle1 and cycle2, display the student that exist only in the cycle 1 and 2.

this is my code:

public function search()


		// Warning: Please modify the following code to remove attributes that

		// should not be searched.

		$type = Yii::app()->user->getType();

		$criteria=new CDbCriteria;


		$criteria->with = array('student');


                // $type represent the user type

                // if type is assistant or teacher, display the student and the assignment information of the student exists in the same user's cycles.

		if(($type == 'Assistant')or($type == 'Teacher')) {

		        // the cycle of the user

			$usercycles = Cycle::model()->GetCycleOfUser(Yii::app()->user->getID());

			foreach($usercycles as $usercycle){

				$studentCycle = Student::model()->find('Group_Code=\'' . $usercycle['Group_Code'] .'\'');


// $studentCycle is the list of students having the same cycle of the logged in user

			foreach($studentCycle as $cyc){


				$criteria->addSearchCondition('CONCAT('. $cyc->E_Child_Name . ',' . $cyc->E_Family_Name . ')',$this->studentID);








		return new CActiveDataProvider($this, array(




how to write such thing?

can anyone help me?


no response? i really urgently need help


I think you’re thinking wrong. If you want to do this, you should use User model, User controller and User views. This is because in your User model all relationships already exists, then everything will be eassier.

A user have one or more cycles, a cycle one or more students and a student one or more assignments.

Then you have relationships such as:


For example.

Then in your specific User view can show all this data (previously collected in your controller).

Of course you can do it backwards because relationships have also, but always using the relationships to do it.

You don’t have to do this (for example):

$usercycles = Cycle::model()->GetCycleOfUser(Yii::app()->user->getID());

                        foreach($usercycles as $usercycle){

                                $studentCycle = Student::model()->find('Group_Code=\'' . $usercycle['Group_Code'] .'\'');


You should be through relationships.


but in the assignment/admin.php view, what is i am doing is to display the assignment information, controlled by the user’s cycles.

i am displaying the assignment id, the action name … in plus of the student name

but not all the assignment, only the assignment of the student that exists in the user’s cycle.

can you please gives me more info

You fill the CGridview with function "search" from your model "Assigment", right?

Then in your function "search" you have to write the necessary condition to show only logged user assigments (Understanding that the "userID" property exists in Yii::app()->user. You must create it.).

$criteria->compare('userID', Yii::app()->user->userID, false);

You are doing this:




Isn’t the logged user.

And that’s it.