【问题标题】:how use sql query in Symfony [closed]如何在 Symfony 中使用 sql 查询 [关闭]
【发布时间】:2015-07-30 19:24:20
【问题描述】:

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

【问题讨论】:

  • “复杂查询”非常抽象。你这是什么意思?编写原始 SQL ? DQL ?多亏了 Doctrine DBAL 层,你仍然可以在 Doctrine 中编写原始 SQL。 stackoverflow.com/questions/3325012/…

标签: php mysql symfony doctrine


【解决方案1】:
$connection = $this->getDoctrine()->getManager()->getConnection();

$connection 现在是一个类 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多