M
M
maks2807952016-03-16 15:28:43
Yii
maks280795, 2016-03-16 15:28:43

How to make a relation without related tables?

Good afternoon, I have a relation in the Items model:

public function getPlan()
{
    return $this->hasOne(Plans::className(), ['id' => 'value'])
        ->viaTable(ItemsPropsValues::tableName(), ['item_id' => 'id'], function ($query) {
            /** @var ActiveQuery $query */
            $query->where(['type_id' => $this->category->itemsPropsTypes['plan_id']->id]);
        });
}

It works, but since the properties of the model itself were used via $this, much-needed lazy loading is not supported.
I can’t just take and declare a foreign key, Items is a product that has different sets of properties (ItemsPropsTypes) depending on which category is assigned to it.
How do I edit the finished relation to get a relation that supports lazy loading?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maks280795, 2016-03-16
@maks280795

The structure is something like this:
Items (id, category_id)
ItemsPropsTypes(id, category_id)
ItemsPropsValues(item_id, type_id, value)
Plans(id)
I need to get the Plans object whose id is stored in the ItemsPropsValues ​​table in the value attribute, with the condition that type_id == 4.
This connection is necessary in the Items model, you can get it by FK Items_id- ItemsPropsValues_item_id.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question