private function addOwnersRelations($org_id) { $owners = array_unique($this->owners); foreach ($owners as $owner) { $relation = new UsersOrganizations(); $relation->isNewRecord = true; $relation->user_id = $owner; $relation->organization_id = $org_id; $relation->save(); } }
public static function isOrgOwner($user_id, $org_id) { return self::find()->from(self::tableName() . ' u')->innerJoin(UsersOrganizations::tableName() . ' uo', 'u.id = uo.user_id')->innerJoin(Organizations::tableName() . ' o', 'o.id = uo.organization_id')->where(['o.id' => $org_id, 'u.id' => $user_id])->exists(); }
/** * @return \yii\db\ActiveQuery */ public function getUsersOrganizations() { return $this->hasMany(UsersOrganizations::className(), ['user_id' => 'id']); }
public static function getByOrg($org_id) { $sql = 'SELECT u.username ' . ' FROM ' . self::tableName() . ' u ' . ' JOIN ' . UsersOrganizations::tableName() . ' uo ' . ' ON u.id = uo.user_id ' . ' JOIN ' . Organizations::tableName() . ' o ' . ' ON o.id = uo.organization_id ' . ' WHERE o.id = :org_id'; return self::findBySql($sql, [':org_id' => $org_id])->asArray()->all(); self::find()->select('u.username')->from(self::tableName() . ' u')->innerJoin(UsersOrganizations::tableName() . ' uo', 'u.id = uo.user_id')->innerJoin(Organizations::tableName() . ' o', 'o.id = uo.organization_id')->where(['o.id' => $org_id])->asArray()->all(); }
public static function getByUserId($user_id) { $sql = 'SELECT o.id, o.nice_name ' . ' FROM ' . self::tableName() . ' o ' . ' JOIN ' . UsersOrganizations::tableName() . ' uo ' . ' ON o.id = uo.organization_id ' . ' JOIN ' . Users::tableName() . ' u ' . ' ON u.id = uo.user_id ' . ' WHERE u.id = :user_id' . ' ORDER BY o.nice_name '; return self::findBySql($sql, [':user_id' => $user_id])->asArray()->all(); }