修改数据库中的信息(Doctrine)

  1. 作者QQ:67065435 QQ群:821635552
<?php
$orm = $this->getDoctrine()->getManager('default');
$user = $orm->getRepository('DBBundle:User')->find($id);
if (!empty($user)) {
    $user->setName('新数据');
    $orm->flush();
}

更新数据库中相关信息(PDO)

<?php
# 最简单的方式执行(exec返回受影响行数)
$this->dbh = $this->get('database_connection');
$sql = <<<SQL
UPDATE
  `user`
SET
  `user`.login_time = `user`.login_time + 1
WHERE
  `user`.id = 1
SQL;
$res = $this->dbh->exec($sql);
unset($this->dbh);

# 按序绑定参数执行(execute返回执行bool结果)
$this->dbh = $this->get('database_connection');
$sql = <<<SQL
UPDATE
  `user`
SET
  `user`.login_time = `user`.login_time + 1
WHERE
  `user`.id IN(?,?,?)
SQL;
$sth = $this->dbh->prepare($sql);
$sth->bindValue(1, 10000);
$sth->bindValue(2, 10086);
$sth->bindValue(3, 95001);
$res = $sth->execute();
unset($sth);
unset($this->dbh);

# 按key绑定参数执行(execute返回执行bool结果)
$this->dbh = $this->get('database_connection');
$sql = <<<SQL
UPDATE
  `user`
SET
  `user`.login_time = `user`.login_time + 1
WHERE
  `user`.id = :id
SQL;
$id = 10000;
$sth = $this->dbh->prepare($sql);
$sth->bindParam('id', $id);
$res = $sth->execute();
unset($sth);
unset($this->dbh);
Copyright © 豆包嘿嘿~ 2012-∞ 冀ICP备17033181号 all right reserved,powered by Gitbook修订: 2012-12-31 12:00:00

results matching ""

    No results matching ""