Skip to content

Commit

Permalink
fix: User Class
Browse files Browse the repository at this point in the history
  • Loading branch information
forecho committed Aug 27, 2021
1 parent 5c02bb1 commit fa91bbc
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/models/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace yiier\notification\models;

use common\models\User;
use Yii;
use yii\db\ActiveRecord;
use yii\db\Exception;

/**
Expand Down Expand Up @@ -113,17 +113,18 @@ public function beforeSave($insert)
* @param array $condition
* @return int
*/
public static function readAll($condition = [])
public static function readAll(array $condition = []): int
{
return self::updateAll(['status' => self::STATUS_READ], array_merge(['user_id' => Yii::$app->user->id], $condition));
return self::updateAll(['status' => self::STATUS_READ],
array_merge(['user_id' => Yii::$app->user->id], $condition));
}


/**
* @param $id
* @return int
*/
public static function read($id)
public static function read($id): int
{
return self::updateAll(['status' => self::STATUS_READ], ['user_id' => Yii::$app->user->id, 'id' => $id]);
}
Expand All @@ -133,7 +134,7 @@ public static function read($id)
* @param array $condition
* @return int
*/
public static function delAll($condition = [])
public static function delAll(array $condition = []): int
{
return self::deleteAll(array_merge(['user_id' => Yii::$app->user->id], $condition));
}
Expand All @@ -143,15 +144,15 @@ public static function delAll($condition = [])
* @param $id
* @return int
*/
public static function del($id)
public static function del($id): int
{
return self::deleteAll(['user_id' => Yii::$app->user->id, 'id' => $id]);
}

/**
* @return int
*/
public static function unreadCount()
public static function unreadCount(): int
{
return self::find()->where(['user_id' => Yii::$app->user->id, 'status' => self::STATUS_UNREAD])->count('id');
}
Expand All @@ -164,7 +165,7 @@ public static function unreadCount()
* @param string $userNotification Optional user Notification field
* @throws Exception
*/
public static function create($type, $toUserId, $content, $params = [], $userNotification = '')
public static function create($type, $toUserId, $content, array $params = [], string $userNotification = '')
{
$model = new self();
$model->setAttributes(array_merge([
Expand All @@ -174,7 +175,9 @@ public static function create($type, $toUserId, $content, $params = [], $userNot
], $params));
if ($model->save()) {
if ($userNotification) {
User::updateAllCounters([$userNotification => 1], ['id' => $toUserId]);
/** @var ActiveRecord $userModel */
$userModel = Yii::$app->user->identityClass;
$userModel::updateAllCounters([$userNotification => 1], ['id' => $toUserId]);
}
} else {
throw new Exception(array_values($model->getFirstErrors())[0]);
Expand All @@ -189,9 +192,11 @@ public static function create($type, $toUserId, $content, $params = [], $userNot
* @param string $userNotification Optional user Notification field
* @throws Exception
*/
public static function createToAllUser($type, $content, $params = [], $userNotification = '')
public static function createToAllUser($type, $content, array $params = [], string $userNotification = '')
{
$items = User::find()->column();
/** @var ActiveRecord $userModel */
$userModel = Yii::$app->user->identityClass;
$items = $userModel::find()->column();
$rows = [];
foreach ($items as $key => $item) {
$rows[$key]['user_id'] = $item;
Expand All @@ -212,19 +217,22 @@ public static function createToAllUser($type, $content, $params = [], $userNotif
*/
public static function syncUserNotificationCount($userNotification)
{
$items = User::find()->column();
/** @var ActiveRecord $userModel */
$userModel = Yii::$app->user->identityClass;
$items = $userModel::find()->column();
foreach ($items as $key => $item) {
$unreadCount = self::find()->where(['user_id' => $item, 'status' => self::STATUS_UNREAD])->count('id');
User::updateAll([$userNotification => $unreadCount, 'id' => $item]);
$userModel::updateAll([$userNotification => $unreadCount, 'id' => $item]);
}
}

/**
* @param $tableName
* @param array $rows
* @return bool|int
* @throws Exception
*/
public static function saveAll($tableName, $rows = [])
public static function saveAll($tableName, array $rows = [])
{
if ($rows) {
return \Yii::$app->db->createCommand()
Expand Down

0 comments on commit fa91bbc

Please sign in to comment.