S
S
Sergey2016-02-23 22:02:36
Yii
Sergey, 2016-02-23 22:02:36

How to add a condition on links?

There is a Price class, it is tied to the Product class, in the Product model. In the following way

public function getPrice()
    {
        return $this->hasMany(Price::className(), ['product_id' => 'id']);

    }

further called like this $price = $model->price; There was also a need to make a selection by one condition in the price table there is a countryid column, it contains country id's there is a country table with information about countries. Through tricky dances with a tambourine, I get the id of the country from which the user comes. But here's how I can now select by the countryid column, I'll never know, tell me please.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Natarov, 2016-02-23
@HanDroid

See, you can go two ways. Make the selection condition single, where you need it.

function ($someCountry = 'Ukraine')
{
$product = Product::findOne(123);
//Запрос ниже выполняться может сколько угодно раз в отличии от $price = $product ->Price();
$price = $product ->getPrice()
    ->where(['country =:someCountry', [':someCountry' => $someCountry])
    ->orderBy('id')
    ->all();
}

Or initially make a connection with a certain condition.
class Product extends ActiveRecord
{
    public function getBigPrices($someCountry)
    {
        return $this->hasMany(Price::className(), ['product_id' => 'id'])
            ->where('country > :someCountry', [':someCountry' => $someCountry])
            ->orderBy('id');
    }
}

Only in the first option, I think there is a simpler Where something like
$query->where(['country' => USA]);

S
Sergey, 2016-02-24
@Serggalas

$model->getPrice()->andWhere(['countryid' => $country->id])->one() got out of the way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question