Symfony2 amp; Doctrine: Create custom SQL-Query(Symfony2 amp;原则:创建自定义 SQL 查询)
问题描述
如何使用 Doctrine 在 Symfony2 中创建自定义 SQL 查询?或者没有教义,我不在乎.
How can I create a custom SQL query in Symfony2 using Doctrine? Or without Doctrine, I don't care.
不能这样工作:
$em = $this->getDoctrine()->getEntityManager();
$em->createQuery($sql);
$em->execute();
谢谢.
推荐答案
你可以直接从Entity Manager中获取Connection对象,并通过它直接运行SQL查询:
You can get the Connection object directly from the Entity Manager, and run SQL queries directly through that:
$em = $this->getDoctrine()->getManager(); // ...or getEntityManager() prior to Symfony 2.1
$connection = $em->getConnection();
$statement = $connection->prepare("SELECT something FROM somethingelse WHERE id = :id");
$statement->bindValue('id', 123);
$statement->execute();
$results = $statement->fetchAll();
但是,除非真的有必要,否则我建议不要这样做... Doctrine 的 DQL 几乎可以处理您可能需要的任何查询.
However, I'd advise against this unless it's really necessary... Doctrine's DQL can handle almost any query you might need.
官方文档:https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/data-retrieval-and-manipulation.html
这篇关于Symfony2 &原则:创建自定义 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Symfony2 &原则:创建自定义 SQL 查询


基础教程推荐
- Web 服务器如何处理请求? 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- php中的foreach复选框POST 2021-01-01
- 将变量从树枝传递给 js 2022-01-01
- php中的PDF导出 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01