如何在 Symfony 中使用 sql 查询

how use sql query in Symfony

我如何在 symfony 中使用学说编写本机 sql 查询?

$connection = $this->getDoctrine()->getManager()->getConnection();

$connection 现在是 class Doctrine\DBAL\Connection 检查文档 http://www.doctrine-project.org/api/dbal/2.1/class-Doctrine.DBAL.Connection.html

这段代码应该能帮到你。

$connection = $this->getDoctrine()->getManager()->getConnection();
$query = "
  INSERT INTO data (
      name
    , age
  ) VALUES (
      ?
    , ?
  )
";
$connection->executeQuery(
   $query
 , array(
      'test'
    , 30
   )
 , array(
     \PDO::PARAM_INT   
    ,\PDO::PARAM_STR
  )
);

或者

$connection = $this->getDoctrine()->getManager()->getConnection();

$name = 'test';
$age = 31;

$query = "
  INSERT INTO data (
      name
    , age
  ) VALUES (
      :name
    , :age
  )
";

$statement = $connection->prepare($sql);
$statement->bindValue("name", $name, \PDO::PARAM_INT);
$statement->bindValue("age", $age, \PDO::PARAM_STR);
$statement->execute();