将数据持久化到数据库(Doctrine)
作者QQ:67065435 QQ群:821635552
本站内容全部为作者原创,转载请注明出处!
<?php
$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)
<?php
# 最简单的方式执行(exec返回受影响行数)
$this->dbh = $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->dbh->exec($sql);
$id = $this->dbh->lastInsertId();
unset($this->dbh);
# 按序绑定参数执行(execute返回执行bool结果)
$this->dbh = $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;
$sth = $this->dbh->prepare($sql);
$sth->bindValue(1, 4);
$sth->bindValue(2, 'a126');
$sth->bindValue(3, '1234567890');
$sth->bindValue(4, '1234567890');
$sth->bindValue(5, '测试昵称4');
$sth->bindValue(6, 1506743300);
$sth->bindValue(7, 1506743300);
$sth->bindValue(8, 1506743300);
$res = $sth->execute();
$id = $this->dbh->lastInsertId();
unset($sth);
unset($this->dbh);
# 按key绑定参数执行(execute返回执行bool结果)
$this->dbh = $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;
$sth = $this->dbh->prepare($sql);
$sth->bindParam('id', $id);
$sth->bindParam('user_name', $user_name);
$sth->bindParam('user_skey', $user_skey);
$sth->bindParam('user_uuid', $user_uuid);
$sth->bindParam('user_nick', $user_nick);
$sth->bindParam('free_time', $free_time);
$sth->bindParam('login_time', $login_time);
$sth->bindParam('insert_time', $insert_time);
$res = $sth->execute();
$id = $this->dbh->lastInsertId();
unset($sth);
unset($this->dbh);