示例#1
0
 /**
  * Saves a user to the database
  * Or update an existing user
  *
  * @url POST /
  * @url PUT /$id
  */
 public function SaveUser($id = null, $data)
 {
     // Only process valid data
     if ($this->ValidateUser($data)) {
         // Format for update/insert query
         $dbData = array('firstName' => $data->FirstName, 'lastName' => $data->LastName, 'birthday' => $data->Birthday, 'gender' => $data->Gender, 'categoryId' => $data->Category->Id);
         // Update existing user?
         if ($id) {
             $this->Db()->where('id', $id);
             if ($this->Db()->update('Users', $dbData)) {
                 $user = new UserModel($id, $data->FirstName, $data->LastName, $data->Birthday, $data->Gender, $data->Category);
             } else {
                 throw new RestException(500, 'Update mislukt: ' . $this->Db()->getLastError());
             }
             // Add new user
         } else {
             $id = $this->Db()->insert('Users', $dbData);
             if ($id) {
                 $user = new UserModel($id, $data->FirstName, $data->LastName, $data->Birthday, $data->Gender, $data->Category);
             } else {
                 throw new RestException(500, 'Toevoegen mislukt: ' . $this->Db()->getLastError());
             }
         }
         return $user->toArray();
     }
 }
示例#2
0
 public function Execute()
 {
     $result = $this->Db()->join('Categories c', 'c.id = u.categoryId', 'LEFT')->where('u.id', $this->userId)->orderBy('u.firstName', 'ASC')->orderBy('u.lastName', 'ASC')->getOne('Users u', 'u.id AS id,
             u.firstName AS firstName,
             u.lastName AS lastName,
             u.birthday AS birthday,
             u.gender as gender,
             c.id AS categoryId,
             c.name AS categoryName');
     // User found?
     if ($result == null) {
         throw new RestException(500, 'Deelnemer niet gevonden. ' . $this->Db()->getLastError());
     }
     $c = new CategoryModel($result['categoryId'], $result['categoryName']);
     $u = new UserModel($result['id'], $result['firstName'], $result['lastName'], $result['birthday'], $result['gender'], $c);
     return $u->toArray();
 }
示例#3
0
 public function Execute()
 {
     $results = $this->Db()->join('Categories c', 'c.id = u.categoryId', 'LEFT')->orderBy('u.firstName', 'ASC')->orderBy('u.lastName', 'ASC')->get('Users u', null, 'u.id AS id,
             u.firstName AS firstName,
             u.lastName AS lastName,
             u.birthday AS birthday,
             u.gender as gender,
             c.id AS categoryId,
             c.name AS categoryName');
     $users = array();
     // Add all user object to array
     foreach ($results as $i => $result) {
         $c = new CategoryModel($result['categoryId'], $result['categoryName']);
         $u = new UserModel($result['id'], $result['firstName'], $result['lastName'], $result['birthday'], $result['gender'], $c);
         $users[] = $u->toArray();
     }
     return $users;
 }