QueryFactory.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Drupal\Core\Entity\Query;
  3. use Drupal\Core\Entity\EntityManagerInterface;
  4. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  5. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  6. /**
  7. * Factory class Creating entity query objects.
  8. *
  9. * Any implementation of this service must call getQuery()/getAggregateQuery()
  10. * of the corresponding entity storage.
  11. *
  12. * @see \Drupal\Core\Entity\EntityStorageBase::getQuery()
  13. *
  14. * @deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Use
  15. * \Drupal\Core\Entity\EntityStorageInterface::getQuery() or
  16. * \Drupal\Core\Entity\EntityStorageInterface::getAggregateQuery() instead.
  17. */
  18. class QueryFactory implements ContainerAwareInterface {
  19. use ContainerAwareTrait;
  20. /**
  21. * Stores the entity manager used by the query.
  22. *
  23. * @var \Drupal\Core\Entity\EntityManagerInterface
  24. */
  25. protected $entityManager;
  26. /**
  27. * Constructs a QueryFactory object.
  28. *
  29. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
  30. * The entity manager used by the query.
  31. */
  32. public function __construct(EntityManagerInterface $entity_manager) {
  33. $this->entityManager = $entity_manager;
  34. }
  35. /**
  36. * Returns a query object for a given entity type.
  37. *
  38. * @param string $entity_type_id
  39. * The entity type ID.
  40. * @param string $conjunction
  41. * - AND: all of the conditions on the query need to match.
  42. * - OR: at least one of the conditions on the query need to match.
  43. *
  44. * @return \Drupal\Core\Entity\Query\QueryInterface
  45. * The query object that can query the given entity type.
  46. */
  47. public function get($entity_type_id, $conjunction = 'AND') {
  48. return $this->entityManager->getStorage($entity_type_id)->getQuery($conjunction);
  49. }
  50. /**
  51. * Returns an aggregated query object for a given entity type.
  52. *
  53. * @param string $entity_type_id
  54. * The entity type ID.
  55. * @param string $conjunction
  56. * - AND: all of the conditions on the query need to match.
  57. * - OR: at least one of the conditions on the query need to match.
  58. *
  59. * @return \Drupal\Core\Entity\Query\QueryAggregateInterface
  60. * The aggregated query object that can query the given entity type.
  61. */
  62. public function getAggregate($entity_type_id, $conjunction = 'AND') {
  63. return $this->entityManager->getStorage($entity_type_id)->getAggregateQuery($conjunction);
  64. }
  65. }