doctrine随机取数据
2016-07-09 17:13
197 查看
For some really good reasons or, doctrine2 do not implement the RAND() fonction to sort randomly your query results. Here some dirty ways to do it. Don’t use them it’s bad.
One solution is to shuffle your collection of rows using php
In this solution, you retrieve the last 10 rows and shuffle them after. Peformances are not too bad but you will always retrieves the same last rows from your table.
Another solution is to use the array_rand php fonction
In this case you fetch all rows from your table, this could be slow and memory consuming…
If you need to retrieve only one row, you can use somethinfg like this
This solution can only be used if you want to retrieve any of the tables rows, if you add a filtrer you may return an empty result.
This solution is not dirty it just use 2 queries instead of one. the tips is to use the offset. And you can use a filter !!!
And you still can use native queries : http://docs.doctrine-project.org/en/latest/reference/native-sql.html
One solution is to shuffle your collection of rows using php
public function getRandom($max = 10,$site) { $results = $this->createQueryBuilder('u') ->where('u.site = :site') ->setParameter('site', $site) ->orderBy('u.sort', 'DESC') ->setMaxResults($max) ->getQuery() ->getResult(); shuffle($results); return $results; }
In this solution, you retrieve the last 10 rows and shuffle them after. Peformances are not too bad but you will always retrieves the same last rows from your table.
Another solution is to use the array_rand php fonction
public function getRandom($site) { $results = $this->createQueryBuilder('u') ->where('u.site = :site') ->setParameter('site', $site) ->orderBy('u.sort', 'DESC') ->getQuery() ->getResult(); $result2 = array_rand($results); return $result2; }
In this case you fetch all rows from your table, this could be slow and memory consuming…
If you need to retrieve only one row, you can use somethinfg like this
public function getOneRandom() { $em = $this->getEntityManager(); $max = $em->createQuery(' SELECT MAX(q.id) FROM questions q ') ->getSingleScalarResult(); return $em->createQuery(' SELECT q FROM questions q WHERE q.id >= :random ORDER BY q.id ASC ') ->setParameter('random',rand(0,$max)) ->setMaxResults(1) ->getSingleResult(); }
This solution can only be used if you want to retrieve any of the tables rows, if you add a filtrer you may return an empty result.
This solution is not dirty it just use 2 queries instead of one. the tips is to use the offset. And you can use a filter !!!
$qCount = Doctrine::getTable('Questions') ->createQuery() ->select('count(*)') ->where('site = :site') ->setParameter('site', $site) ->fetchOne(array(), Doctrine::HYDRATE_NONE); $question = Doctrine::getTable('Questions') ->createQuery() ->select('*') ->where('site = :site') ->setParameter('site', $site) ->limit(1) ->offset(rand(0, $qCount[0] - 1)) ->fetchOne();
And you still can use native queries : http://docs.doctrine-project.org/en/latest/reference/native-sql.html
相关文章推荐
- Linux高速抓包内容分析(1)
- 设计模式——单例模式
- Java开发环境(JDK)的下载、安装、配置
- java语言程序设计基础篇第七章编程练习题
- 记录自己所学的,帮助自己,希望也能帮助他人!!!
- 从实施工程师到研发工程师
- hadoop入门级总结二:Map/Reduce
- hadoop入门级总结二:Map/Reduce
- Android Studio插件整理
- 华为面试题【8】-找零钱
- 华为面试题【8】-找零钱
- OpenGL学习脚印:光源类型和使用多个光源(Light source and multiple lights)
- 华为面试题【8】-找零钱
- 华为面试题【8】-找零钱
- 华为面试题【8】-找零钱
- 华为面试题【8】-找零钱
- 华为面试题【8】-找零钱
- MVC 项目中为什么会有两个web.config
- CentOS7 安装Keepalived以及基本配置
- office法找到此应用程序的许可证 解