Skip to content

Commit

Permalink
Reduced queries by multiple users Post
Browse files Browse the repository at this point in the history
  • Loading branch information
tbae604 committed Dec 23, 2014
1 parent cc3b243 commit 346c87a
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions app/controllers/v1_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,17 @@ public function users($id = null) {
}
// adding multiple users from import (expected input: array)
} else if (isset($decode['0'])) {
// extract all usernames from decode to array
$names = array();
foreach ($decode as $person) {
$names[] = $person['username'];
}
// one query of db by usernames to get all users' ids and roles
$allUsers = $this->User->find('all', array(
'conditions' => array('username' => $names),
'contain' => 'Role'));
// lookup table to compare roles
$allUsers = Set::combine($allUsers, '{n}.User.id', '{n}.Role.0.id');
$data = array();
// rearrange the data
foreach ($decode as $person) {
Expand All @@ -376,9 +387,8 @@ public function users($id = null) {
$roleId = $person['role_id'];
// if (ipeer) id does exist
if ($person['id']) {
// Queries role_id saved in iPeer
$role = $this->RolesUser->find('first', array('conditions' => array('user_id' => $person['id']), 'fields' => 'role_id'));
$iprRole = $role['RolesUser']['role_id'];
// use lookup table of saved iPeer roles
$iprRole = $allUsers[$person['id']];
// use external id if its number lower
if ($person['role_id'] > $iprRole) {
$roleId = $iprRole;
Expand Down

0 comments on commit 346c87a

Please sign in to comment.