删除数据库中的相关信息(Doctrine)

$orm = $this->getDoctrine()->getEntityManager();
$product = $orm->getRepository('DBBundle:User')->find($user_id);
if (!empty($product)){
    $orm->remove($product);
    $orm->flush();
}

删除数据库中相关信息(PDO)

# 最简单的方式执行(exec返回受影响行数)
$this->conn = $this->get('database_connection');
$sql = <<<SQL
DELETE
FROM
  `user`
WHERE
  `user`.id = 1
SQL;
$res = $this->conn->exec($sql);
unset($this->conn);

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

# 按key绑定参数执行(execute返回执行bool结果)
$this->conn = $this->get('database_connection');
$sql = <<<SQL
DELETE
FROM
  `user`
WHERE
  `user`.id = :id
SQL;
$id = 10000;
$do_exec = $this->conn->prepare($sql);
$do_exec->bindParam('id', $id);
$res = $do_exec->execute();
unset($do_exec);
unset($this->conn);
@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2018-05-23 16:25:59

results matching ""

    No results matching ""