Yii: Naming relations and fields -


i wrote yii code same name db field , relation:

public function relations() {     return array(         'kin' => array(self::belongs_to, 'kin', 'kin'),         'child' => array(self::belongs_to, 'child', 'child'),     ); } 

now understand wrong, because relations "cover" on field attributes , queries $this->child child id not work.

my question: naming scheme suggest db fields , relations?

firstly, i'd avoid naming primary key after table. makes kind of statement difficult read. name primary keys childid or kinid; way it's clear field you're talking about.

i think there no hard , fast rules naming, except use makes sense in context. this, assuming i've understood table structure correctly, i'd inclined go this: table 1: kin primary key: kinid

table 2: child primary key: childid foreign key table kin: fkkinid

then, assuming 1 kin can have many children, relation in kin model becomes:

public function relations() {     return array(         'children' => array(self::has_many, 'child', 'fkkinid'),     ); } 

you can call child model kin using $this->children if you're inside kin model.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -