How to find values that referenced by FK

Hey All,

I want to know, is this possible to find only referenced values from child table.



I show you what I want actually.

I have two table, one is PG and second is P.

P has a field pg_id that is referenced by FK on PG (id).

Now how to find P fields that has same value of in P.pg_id from PG model class if class has one relation as

class PG extends CActiveRecord {


	public function relations() {

		return array(

    		'p' => array(self::HAS_MANY, 'P', 'id'),





For example if both table having records as


id | value

1 | group_1

2 | group_2

3 | group_3


pg_id | p_name

1 | param_1

3 | param_2

Then Result should be | PG. value | P.p_name

1 | group_1 | param_1

3 | group_3 | param_2

I hope you will understand

Thanks in Advance

I will assume you have defined your relation correctly!

then try

$model = P:model()->with('p')->findAll()

Hi bettor

I try to apply this to clases Department and Employee of Larry Ulman’s tutorial.

Here is the code which is in views/department/view.php



echo CHtml::dropDownList(‘emp’,’’,$deplist,array (‘empty’ => ‘Select an employee’)); ?>

My aim was to show the employee from the current department (say id=#1) as a select box.

This is for learning purposes only.

But all the employee shows up.

I have thought that this piece of code magically may bring me the employee from this particular department.

Now my question is should I use other methods like find()… to get those employee?

Or yii does this magically somehow?

I am now learning the framework and I see that those relations between tables do some magic.