Answer the question
In order to leave comments, you need to log in
Difficulties with ActiveRecord -> relations?
I redo the project WITHOUT POSSIBILITY to change structure of tables. I'm learning YII (version 1.1.13) I'm setting up
relationships in models and I'm confused about some pretty simple relationships.
There is a link Country < - Area < - Settlement (Tobizh in each child model there is a link to the parent)
1. For the Country model, the relationship looks like this:
return array(<br>
'areas' => array(self::HAS_MANY, 'Area', 'id_country'),<br>
'areaCount'=>array(self::STAT, 'Area', 'id_country'),<br><br>
//Статический запрос через две таблицы не прокатил (вернее если бы в area, была свяь к settlement а не на оборот)<br>
//'settlementCount'=>array(self::STAT, 'Settlement', 'area(id_country, id_area)'),<br><br>
//id - относится к area<br>
'settlements'=>array(self::HAS_MANY, 'Settlement', array('id'=>'id_area'), 'through'=>'areas'),<br>
);<br>
return array(<br>
//'country' => ???,<br>
//'area' => ???,<br>
'settlement' => array(self::BELONGS_TO, 'Settlement', 'id_settlement'), /<br>
);<br>
Answer the question
In order to leave comments, you need to log in
I will partially answer myself to question 2.1:
Most recently, Yii 1.1.14 RC was released, which declared support for BELONGS_TO for the through option and indeed tests showed that for Area you can set a relationship like this:
public function relations()
{
'settlement' => array(self::BELONGS_TO, 'Settlement', 'id_settlement'),
'area' => array(self::BELONGS_TO, 'Area', array('id_area'=>'id'), 'through'=>'settlement'),
//'country' => array(self::BELONGS_TO, 'Country', array('id_country'=>'id'), 'through'=>'area'),
}
}
Organized settlementCount like this:
class Country 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(
'areas' => array(self::HAS_MANY, 'Area', 'id_country'),
'settlementCount'=>array(self::STAT, 'Area', 'id_country',
'join' => 'INNER JOIN `Settlement` ON `Settlement`.id_area = `t`.id',
),
'settlements'=>array(self::HAS_MANY, 'Settlement', array('id'=>'id_area'), 'through'=>'areas'),
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question