Hello,
I have two projects running Yii2:
Project X connecting with database X
Project Y connecting with database Y.
But there’s some common data between both, like the User table.
Today if I want to add a user, I have to add it on both tables.
How can I set up project Y to use the User table from database X ?
Here’s the code from my model:
class User extends ActiveRecord implements IdentityInterface {
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
const ROLE_OPERATOR = 1;
const ROLE_ADMIN = 10;
const ROLE_SUPERADMIN = 100;
public $department;
/**
* @inheritdoc
*/
public static function tableName() {
return '{{%user}}';
}
public function behaviors() {
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'createdAtDtm',
'updatedAtAttribute' => 'updatedAtDtm',
'value' => new Expression('GETDATE()'),
]
];
}
/**
* @inheritdoc
*/
public function rules() {
return [
[['username', 'email'], 'required'],
[['user_id', 'fullName', 'department_id'], 'string'],
[['createdAtDtm', 'updatedAtDtm', 'fullName', 'department'], 'safe'],
[['status', 'role'], 'integer'],
[['username', 'email', 'auth_key'], 'string', 'max' => 255],
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
];
}
/**
* @inheritdoc
*/
public function attributeLabels() {
return [
'username' => 'Username',
'email' => 'Email',
'user_id' => 'User ',
'createdAtDtm' => 'Created At',
'updatedAtDtm' => 'Updated At',
'status' => 'Status',
'auth_key' => 'Auth Key',
'role' => 'Role',
'fullName' => 'Full name',
'department_id' => 'Department '
];
}
public static function getAllUsersDropdownList() {
return ArrayHelper::map(static::find()->orderBy('fullName')->all(), 'user_id', 'fullName');
}
public static function getAllUsersDropdownListGrid() {
return ArrayHelper::map(static::find()->orderBy('fullName')->all(), 'fullName', 'fullName');
}
/**
* @inheritdoc
*/
public static function findIdentity($id) {
return static::findOne(['user_id' => $id, 'status' => self::STATUS_ACTIVE]);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null) {
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username) {
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
}
/**
* @inheritdoc
*/
public function getId() {
return $this->getPrimaryKey();
}
public function getFullName() {
return $this->fullName;
}
...............