PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Bisna/Domain/Entity/Filter/Criteria.php

https://gitlab.com/oytunistrator/Zend1-Doctrine2-Skeleton
PHP | 358 lines | 102 code | 36 blank | 220 comment | 0 complexity | e3315a2aefdeabe6122104521a1ba17e MD5 | raw file
  1. <?php
  2. namespace Bisna\Domain\Entity\Filter;
  3. use Doctrine\ORM\QueryBuilder;
  4. /**
  5. * Composes the Doctrine QueryBuilder
  6. *
  7. * @category Bisna
  8. * @package Domain
  9. * @subpackage Filter
  10. */
  11. class Criteria
  12. {
  13. /**
  14. * @var Doctrine\ORM\QueryBuilder
  15. */
  16. protected $queryBuilder;
  17. /**
  18. * Constructor of entity filter criteria.
  19. *
  20. * @param Doctrine\ORM\QueryBuilder $queryBuilder
  21. */
  22. public function __construct(QueryBuilder $queryBuilder)
  23. {
  24. $this->queryBuilder = $queryBuilder;
  25. }
  26. /**
  27. * Retrieve the Doctrine query expression builder.
  28. *
  29. * @return Doctrine\ORM\Query\Expr
  30. */
  31. public function expr()
  32. {
  33. return $this->queryBuilder->expr();
  34. }
  35. /**
  36. * Retrieve the Doctrine query associated to this filter criteria.
  37. *
  38. * @return Doctrine\ORM\Query
  39. */
  40. public function getQuery()
  41. {
  42. return $this->queryBuilder->getQuery();
  43. }
  44. /**
  45. * Gets the root alias of the filter criteria.
  46. *
  47. * <code>
  48. * $filter = $service->buildFilterCriteria('u')
  49. *
  50. * $filter->getRootAlias(); // 'u'
  51. * </code>
  52. *
  53. * @return string
  54. */
  55. public function getRootAlias()
  56. {
  57. $rootAliases = $this->queryBuilder->getRootAliases();
  58. return $rootAliases[0];
  59. }
  60. /**
  61. * Gets the root entity of the filter criteria.
  62. *
  63. * <code>
  64. * $filter = $service->buildFilterCriteria('u')
  65. *
  66. * $filter->getRootEntity(); // 'User'
  67. * </code>
  68. *
  69. * @return string
  70. */
  71. public function getRootEntity()
  72. {
  73. $rootEntities = $this->queryBuilder->getRootEntities();
  74. return $rootEntities[0];
  75. }
  76. /**
  77. * Creates and adds a join over an entity association to the filter criteria.
  78. *
  79. * The entities in the joined association will be fetched as part of the query
  80. * result if the alias used for the joined association is placed in the select
  81. * expressions.
  82. *
  83. * <code>
  84. * $filter = $service->buildFilterCriteria('u')
  85. * ->leftJoin('u.Phonenumbers', 'p');
  86. * </code>
  87. *
  88. * @param string $join Doctrine join (ie. "e.groups")
  89. * @param string $alias Doctrine join alias (ie. "g")
  90. *
  91. * @return Criteria
  92. */
  93. public function leftJoin($join, $alias)
  94. {
  95. $this->queryBuilder->addSelect($alias);
  96. $this->queryBuilder->leftJoin($join, $alias);
  97. return $this;
  98. }
  99. /**
  100. * Creates and adds a join over an entity association to the filter criteria.
  101. *
  102. * The entities in the joined association will be fetched as part of the query
  103. * result if the alias used for the joined association is placed in the select
  104. * expressions.
  105. *
  106. * <code>
  107. * $filter = $service->buildFilterCriteria('u')
  108. * ->innerJoin('u.Phonenumbers', 'p');
  109. * </code>
  110. *
  111. * @param string $join Doctrine join (ie. "e.groups")
  112. * @param string $alias Doctrine join alias (ie. "g")
  113. *
  114. * @return Criteria
  115. */
  116. public function innerJoin($join, $alias)
  117. {
  118. $this->queryBuilder->addSelect($alias);
  119. $this->queryBuilder->innerJoin($join, $alias);
  120. return $this;
  121. }
  122. /**
  123. * Specifies one or more restrictions to the query result.
  124. * Replaces any previously specified restrictions, if any.
  125. *
  126. * <code>
  127. * $filter = $service->buildFilterCriteria('u')
  128. * ->where('u.id = ?');
  129. *
  130. * // You can optionally programatically build and/or expressions
  131. * $filter = $service->buildFilterCriteria('u')
  132. *
  133. * $or = $filter->expr()->orx();
  134. * $or->add($filter->expr()->eq('u.id', 1));
  135. * $or->add($filter->expr()->eq('u.id', 2));
  136. *
  137. * $filter->where($or);
  138. * </code>
  139. *
  140. * @param mixed $predicates The restriction predicates.
  141. *
  142. * @return Criteria
  143. */
  144. public function where($predicates)
  145. {
  146. call_user_func_array(array($this->queryBuilder, 'where'), func_get_args());
  147. return $this;
  148. }
  149. /**
  150. * Adds one or more restrictions to the query results, forming a logical
  151. * conjunction with any previously specified restrictions.
  152. *
  153. * <code>
  154. * $filter = $service->buildFilterCriteria('u')
  155. * ->where('u.username LIKE ?1')
  156. * ->andWhere('u.is_active = 1');
  157. * </code>
  158. *
  159. * @see where()
  160. * @param mixed $where The query restrictions.
  161. *
  162. * @return Criteria
  163. */
  164. public function andWhere($where)
  165. {
  166. call_user_func_array(array($this->queryBuilder, 'andWhere'), func_get_args());
  167. return $this;
  168. }
  169. /**
  170. * Adds one or more restrictions to the query results, forming a logical
  171. * conjunction with any previously specified restrictions.
  172. *
  173. * <code>
  174. * $filter = $service->buildFilterCriteria('u')
  175. * ->where('u.username LIKE ?1')
  176. * ->orWhere('u.is_active = 1');
  177. * </code>
  178. *
  179. * @see where()
  180. * @param mixed $where The query restrictions.
  181. *
  182. * @return Criteria
  183. */
  184. public function orWhere($where)
  185. {
  186. call_user_func_array(array($this->queryBuilder, 'orWhere'), func_get_args());
  187. return $this;
  188. }
  189. /**
  190. * Specifies an ordering for the query results.
  191. * Replaces any previously specified orderings, if any.
  192. *
  193. * @param string $sort The ordering expression.
  194. * @param string $order The ordering direction.
  195. *
  196. * @return Criteria
  197. */
  198. public function orderBy($sort, $order = null)
  199. {
  200. $this->queryBuilder->orderBy($sort, $order);
  201. return $this;
  202. }
  203. /**
  204. * Adds an ordering to the query results.
  205. *
  206. * @param string $sort The ordering expression.
  207. * @param string $order The ordering direction.
  208. *
  209. * @return Criteria
  210. */
  211. public function addOrderBy($sort, $order = null)
  212. {
  213. $this->queryBuilder->addOrderBy($sort, $order);
  214. return $this;
  215. }
  216. /**
  217. * Sets a filter criteria parameter for the query being constructed.
  218. *
  219. * <code>
  220. * $filter = $service->buildFilterCriteria('u')
  221. * ->where('u.id = :user_id')
  222. * ->setParameter(':user_id', 1);
  223. * </code>
  224. *
  225. * @param string|integer $key The parameter position or name.
  226. * @param mixed $value The parameter value.
  227. * @param string|null $type PDO::PARAM_* or \Doctrine\DBAL\Types\Type::* constant
  228. *
  229. * @return Criteria
  230. */
  231. public function setParameter($key, $value, $type = null)
  232. {
  233. $this->queryBuilder->setParameter($key, $value, $type);
  234. return $this;
  235. }
  236. /**
  237. * Sets a collection of filter criteria parameters for the query being constructed.
  238. *
  239. * <code>
  240. * $filter = $service->buildFilterCriteria('u')
  241. * ->where('u.id = :user_id1 OR u.id = :user_id2')
  242. * ->setParameters(array(
  243. * ':user_id1' => 1,
  244. * ':user_id2' => 2
  245. * ));
  246. * </code>
  247. *
  248. * @param array $params The filter criteria parameters to set.
  249. * @param array $types array of PDO::PARAM_* or \Doctrine\DBAL\Types\Type::* constants
  250. *
  251. * @return Criteria
  252. */
  253. public function setParameters(array $params, array $types = array())
  254. {
  255. $this->queryBuilder->setParameters($params, $types);
  256. return $this;
  257. }
  258. /**
  259. * Gets all defined query parameters for the query being constructed.
  260. *
  261. * @return array The currently defined query parameters.
  262. */
  263. public function getParameters()
  264. {
  265. return $this->queryBuilder->getParameters();
  266. }
  267. /**
  268. * Gets a (previously set) query parameter of the query being constructed.
  269. *
  270. * @param mixed $key The key (index or name) of the bound parameter.
  271. *
  272. * @return mixed The value of the bound parameter.
  273. */
  274. public function getParameter($key)
  275. {
  276. return $this->queryBuilder->getParameter($key);
  277. }
  278. /**
  279. * Sets the position of the first result to retrieve (the "offset").
  280. *
  281. * @param integer $firstResult The first result to return.
  282. *
  283. * @return Criteria
  284. */
  285. public function setOffset($offset)
  286. {
  287. $this->queryBuilder->setFirstResult($offset);
  288. return $this;
  289. }
  290. /**
  291. * Gets the position of the first result the query object was set to retrieve (the "offset").
  292. * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder.
  293. *
  294. * @return integer The position of the first result.
  295. */
  296. public function getOffset()
  297. {
  298. return $this->queryBuilder->getFirstResult();
  299. }
  300. /**
  301. * Sets the maximum number of results to retrieve (the "limit").
  302. *
  303. * @param integer $maxResults The maximum number of results to retrieve.
  304. *
  305. * @return Criteria
  306. */
  307. public function setMaxResults($maxResults)
  308. {
  309. $this->queryBuilder->setMaxResults($maxResults);
  310. return $this;
  311. }
  312. /**
  313. * Gets the maximum number of results the query object was set to retrieve (the "limit").
  314. * Returns NULL if {@link setMaxResults} was not applied to this query builder.
  315. *
  316. * @return integer Maximum number of results.
  317. */
  318. public function getMaxResults()
  319. {
  320. return $this->queryBuilder->getMaxResults();
  321. }
  322. }