Skip to content

Instantly share code, notes, and snippets.

@VictorFursa
Created October 9, 2017 21:55
Show Gist options
  • Save VictorFursa/cae3e38dde7f415be6e71a4aab740d54 to your computer and use it in GitHub Desktop.
Save VictorFursa/cae3e38dde7f415be6e71a4aab740d54 to your computer and use it in GitHub Desktop.
позднее статическое связывание
<?php
namespace app\modules\admin\models;
use yii\db\ActiveRecord;
class Active extends ActiveRecord
{
public static function getDb()
{
return \Yii::$app->db2;
}
}
<?php
/** app/config */
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=active',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
];
<?php
/** MODEL TYPE */
namespace app\modules\admin\models;
class Type extends Active
{
...
}
<?php
namespace app\modules\admin\controllers;
use app\modules\admin\models\Type;
public function actionCreate()
{
$model = new Type();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
<?php
/** app/web */
$db = require __DIR__ . '/db.php';
$db2 = require __DIR__ . '/db2.php';
'components' => [
'db' => $db,
'db2' => $db2,
];
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment