Skip to content

Commit

Permalink
Merge 80a9f7c into 2a37c94
Browse files Browse the repository at this point in the history
  • Loading branch information
ohga21 committed Aug 28, 2018
2 parents 2a37c94 + 80a9f7c commit 5078059
Showing 1 changed file with 61 additions and 9 deletions.
70 changes: 61 additions & 9 deletions Model/RolesRoomsUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
*/
class RolesRoomsUser extends RoomsAppModel {

/**
* 同じデータを取得しないようにキャッシュする
*
* @var array
*/
private static $__memoryCache = [];

/**
* Validation rules
*
Expand Down Expand Up @@ -133,8 +140,37 @@ public function getRolesRoomsUsers($conditions = array(), $query = array()) {
$conditions = Hash::merge(array('Room.page_id_top NOT' => null), $conditions);
}

if (! isset($query['fields'])) {
$query['fields'] = [
// Condition以外のクエリ調整
$fields = $this->__getRolesRoomsUsersFields($query);
$type = $this->__getRolesRoomsUsersType($query);
$joins = $this->__getRolesRoomsUsersJoin($type, $query);

// 取得条件毎にキャッシュする
$cacheId = json_encode($conditions);
if (empty(self::$__memoryCache[$cacheId][$type])) {
self::$__memoryCache[$cacheId][$type] = $this->find($type, Hash::merge(array(
'recursive' => -1,
'fields' => $fields,
'joins' => $joins,
'conditions' => $conditions,
), $query));
}
$rolesRoomsUsers = self::$__memoryCache[$cacheId][$type];
return $rolesRoomsUsers;
}

/**
* Return Fields of "roles rooms users" to get
*
* @param array &$query Condition以外のクエリ
* @return array
*/
private function __getRolesRoomsUsersFields(&$query) {
if (isset($query['fields'])) {
$fields = $query['fields'];
unset($query['fields']);
} else {
$fields = [
$this->alias . '.id',
$this->alias . '.roles_room_id',
$this->alias . '.user_id',
Expand All @@ -148,9 +184,31 @@ public function getRolesRoomsUsers($conditions = array(), $query = array()) {
];
}

return $fields;
}

/**
* Return Type of Query
*
* @param array &$query Condition以外のクエリ
* @return string
* @throws InvalidArgumentException
*/
private function __getRolesRoomsUsersType(&$query) {
$type = Hash::get($query, 'type', 'all');
$query = Hash::remove($query, 'type');

return $type;
}

/**
* Return Joins of Query
*
* @param string $type 取得するタイプ
* @param array &$query Condition以外のクエリ
* @return string
*/
private function __getRolesRoomsUsersJoin($type, &$query) {
//呼ばれる条件に応じて、結合テーブルを切り分ける
if (isset($query['joins'])) {
$joins = $query['joins'];
Expand Down Expand Up @@ -211,13 +269,7 @@ public function getRolesRoomsUsers($conditions = array(), $query = array()) {
);
}

$rolesRoomsUsers = $this->find($type, Hash::merge(array(
'recursive' => -1,
'joins' => $joins,
'conditions' => $conditions,
), $query));

return $rolesRoomsUsers;
return $joins;
}

/**
Expand Down

0 comments on commit 5078059

Please sign in to comment.