• Как сделать связь между 3 таблицами?

    slo_nik
    @slo_nik Куратор тега Yii
    Добрый вечер.
    Для начала начните с того, что проверяйте, что передаёте для foreach().
    Что-то подобное:
    print_r($car->components);
    Это не правильно
    $component->getDamages($car->id)
    Если Вы уж передаёте параметр, то сначала передайте, получите результат, а потом уже передавайте это в foreahc()
    $component = $component->getDamages($car->id)
    foreach($component as $damages){
     ****************
    }

    Если Вы обращаетесь к связи так
    $component->getDamages($car->id)
    то Вы получаете объект ActiveQuery, но Вы не получите данные.
    Вот пример. У меня есть две модели, которые связаны через связь getCity()
    public function getCity()
        {
            return $this->hasOne(Cities::className(), ['id' => 'city_id']);
        }

    Вот, что я сделал, посмотрите результат и сравните.
    $test = new RoutesCities(['city_id' => 1]);
    echo "<pre>";
    print_r($test->getCity());
    print_r($test->city);

    Результат 1

    yii\db\ActiveQuery Object
    (
    [sql] =>
    [on] =>
    [joinWith] =>
    [select] =>
    [selectOption] =>
    [distinct] =>
    [from] =>
    [groupBy] =>
    [join] =>
    [having] =>
    [union] =>
    [params] => Array
    (
    )

    [_events:yii\base\Component:private] => Array
    (
    )

    [_behaviors:yii\base\Component:private] => Array
    (
    )

    [where] =>
    [limit] =>
    [offset] =>
    [orderBy] =>
    [indexBy] =>
    [emulateExecution] =>
    [modelClass] => app\modules\routes\models\Cities
    [with] =>
    [asArray] =>
    [multiple] =>
    [primaryModel] => app\modules\routes\models\RoutesCities Object
    (
    [from_rout] =>
    [to_rout] =>
    [tariff] =>
    [arr_route] => Array
    (
    )

    [_attributes:yii\db\BaseActiveRecord:private] => Array
    (
    [city_id] => 1
    )

    [_oldAttributes:yii\db\BaseActiveRecord:private] =>
    [_related:yii\db\BaseActiveRecord:private] => Array
    (
    )

    [_errors:yii\base\Model:private] =>
    [_validators:yii\base\Model:private] =>
    [_scenario:yii\base\Model:private] => default
    [_events:yii\base\Component:private] => Array
    (
    )

    [_behaviors:yii\base\Component:private] => Array
    (
    )

    )

    [link] => Array
    (
    [id] => city_id
    )

    [via] =>
    [inverseOf] =>
    )
    Результат 2

    app\modules\routes\models\Cities Object
    (
    [_attributes:yii\db\BaseActiveRecord:private] => Array
    (
    [id] => 1
    [title] => Иркутск
    [created_at] => 1506275820
    [updated_at] => 1506692111
    [status] => 1
    [address] =>
    [phone] =>
    [time_work] =>
    )

    [_oldAttributes:yii\db\BaseActiveRecord:private] => Array
    (
    [id] => 1
    [title] => Иркутск
    [created_at] => 1506275820
    [updated_at] => 1506692111
    [status] => 1
    [address] =>
    [phone] =>
    [time_work] =>
    )

    [_related:yii\db\BaseActiveRecord:private] => Array
    (
    )

    [_errors:yii\base\Model:private] =>
    [_validators:yii\base\Model:private] =>
    [_scenario:yii\base\Model:private] => default
    [_events:yii\base\Component:private] => Array
    (
    [beforeInsert] => Array
    (
    [0] => Array
    (
    [0] => Array
    (
    [0] => yii\behaviors\TimestampBehavior Object
    (
    [createdAtAttribute] => created_at
    [updatedAtAttribute] => updated_at
    [value] =>
    [attributes] => Array
    (
    [beforeInsert] => Array
    (
    [0] => created_at
    [1] => updated_at
    )

    [beforeUpdate] => updated_at
    )

    [skipUpdateOnClean] => 1
    [owner] => app\modules\routes\models\Cities Object
    *RECURSION*
    )

    [1] => evaluateAttributes
    )

    [1] =>
    )

    )

    [beforeUpdate] => Array
    (
    [0] => Array
    (
    [0] => Array
    (
    [0] => yii\behaviors\TimestampBehavior Object
    (
    [createdAtAttribute] => created_at
    [updatedAtAttribute] => updated_at
    [value] =>
    [attributes] => Array
    (
    [beforeInsert] => Array
    (
    [0] => created_at
    [1] => updated_at
    )

    [beforeUpdate] => updated_at
    )

    [skipUpdateOnClean] => 1
    [owner] => app\modules\routes\models\Cities Object
    *RECURSION*
    )

    [1] => evaluateAttributes
    )

    [1] =>
    )

    )

    )

    [_behaviors:yii\base\Component:private] => Array
    (
    [0] => yii\behaviors\TimestampBehavior Object
    (
    [createdAtAttribute] => created_at
    [updatedAtAttribute] => updated_at
    [value] =>
    [attributes] => Array
    (
    [beforeInsert] => Array
    (
    [0] => created_at
    [1] => updated_at
    )

    [beforeUpdate] => updated_at
    )

    [skipUpdateOnClean] => 1
    [owner] => app\modules\routes\models\Cities Object
    *RECURSION*
    )

    )

    )

    Ответ написан