insertInto('table') => "INSERT INTO table"
public static insertInto ( string $table ) : |
||
$table | string | |
return |
/** * 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]; }
public function testForInsert3() { //INSERT INTO tab(a,b,c)VALUES(1,2,now()) $this->db->setExpected('INSERT INTO tab(a,b,c) VALUES(?,?,now()) ON DUPLICATE KEY UPDATE a=a+1', 1, 2); Sql::insertInto('tab')->values(['a' => 1, 'b' => 2, 'c' => Sql::native('now()')])->onDuplicateKeyUpdate('a', Sql::native('a+1'))->exec($this->db); Sql::insertInto('tab')->values(['a' => 1, 'b' => 2, 'c' => Sql::native('now()')])->onDuplicateKeyUpdateArgs(['a' => Sql::native('a+1')])->exec($this->db); Sql::insertInto('tab')->values(['a' => 1, 'b' => 2, 'c' => Sql::native('now()')])->onDuplicateKeyUpdateExpr('a=a+1')->exec($this->db); }