将数据持久化到数据库(Doctrine)

$orm = $this->getDoctrine()->getManager('default');

//增加的数据
$user = new User();
$user->setUsername("123");
$user->setPassword('1234');
$user->setSign("My sign.");
$user->setName("Hero");

$orm->persist($user);
$orm->flush();

插入数据库中相关信息(PDO)

# 最简单的方式执行(exec返回受影响行数)
$this->conn = $this->get('database_connection');
$sql = <<<SQL
INSERT INTO
  `user`(id,user_name,user_skey,user_uuid,user_nick,free_time,login_time,insert_time)
VALUES
  (1,'a123','1234567890','1234567890','测试昵称1',1506743300,1506743300,1506743300),
  (2,'a124','1234567890','1234567890','测试昵称2',1506743300,1506743300,1506743300),
  (3,'a125','1234567890','1234567890','测试昵称3',1506743300,1506743300,1506743300)
SQL;
$res = $this->conn->exec($sql);
$id = $this->conn->lastInsertId();
unset($this->conn);

# 按序绑定参数执行(execute返回执行bool结果)
$this->conn = $this->get('database_connection');
$sql = <<<SQL
INSERT INTO
  `user`(id,user_name,user_skey,user_uuid,user_nick,free_time,login_time,insert_time)
VALUES
  (?,?,?,?,?,?,?,?)
SQL;
$do_exec = $this->conn->prepare($sql);
$do_exec->bindValue(1, 4);
$do_exec->bindValue(2, 'a126');
$do_exec->bindValue(3, '1234567890');
$do_exec->bindValue(4, '1234567890');
$do_exec->bindValue(5, '测试昵称4');
$do_exec->bindValue(6, 1506743300);
$do_exec->bindValue(7, 1506743300);
$do_exec->bindValue(8, 1506743300);
$res = $do_exec->execute();
$id = $this->conn->lastInsertId();
unset($do_exec);
unset($this->conn);

# 按key绑定参数执行(execute返回执行bool结果)
$this->conn = $this->get('database_connection');
$sql = <<<SQL
INSERT INTO
  `user`(id,user_name,user_skey,user_uuid,user_nick,free_time,login_time,insert_time)
VALUES
  (:id,:user_name,:user_skey,:user_uuid,:user_nick,:free_time,:login_time,:insert_time)
SQL;
$id = 5;
$user_name = 'a127';
$user_skey = '1234567890';
$user_uuid = '1234567890';
$user_nick = '测试昵称5';
$free_time = 1506743300;
$login_time = 1506743300;
$insert_time = 1506743300;
$do_exec = $this->conn->prepare($sql);
$do_exec->bindParam('id', $id);
$do_exec->bindParam('user_name', $user_name);
$do_exec->bindParam('user_skey', $user_skey);
$do_exec->bindParam('user_uuid', $user_uuid);
$do_exec->bindParam('user_nick', $user_nick);
$do_exec->bindParam('free_time', $free_time);
$do_exec->bindParam('login_time', $login_time);
$do_exec->bindParam('insert_time', $insert_time);
$res = $do_exec->execute();
$id = $this->conn->lastInsertId();
unset($do_exec);
unset($this->conn);
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2018-05-23 16:25:59

results matching ""

    No results matching ""