a13*_*37q 23 php sql doctrine symfony
如何使用Doctrine在Symfony2中创建自定义SQL查询?或者没有学说,我不在乎.
不这样工作:
$em = $this->getDoctrine()->getEntityManager();
$em->createQuery($sql);
$em->execute();
Run Code Online (Sandbox Code Playgroud)
谢谢.
Tho*_*ley 83
您可以直接从实体管理器获取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();
Run Code Online (Sandbox Code Playgroud)
但是,除非确实有必要,否则我建议不要这样做...... Doctrine的DQL几乎可以处理您可能需要的任何查询.
官方文件:https://www.doctrine-project.org/projects/doctrine-dbal/en/2.9/reference/data-retrieval-and-manipulation.html