如何使用Doctrine在Symfony2中创建自定义SQL查询?或者没有教条,我不在乎。Symfony2&Doctrine:创建自定义SQL查询
不工作是这样的:
$em = $this->getDoctrine()->getEntityManager();
$em->createQuery($sql);
$em->execute();
感谢。
如何使用Doctrine在Symfony2中创建自定义SQL查询?或者没有教条,我不在乎。Symfony2&Doctrine:创建自定义SQL查询
不工作是这样的:
$em = $this->getDoctrine()->getEntityManager();
$em->createQuery($sql);
$em->execute();
感谢。
您可以直接从实体管理器得到的Connection对象,并直接通过运行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();
不过,我会反对这项建议,除非真的有必要...主义的DQL可以处理几乎任何你可能需要的查询。
正式文件:http://doctrine-dbal.readthedocs.org/en/latest/reference/data-retrieval-and-manipulation.html
您可以执行此代码它的工作原理:
$em = $this->getDoctrine()->getEntityManager();
$result= $em->createQuery($sql)->getResult();
'$ em-> createQuery()'不会执行SQL,而是执行DQL。 – loostro
也有在说与本地SQL规定:http://docs.doctrine-project.org/en/最新/ reference/native-sql.html – Orbling
工程就像一个魅力,谢谢:) – a1337q
它的完美,非常感谢! – iarroyo