S
S
salopot2013-08-10 13:59:17
Yii
salopot, 2013-08-10 13:59:17

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>

Questions:
1.1. Is the settlements link configured correctly (it works, but there is a suspicion that it can be configured without an intermediate link)?
1.2 How do you set up settlementCount?
2. There is a Message model where only the link to id_settlement is specified.
return array(<br>
            //'country' => ???,<br>
            //'area' => ???,<br>
            'settlement' => array(self::BELONGS_TO, 'Settlement', 'id_settlement'), /<br>
);<br>

2.1 How to set up communication on Area?
2.2 How to set up communication on Country?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
salopot, 2013-08-12
@salopot

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'),
        }
}

Relation for Area works fine with Message::model()->with(array('settlement', 'area'))->…
Setting relation for country via area didn't work, call chains are not supported…
And it's not always necessary the same intermediate relation, although it certainly will not play the weather.
In general, if you think about it, then the relationships I need are more like MANY_MANY, except that it is not the intermediate table itself that refers, but to it.
Perhaps the esteemed SamDark will clarify the situation a little.

S
salopot, 2013-08-14
@salopot

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'),
...

Although some residue of incorrectness of the decision remained ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question