{"id":866,"date":"2017-05-23T11:11:25","date_gmt":"2017-05-23T09:11:25","guid":{"rendered":"http:\/\/webpiraten.de\/?p=866"},"modified":"2017-05-23T11:15:58","modified_gmt":"2017-05-23T09:15:58","slug":"get-random-object-mit-doctrine","status":"publish","type":"post","link":"https:\/\/webpiraten.de\/index.php\/php\/get-random-object-mit-doctrine\/","title":{"rendered":"GET random Object mit Doctrine"},"content":{"rendered":"<p>Doctrine selbst hat keine <strong>ORDER BY rand()<\/strong> Funktion implementiert. Aber es gibt eine L\u00f6sung. Man l\u00e4sst sich den <strong>total count<\/strong> der <strong>rows <\/strong>ausgeben und nutzt die PHP interne <strong>rand()<\/strong> Funktion um <strong>FirstResult<\/strong> zu definieren.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n    \/**\r\n     * @return mixed\r\n     *\/\r\n    public function getTotal()\r\n    {\r\n        return $this-&gt;getEntityManager()-&gt;createQueryBuilder()\r\n            -&gt;select('COUNT(p.id)')\r\n            -&gt;from('AppBundle:Product', 'p')\r\n            -&gt;getQuery()\r\n            -&gt;getSingleScalarResult();\r\n    }\r\n\r\n    \/**\r\n     * @return mixed\r\n     *\/\r\n    public function getRandom()\r\n    {\r\n        return $this-&gt;getEntityManager()-&gt;createQueryBuilder()\r\n            -&gt;select('p')\r\n            -&gt;from('AppBundle:Product', 'p')\r\n            -&gt;setFirstResult(rand(0, $this-&gt;getTotal()-1))\r\n            -&gt;setMaxResults(1)\r\n            -&gt;getQuery()\r\n            -&gt;getSingleResult();\r\n    }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Doctrine selbst hat keine ORDER BY rand() Funktion implementiert. Aber es gibt eine L\u00f6sung. Man l\u00e4sst sich den total count der rows ausgeben und nutzt die PHP interne rand() Funktion um FirstResult zu definieren. \/** * @return mixed *\/ public function getTotal() { return $this-&gt;getEntityManager()-&gt;createQueryBuilder() -&gt;select(&#8218;COUNT(p.id)&#8216;) -&gt;from(&#8218;AppBundle:Product&#8216;, &#8218;p&#8216;) -&gt;getQuery() -&gt;getSingleScalarResult(); } \/** * @return mixed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,11,5,567],"tags":[296,634,657,13,633,655,654,154,656],"class_list":["post-866","post","type-post","status-publish","format-standard","hentry","category-doctrine-orm","category-mysql","category-php","category-symfony2","tag-doctrine","tag-mysql","tag-object","tag-orm","tag-php","tag-rand","tag-random","tag-row","tag-total"],"_links":{"self":[{"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/posts\/866","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/comments?post=866"}],"version-history":[{"count":4,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/posts\/866\/revisions"}],"predecessor-version":[{"id":870,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/posts\/866\/revisions\/870"}],"wp:attachment":[{"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/media?parent=866"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/categories?post=866"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webpiraten.de\/index.php\/wp-json\/wp\/v2\/tags?post=866"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}