I
I
ImPuuLsE2015-01-20 14:57:35
Yii
ImPuuLsE, 2015-01-20 14:57:35

How to organize scope in Yii2?

public function withFullUrl($url)
    {
        $this->getDbCriteria()->mergeWith(array(
            'condition'=>'full_url=:url',
            'params'=>array(':url'=>$url)
        ));

        return $this;
    }

This is how the method looked like in the Yii1 model, tell me about Yii2 please...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Domrachev, 2015-01-20
@ImPuuLsE

Start reading the docs already, this is the basis of the framework in general.
www.yiiframework.com/doc-2.0/guide-db-active-recor...

class Model extends ActiveRecord {
    public static function find() {
        return new MyQuery(get_called_class());
    }
}

class MyQuery extends ActiveQuery {
    public function withFullUrl($url) {
        $this->andWhere(['full_url' => $url]);
        return $this;
    }
}

Now the model and query are separated, so everything related to queries and the formation of this very query is in a separate class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question