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.

<_<

Confused!

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 PG.id 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

IN TABLE => PG

id | value


1 | group_1

2 | group_2

3 | group_3

AND IN TABLE => P

pg_id | p_name


1 | param_1

3 | param_2

Then Result should be

PG.id | 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

$emp=Employee::model()->with(‘department’)->findAll();

$deplist=CHtml::listData($emp,‘id’,‘firstName’);

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.