From 3569c43fb232dbc57dbbe196f8c628e7bd8d5d79 Mon Sep 17 00:00:00 2001 From: printempw Date: Sun, 8 Jan 2017 14:29:48 +0800 Subject: [PATCH] use Query Builder to get storage size used by user --- app/Models/User.php | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index b6c232bf..1dd20839 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -39,6 +39,13 @@ class User extends Model public $timestamps = false; protected $fillable = ['email', 'nickname', 'permission']; + /** + * Storage size used by user in KiB. + * + * @var int + */ + protected $storageUsed; + /** * Check if user is admin. * @@ -253,14 +260,18 @@ class User extends Model */ public function getStorageUsed() { - if (is_null($this->storage_used)) { - $this->storage_used = 0; - // recalculate - $sql = "SELECT SUM(`size`) AS total_size FROM `{table}` WHERE uploader = {$this->uid}"; - $result = \Database::table('textures')->fetchArray($sql)['total_size']; - $this->storage_used = $result ?: 0; + if (is_null($this->storageUsed)) { + $this->storageUsed = 0; + + $result = DB::table('textures') + ->select(DB::raw("SUM(size) AS total_size")) + ->where('uploader', $this->uid) + ->first()->total_size; + + $this->storageUsed = $result ?: 0; } - return $this->storage_used; + + return $this->storageUsed; } /**