public function testIfWillCountByCriteria() { $model = new WithBaseAttributes(); $model->string = 'foo'; $em = new EntityManager($model); $em->insert(); $model = new WithBaseAttributes(); $model->string = 'foo'; $em->insert($model); $model = new WithBaseAttributes(); $model->string = 'foo'; $em->insert($model); // Some other models $model = new WithBaseAttributes(); $model->string = 'blah'; $em->insert($model); $model = new WithBaseAttributes(); $model->string = 'blah'; $em->insert($model); $finder = new Finder($model); $count = $finder->count(); $this->assertSame(5, $count); $criteria = new Criteria(); $criteria->addCond('string', '==', 'foo'); $criteriaCount = $finder->count($criteria); $this->assertSame(3, $criteriaCount); }
public function testIfWillProperlyCreateCriteriaWithAllOperators() { $criteria = new Criteria(); foreach (Criteria::$operators as $name => $mongoName) { $criteria->addCond(sprintf('title%s', str_replace('$', '.', $mongoName)), $name, 1); } $conditions = $criteria->getConditions(); }
public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class) { $fieldMeta = ManganMeta::create($model)->field($name); $relMeta = $fieldMeta->related; $relModel = new $relMeta->class(); $criteria = new Criteria(null, $relModel); if (empty($relMeta->join)) { throw new InvalidArgumentException(sprintf('Parameter `join` is required for Related annotation, model `%s`, field `%s`', get_class($model), $name)); } foreach ($relMeta->join as $source => $rel) { assert($model->{$source} !== null); $criteria->addCond($rel, '==', $model->{$source}); } $criteria->setSort(new Sort($relMeta->sort)); if ($relMeta->single) { $model->{$name} = (new Finder($relModel))->find($criteria); } else { $model->{$name} = (new Finder($relModel))->findAll($criteria); } }
private function getTrashed($item, &$trashed) { $criteria = new Criteria(); $criteria->addCond('data._id', '==', $item->_id); $trashed = (new Trash())->find($criteria); }
/** * Apply scopes to criteria, will create criteria object if not provided and pass it by reference * @param CriteriaInterface|array|null $criteria * @return CriteriaInterface */ public function apply(&$criteria = null) { if (null === $criteria) { return $this->getModelCriteria(); } elseif (is_array($criteria)) { $criteria = new Criteria($criteria); } $criteria->mergeWith($this->criteria); $criteria->mergeWith($this->getModelCriteria()); return $criteria; }
/** * Resets all scopes and criteria applied including default scope. * * @return Finder * @since v1.0 */ public function resetScope() { $this->_criteria = new Criteria(); $this->_criteria->decorateWith($this->model); return $this; }
/** * Register event handlers for parent of parent-child relation. * * @param AnnotatedInterface $parent * @param string $childClass */ public function registerParent(AnnotatedInterface $parent, $childClass) { if (!ClassChecker::exists($childClass)) { throw new UnexpectedValueException(sprintf('Class `%s` not found', $childClass)); } // Delete all of this child items after removing from trash $beforeDelete = function (ModelEvent $event) use($parent, $childClass) { $model = $event->sender; $event->isValid = true; if ($model instanceof $parent) { $child = new $childClass(); $criteria = new Criteria(null, $child); $criteria->parentId = $model->_id; $event->isValid = $child->deleteAll($criteria); } }; Event::on($parent, EntityManagerInterface::EventBeforeDelete, $beforeDelete); // Trash all child items from parent item $afterTrash = function (ModelEvent $event) use($parent, $childClass) { $model = $event->sender; $event->isValid = true; if ($model instanceof $parent) { $child = new $childClass(); $criteria = new Criteria(null, $child); $criteria->parentId = $model->_id; $items = $child->findAll($criteria); // No items found, so skip if (empty($items)) { $event->isValid = true; return true; } // Trash in loop all items foreach ($items as $item) { if (!$item->trash()) { $event->isValid = false; return false; } } } }; Event::on($parent, TrashInterface::EventAfterTrash, $afterTrash); // Restore all child items from parent, but only those after it was trashed. // This will keep previously trashed items in trash $afterRestore = function (RestoreEvent $event) use($parent, $childClass) { $model = $event->sender; if ($model instanceof $parent) { $child = new $childClass(); $trash = $event->getTrash(); $criteria = new Criteria(null, $trash); // Conditions decorator do not work with dots so sanitize manually. $s = new Sanitizer($child); $id = $s->write('parentId', $model->_id); $criteria->addCond('data.parentId', '==', $id); // Restore only child items trashed when blog was trashed - skip earlier $criteria->addCond('createDate', 'gte', $trash->createDate); $trashedItems = $trash->findAll($criteria); if (empty($trashedItems)) { $event->isValid = true; return true; } // Restore all items foreach ($trashedItems as $trashedItem) { $trashedItem->restore(); } } $event->isValid = true; }; Event::on($parent, TrashInterface::EventAfterRestore, $afterRestore); }