{"id":600,"date":"2012-06-02T22:43:43","date_gmt":"2012-06-03T03:43:43","guid":{"rendered":"http:\/\/blog.xfloyd.net\/?p=600"},"modified":"2015-11-09T17:10:55","modified_gmt":"2015-11-09T22:10:55","slug":"executing-sql-directly-in-symfony2-doctrine","status":"publish","type":"post","link":"https:\/\/xfloyd.net\/blog\/?p=600","title":{"rendered":"Executing SQL directly in Symfony2 Doctrine"},"content":{"rendered":"<p>Ok, so I&#8217;m moving project from non framework PHP to Symfony2, and it goes very nice, except of Doctrine, I&#8217;m sure it&#8217;s me. I&#8217;ve been using MySQL for last 8 years and DQL vs SQL to me feels like i.e. taking snowboard for the first time, after skiing for 30 years. What I do after 30 min. of trying. I&#8217;m going back to rental place and I switch to skis. Below one can find skis.<br \/>\n<!--more--><\/p>\n<pre>\r\nuse Doctrine\\ORM\\EntityRepository;\r\n\r\nclass ProductRepository extends EntityRepository\r\n{\r\n\r\n$stmt = $this-&gt;getEntityManager()\r\n-&gt;getConnection()\r\n-&gt;prepare('select * from product where 1');\r\n$stmt-&gt;execute();\r\n$result = $stmt-&gt;fetchAll();\r\n\r\nreturn $this-&gt;render('Test1Bundle:Default:index.html.twig', array('name' =&gt; 'Showig '. print_r($result,true)));\r\n}\r\n<\/pre>\n<p>Or if you do not want to use class try this.<\/p>\n<pre>\r\n$stmt = $this->getDoctrine()->getEntityManager()\r\n   ->getConnection()\r\n    ->prepare('select * from product where 1');\r\n$stmt->execute();\r\n$result = $stmt->fetchAll();\r\n<\/pre>\n<p>Or this<\/p>\n<pre>\r\n$rsm = new ResultSetMapping;\r\n        $rsm->addScalarResult('optionname', 'option');\r\n        $rsm->addScalarResult('answers', 'count');\r\n\r\n        return $this->_em->createNativeQuery('\r\n       SELECT\r\n         qo.title as optionname,\r\n         COUNT(qap.answer_id) as answers\r\n      FROM\r\n         quiz_answer_option qap\r\n         INNER JOIN quizoption qo ON qo.id = qap.option_id\r\n         INNER JOIN quizquestion qq ON qq.id = qo.quizquestion_id\r\n      WHERE\r\n         qq.active\r\n         AND qq.id = :quizquestionid\r\n      GROUP BY qap.option_id\r\n      ORDER BY qo.number asc\r\n        ', $rsm)\r\n       ->setParameter('quizquestionid', $quizquestion->getId())\r\n       ->getResult();\r\n<\/pre>\n<p>For more look <a href=\"http:\/\/forum.symfony-project.org\/viewtopic.php?f=23&#038;t=37872\">HERE<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ok, so I&#8217;m moving project from non framework PHP to Symfony2, and it goes very nice, except of Doctrine, I&#8217;m sure it&#8217;s me. I&#8217;ve been using MySQL for last 8 years and DQL vs SQL to me feels like i.e. taking snowboard for the first time, after skiing for 30 years. What I do after [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,4,13],"tags":[],"_links":{"self":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/600"}],"collection":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=600"}],"version-history":[{"count":10,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/600\/revisions"}],"predecessor-version":[{"id":864,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/600\/revisions\/864"}],"wp:attachment":[{"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=600"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=600"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xfloyd.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}