如何使用 Doctrine 在 Symfony2 中创建自定义 SQL 查询?或者没有 Doctrine,我不在乎。
不是这样工作的:
$em = $this->getDoctrine()->getEntityManager();
$em->createQuery($sql);
$em->execute();
谢谢。
最佳答案
您可以直接从实体管理器中获取连接对象,并通过它直接运行 SQL 查询:
$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 几乎可以处理您可能需要的任何查询。
关于php - Symfony2 & Doctrine : Create custom SQL-Query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12862389/