Skip to content

Instantly share code, notes, and snippets.

@pastuhov
Last active August 14, 2023 09:16
Show Gist options
  • Save pastuhov/f8a13d35e2181987da9c to your computer and use it in GitHub Desktop.
Save pastuhov/f8a13d35e2181987da9c to your computer and use it in GitHub Desktop.
Use Master yii2 activerecord trait
<?php
namespace common\models;
trait MasterTrait
{
public static function getDb()
{
$connection = clone \Yii::$app->getDb();
$connection->enableSlaves = false;
return $connection;
}
}
<?php
class User extends ActiveRecord
{
use MasterTrait;
}
/**
* Fetch all users from master server
*/
User::find()->all();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment