Example #1
0
 public function testSaveInserts()
 {
     // the return value from the find
     $collectionName = 'users';
     $userData = array('name' => 'Martyn', 'created_at' => new \MongoDate(time()));
     $this->connectionMock->expects($this->once())->method('insert')->with($collectionName, $userData);
     $user = new UserValidator($userData);
     $user->save();
 }
 /**
  * 
  */
 public function __construct()
 {
     if (!isset(self::$validator)) {
         self::$validator = Validator::create();
     }
 }
Example #3
0
<?php

$errors = array();
$User = null;
$edit = array_key_exists('id', $_GET);
if ($edit) {
    $User = Utils::getUserByGetId();
} else {
    // set defaults
    $User = new User();
    //$User->setDate(new DateTime());
}
if (array_key_exists('cancel', $_POST)) {
} elseif (array_key_exists('save', $_POST)) {
    $data = array('email' => $_POST['Users']['email'], 'password' => $_POST['Users']['password']);
    // map
    UserMapper::map($User, $data);
    // validate
    $errors = UserValidator::validate($User);
    // validate
    if (empty($errors)) {
        // save
        $dao = new UserDao();
        $User = $dao->save($User);
        Flash::addFlash('Success Booking :)');
        // redirect
        Utils::redirect('home');
    }
}
<?php

$errors = array();
$user_obj = new User();
if (array_key_exists('add', $_POST)) {
    $data = array('first_name' => $_POST['user']['first_name'], 'user_password' => $_POST['user']['user_password']);
    UserMapper::map($user_obj, $data);
    $errors = UserValidator::validate($user_obj);
    if (empty($errors)) {
        $dao = new UserDao();
        $dao->add($user_obj);
        Flash::addFlash('1 user is added to db successfully :)');
    }
}
 /**
  * @api {post} /user 注册接口
  * @apiHeader {String} Accept=api-version=1.0 api版本
  * @apiHeaderExample {String} Header-Example:
  *     {
  *       "Accept": "api-version=1.0"
  *     }
  * @apiName register
  * @apiGroup User
  * @apiVersion 1.0.0
  *
  * @apiSuccess {Array} empty_array 空数组
  *
  * @apiUse errorExample
  */
 public function register()
 {
     $this->db->begin();
     $data = $this->request->getPost();
     $userValidator = new UserValidator();
     $messages = $userValidator->validate($data);
     if (0 != count($messages)) {
         return parent::resWithErrMsg($messages, 406);
     }
     $modelUser = new User();
     $duplicate = $modelUser->findFirst("lower(username)='" . strtolower($data['username']) . "'");
     if (!empty($duplicate)) {
         return parent::valueDuplicate('username');
     }
     $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
     $res = $modelUser->create($data);
     if (false == $res) {
         $this->db->rollback();
         return parent::resWithErrMsg($modelUser->getMessages());
     }
     $config = $this->di->get('config');
     $userRole['role_id'] = $config->role->User;
     $userRole['user_id'] = $modelUser->id;
     $roleUserModel = new RoleUser();
     $res = $roleUserModel->create($userRole);
     if (false == $res) {
         $this->db->rollback();
         return parent::resWithErrMsg($roleUserModel->getMessages());
     }
     $this->db->commit();
     return parent::success();
 }