Yii framework one to many tutorial


I am very new to PHP frameworks in general and MVC framework in particular. After several hours of googling, I came to a conclusion that Yii is the best (for me, at least). I am writing this post so that it can serve as a reference for me as well as others.

I closely followed the blog tutorial and use it  as a basis for my application. My application has a Contact Management system, which is very similar to the Blog. Just like the Blog has the Post and the Comment, it has Company and Contact. Let us move on with the Tutorial.

In order to have a One Company to Many Contacts view for the Company, make following changes, after the model *, crud Company and  crud Comment execution.

  1. In the model/Company.php file
  2. add the following code to the relationship function.


    public function relations()
    {
    return array(
    'contacts' => array(self::HAS_MANY, 'Contact', 'contact_company_id', 'order'=>'contact_id ASC'),
    'contactCount' => array(self::STAT, 'Contact', 'contact_company_id'),
    );
    }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s