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); }
/** * Finds document with the specified attributes. * Attributes should be specified as key-value pairs. * This allows easier syntax for simple queries. * * Example: * ```php * $attributes = [ * 'name' => 'John', * 'title' => 'dr' * ]; * ``` * * @param mixed[] Array of stributes and values in form of ['attributeName' => 'value'] * @return AnnotatedInterface|null */ public function findByAttributes(array $attributes) { $criteria = new Criteria(); $criteria->decorateWith($this->model); foreach ($attributes as $name => $value) { $criteria->addCond($name, '==', $value); } return $this->find($criteria); }
/** * 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); }