native() public static méthode

Splice sql use native string(without escaping) for example: where('time>?', 'now()') => " WHERE time > 'now()' " where('time>?', Sql::native('now()')) => " WHERE time > now() "
public static native ( string $str ) : Native
$str string
Résultat Native
Exemple #1
0
 /**
  * create user
  * @route({"POST","/"}) 
  * @param({"account", "$._POST.mobile"})  cell-phone number, required
  * @param({"password", "$._POST.password"})  password, required
  * @param({"alias", "$._POST.alias"})  user's alias, required
  * @param({"avatar", "$._FILES.avatar.tmp_name"})  user's avatar, optional
  * @param({"token", "$._COOKIE.token"})  
  * 
  * @throws({"phprs\util\exceptions\Forbidden","res", "403 Forbidden",{"error":"Forbidden"}}) cookie invalid
  * 
  * @throws({"AliasConflict","res", "409 Conflict",{"error":"AliasConflict"}}) alias conflict
  * 
  * @throws({"AccountConflict","res", "409 Conflict",{"error":"AccountConflict"}}) account conflict
  * 
  * @return({"cookie","uid","$uid","+365 days","/"})  uid
  * @return user's id
  * {"uid":"1233"}
  */
 public function createUser(&$uid, $token, $account, $alias, $password, $avatar = null)
 {
     $tokens = $this->factory->create('Tokens');
     $token = $tokens->getToken($token);
     Verify::isTrue(!$token['uid'], new BadRequest('invalid token'));
     Verify::isTrue($token['account'] == $account, new Forbidden('invalid mobile ' . $account));
     if ($avatar) {
         $avatar = $this->uploadAvatar($avatar);
     } else {
         $avatar = '';
     }
     $pdo = $this->db;
     $pdo->beginTransaction();
     try {
         //is account conflict
         $res = Sql::select('uid')->from('uc_members')->where('username = ? OR email = ? OR mobile = ?', $account, $account, $account)->forUpdate()->get($pdo);
         Verify::isTrue(count($res) == 0, new AccountConflict("account {$account} conflict"));
         //is avatar conflict
         $res = Sql::select('uid')->from('pre_common_member_profile')->where('realname = ?', $alias)->forUpdate()->get($pdo);
         Verify::isTrue(count($res) == 0, new AliasConflict("alias {$alias} conflict"));
         $uid = Sql::insertInto('uc_members')->values(['username' => $account, 'password' => $password, 'regdate' => Sql::native('UNIX_TIMESTAMP(now())'), 'salt' => ''])->exec($pdo)->lastInsertId();
         Sql::insertInto('pre_common_member_profile')->values(['realname' => $alias, 'uid' => $uid, 'avatar' => $avatar])->exec($pdo);
         $pdo->commit();
     } catch (Exception $e) {
         Logger::warning("createUser({$account}) failed with " . $e->getMessage());
         $pdo->rollBack();
         throw $e;
     }
     $token['uid'] = $uid;
     $tokens->updateToken($token, $token);
     return ['uid' => $uid];
 }
Exemple #2
0
 public function testForReplace1()
 {
     //REPLACE INTO tab VALUES(1,2,now())
     $this->db->setExpected('REPLACE INTO tab VALUES(?,?,now())', 1, 2);
     Sql::replaceInto('tab')->values([1, 2, Sql::native('now()')])->exec($this->db);
 }