6. The Advanced Stuff
6.4. Relations
6.3. Named Scopes
« Previous
6.5. Data Provider Object
Next »

6.4. Relations

AuthorDariusz Górecki

HAS_ONE relation

// just define method in yours model class (assume we have client collection, and address collection in client model
public function address()
{
    return Address::model()->findByAttributes(array('attribute_with_client_id'=>$this->primaryKey()));
}

BELONGS_TO relation

// define in address model
public function client()
{
    return Client::model()->findByPk($this->attribute_with_client_id);
}

HAS_MANY relation

 
 
// assume we have clients and orders collection
// define in client:
public function orders()
{
    return Client::model()->findAllByAttributes(array('client_id'=>$this->primaryKey()));
}

MANY_MANY relation

As simple as defining reverse HAS_MANY relation in orders model. You can view the Example models for details.

6.4. Relations
6. The Advanced Stuff
« Previous
6.3. Named Scopes
Next »
6.5. Data Provider Object