Yii Model Relation Join (HAS_ONE)
Asked Answered
D

1

1

I want a basic syntax of relation and their parameters

i need these clarification

  • Difference Between BELONGS_TO and HAS_ONE ?
  • I want to use the relation on CGridView (That relation refers the another table element as a Foreign key ) For Eg I have Three tables
  • Messenger
  • UserLogin
  • Userprofile

I am in Messenger Admin Grid I have Primary key of UserLogin table That userlogin have userprofile Primary key Now i want to access the userProfile fields in my grid

i already try the (through) in relation like

its in Messenger model

    'relationeg'=>array(self::HAS_ONE,'Userprofile',array('user_id'=>'id'),'through'=>'user'),
'user'=>array(self::BELONGS_TO, 'UserLogin','user_id'),

i access this on my grid

    .....
    ....
array(
    'name'=>'message_by',
    'value'=>'$data->relationeg->username'
),
....
...

But i got the wrong data's that are totally differ by the key of base table record

i have doubt on this line

'relationeg'=>array(self::HAS_ONE,'Userprofile',array('user_id'=>'id'),'through'=>'user'),

in this self::HAS_ONE means? when i add the where condition of the join query how will add the condition on this

Advance thanks

Your answer welcome

Darla answered 6/9, 2013 at 13:3 Comment(3)
This is documented in The Definitive Guide to Yii - Relational Active Record. You can enable the query logging and see what queries get generated for each relation type.Sail
about relations you can check this wiki: has_one vs belongs_toCochard
Thanks Dcoder and Adler i resolve this . Thanks for your responseDarla
D
1

KEY POINT : A BELONGS_TO relation says that a field in this model points to the primary key in another model; in this case, the current model owns the linking field.

KEY POINT : A HAS_ONE relation says that some other model has a linking field pointing to this model's primary key; in this case, the related model owns the linking field.

We can thik that a PARENT table will be the one that doesn't have a foreign key, and a CHILD table as the one who "depends" on the parent table, that is, it has a foreign key.

Given that, a CHILD BELONGS_TO a PARENT and a PARENT HAS_ONE CHILD.

The answer is here

Darla answered 7/5, 2014 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.