S
S
sdgs4s4 .2018-04-24 20:30:21
Yii
sdgs4s4 ., 2018-04-24 20:30:21

Yii2 -> work 2 or 100 sites from one directory?

There was a need to organize the work of several sites from one directory
In the database, an identifier was added to each table in the form of a domain, output to the controller through

->where(['status' => '1','domain' => preg_replace("/^www./","",$_SERVER['HTTP_HOST'])])

In principle, the task is performed, but...
...maybe there is an opportunity to simplify the task, maybe there is some kind of magic plop that can be written in the configs and not set conditions for each controller?
I'm not kidding, sorry

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2018-04-24
@Encoderast

Create scope :

class MyActiveQuery extends \yii\db\ActiveQuery
{
    public function init()
    {
        $modelClass = $this->modelClass;
        $tableName = $modelClass::tableName();
        $domainName = preg_replace("/^www./","",$_SERVER['HTTP_HOST']);
        $this->andWhere([$tableName.'.domain' => $domainName]);
        parent::init();
    }
}

Use it in your model:
class MyActiveRecord extends \yii\db\ActiveRecord
{
    public static function find()
    {
        return new MyActiveQuery(get_called_class());
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question