public function relations()

Hi guys,

if i set the public function relations as below, am i also needed to set foreign key at User database?? or i just needed to

declare at public function relations() only??

class user extends CActiveRecord

 public function relations()


        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.

        return array(

            'comp_id' => array(self::BELONGS_TO, 'company', 'company_id'),

            'user_id' => array(self::HAS_ONE, 'company', 'user_id'),




You don’t need to set relations in DB. Tables even may not support relations (e.g. MyISAM in MySQL), but if you have relations in DB, Gii can automatically generate class relations.

Hi andy_s,

You mean even without make relation in DB, i still can show 2 different table info in as long i declare public function relations()?? Currently i fail to set relation in DB, i using SQLyog-MySQL GUI to store the DB.


Yes, why don’t you just try it? :)