Jethik
Jethik

Reputation: 1866

how to make relation between two foreign key values of two tables in Yii

I have the database just like this

==== cis_policy_registration====
id
policy_id
email_id
policy_start_date


==== cis_policy_family_details===
id
policy_id
is_main_applicant
firstname
lastname
gender

Now how to make the relation between models with the policy_id (both tables), I want to take firstname and lastname in registration model and check is main applicant

that must list in CGridView

who can solve this problem

thanks in advance

Upvotes: 1

Views: 712

Answers (1)

BornToDrink
BornToDrink

Reputation: 398

The relation between these two tables should be handled from the "main" model (Policy), so in Policy model class you should have:

'policy_reg' => array(self::HAS_ONE, 'PolicyRegistration', 'policy_id'),
'policy_details' => array(self::HAS_ONE, 'PolicyDetails', 'policy_id'),

And then:

$policy = Policy::model()->with(array('policy_details'))->findByPk($pk);
$policy->policy_details->is_main_applicant;
...

And in CGridView you can print the relational value like this (after sending CActiveDataProvider object from Policy model):

'policy_details.firstname'

or

array(
    'name'=>'Firstname',
    'value'=>'$data->policy_details->firstname',
),

Upvotes: 2

Related Questions