Symfony2 &Doctrine:创建自定义 SQL 查询 [英] Symfony2 & Doctrine: Create custom SQL-Query
本文介绍了Symfony2 &Doctrine:创建自定义 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();
谢谢.
推荐答案
您可以直接从实体管理器中获取 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.
这篇关于Symfony2 &Doctrine:创建自定义 SQL 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文