ODM.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Event\Adapter;
  3. use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM;
  4. use Doctrine\ODM\MongoDB\DocumentManager;
  5. use Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo;
  6. use Doctrine\ODM\MongoDB\Cursor;
  7. /**
  8. * Doctrine event adapter for ODM adapted
  9. * for sluggable behavior
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @package Gedmo\Sluggable\Mapping\Event\Adapter
  13. * @subpackage ODM
  14. * @link http://www.gediminasm.org
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. final class ODM extends BaseAdapterODM
  18. {
  19. /**
  20. * Loads the similar slugs
  21. *
  22. * @param DocumentManager $dm
  23. * @param object $object
  24. * @param ClassMetadataInfo $meta
  25. * @param array $config
  26. * @param string $slug
  27. * @return array
  28. */
  29. public function getSimilarSlugs(DocumentManager $dm, $object, ClassMetadataInfo $meta, array $config, $slug)
  30. {
  31. $qb = $dm->createQueryBuilder($meta->name);
  32. $identifier = $this->extractIdentifier($dm, $object);
  33. if ($identifier) {
  34. $qb->field($meta->identifier)->notEqual($identifier);
  35. }
  36. $qb->where("function() {
  37. return this.{$config['slug']}.indexOf('{$slug}') === 0;
  38. }");
  39. $q = $qb->getQuery();
  40. $q->setHydrate(false);
  41. $result = $q->execute();
  42. if ($result instanceof Cursor) {
  43. $result = $result->toArray();
  44. }
  45. return $result;
  46. }
  47. }