/** * Tests if the user group mapper returns the right user group model using an * data array. */ public function testLoadFromArray() { $groupRow = array('id' => 1, 'name' => 'Administrator'); $mapper = new GroupMapper(); $group = $mapper->loadFromArray($groupRow); $this->assertTrue($group !== false); $this->assertEquals(1, $group->getId()); $this->assertEquals('Administrator', $group->getName()); }
/** * Saves the given group. */ public function saveAction() { $postData = $this->getRequest()->getPost(); if (isset($postData['group'])) { $groupData = $postData['group']; $groupMapper = new GroupMapper(); $group = $groupMapper->loadFromArray($groupData); $groupId = $groupMapper->save($group); if (!empty($groupId) && empty($groupData['id'])) { $this->addMessage('newGroupMsg'); } $this->redirect(array('action' => 'treat', 'id' => $groupId)); } }
/** * Returns an array with user models found by the where clause of false if * none found. * * @param array $where * @return null|\Modules\User\Models\User[] */ protected function getBy($where = []) { $userRows = $this->db()->select('*')->from('users')->where($where)->execute()->fetchRows(); if (!empty($userRows)) { $users = array(); foreach ($userRows as $userRow) { $groups = array(); $sql = 'SELECT g.* FROM `[prefix]_groups` AS g INNER JOIN `[prefix]_users_groups` AS ug ON g.id = ug.group_id WHERE ug.user_id = ' . $userRow['id']; $groupRows = $this->db()->queryArray($sql); $groupMapper = new Group(); foreach ($groupRows as $groupRow) { $groups[$groupRow['id']] = $groupMapper->loadFromArray($groupRow); } $user = $this->loadFromArray($userRow); $user->setGroups($groups); $users[] = $user; } return $users; } return null; }