S
S
Serrrgio2015-12-09 08:14:13
Yii
Serrrgio, 2015-12-09 08:14:13

How to override find()->all()?

There is a Document model . There are two models DocumentIn and DocumentOut , extending the first one. The records in the table are distinguished by the kind field .

class DocumentIn extends Document
{
    ...
    public static function find()
    {
        Yii::warning(get_called_class());
        return new DocumentInQuery(get_called_class());
    }
    ...
}
class DocumentInQuery extends \yii\db\ActiveQuery
{
    public function active()
    {
        $this->andWhere('=0');
        return $this;
    }
    public function all($db = null)
    {
        return parent::all($db);
    }
    public function one($db = null)
    {
        return parent::one($db);
    }
}

All incoming documents can be selected like this: $docs = DocumentIn::find()->active()->all() .
That is, you have to manually add an active() call to DocumentsInSearch when creating $dataProvider and wherever you want to select all records of this type.
The question is how to make DocumentIn::find()->all() return all records with ' =0 '?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2015-12-09
@Serrrgio

public function all($db = null)
{
    $this->active();
    return parent::all($db);
}

or so
public function init()
{
    parent::init();
    $this->active();
}

In the second case, the sum(), count() and other methods will also work correctly.

A
Andrey, 2015-12-09
@VladimirAndreev

DocumentIn::find() returns an object of the ActiveQuery class, and override the all... method in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question