public function testWillUpsertAndUpdateModelWithSameKeyAsCriteria() { $model = new ModelWithCustomIdAsSecondaryKey(); $model->id = '123'; $model->name = 'john'; $em = new EntityManager($model); $finder = new Finder($model, $em); $criteria = new Criteria(); $criteria->id = $model->id; $result1 = $em->updateOne($criteria); $this->assertTrue($result1, 'That update was successfull'); $count = $finder->count(); $this->assertSame(1, $count, 'That one document was inserted'); $found = $finder->find($criteria); //found $this->assertSame('john', $found->name, 'That stored document has proper `name`'); //found $model->id = '666'; $model->name = 'joe'; $criteria->id = 123; $result2 = $em->updateOne($criteria); $this->assertTrue($result2, 'That second update was successfull'); $count2 = $finder->count(); $this->assertSame(1, $count2, 'That one document was updated, not inserted'); $criteria->id = 666; $model = $finder->find($criteria); $this->assertNotNull($model, 'That id was in fact changed'); $this->assertSame('joe', $model->name, 'That stored document has proper `name`'); $this->assertSame('666', $model->id, 'That stored document has proper `id`'); }
public function testIfWillGenerateAndStoreNonActivationKeyWithArrayCallback() { $model = new ModelWithSecretField(); $em = new EntityManager($model); $finder = new Finder($model); $model->activationKey = true; $em->upsert(); $found = $finder->find(); $this->assertSame(1, $finder->count(), 'That only one document is in collection'); $this->assertSame(40, strlen($found->activationKey), 'That non empty activation key was saved as hash'); $hash1 = $found->activationKey; $found->activationKey = ''; $em = new EntityManager($found); $em->upsert(); $found2 = $finder->find(); $this->assertSame(1, $finder->count(), 'That only one document is in collection'); $this->assertSame(40, strlen($found2->activationKey), 'That empty activation key is hash'); $this->assertSame($hash1, $found2->activationKey, 'That empty activation key was NOT saved'); }
public function testIfWillUpdateAll() { $model = new DocumentBaseAttributes(); $model->bool = false; $model->string = 'Las Vegas'; $model->save(); $model = new DocumentBaseAttributes(); $model->bool = true; $model->string = 'Las Palmas'; $model->save(); $model = new DocumentBaseAttributes(); $model->bool = false; $model->string = 'Las Cruces'; $model->save(); $em = new EntityManager($model); // With true $modifier = new Modifier(['int' => ['set' => 1]]); $criteria = new Criteria(); $criteria->bool = true; $ok = $em->updateAll($modifier, $criteria); $this->assertTrue($ok); $criteria = new Criteria(); $criteria->int = 1; $finder = new Finder($model); $modified = $finder->count($criteria); $this->assertSame(1, $modified); $found = $finder->find($criteria); $this->assertSame(1, $found->int); // With false $modifier = new Modifier(); $modifier->set('int', 2); $criteria = new Criteria(); $criteria->bool = false; $ok = $em->updateAll($modifier, $criteria); $this->assertTrue($ok); $criteria = new Criteria(); $criteria->int = 2; $finder = new Finder($model); $modified = $finder->count($criteria); $this->assertSame(2, $modified); $found = $finder->find($criteria); $this->assertSame(2, $found->int); }
/** * Restore trashed item * @return boolean * @throws Exception * @Ignored */ public function restore() { if (!$this instanceof TrashInterface) { // When trying to restore normal document instead of trash item throw new Exception(sprintf('Restore can be performed only on `%s` instance', TrashInterface::class)); } $em = new EntityManager($this->data); // Set scenario to `restore` for model, which is just about to be restored ScenarioManager::setScenario($this->data, TrashInterface::ScenarioRestore); if (!Event::valid($this->data, TrashInterface::EventBeforeRestore)) { return false; } $saved = $em->save(); if (!$saved) { return false; } $finder = new Finder($this->data); $model = $finder->find(PkManager::prepareFromModel($this->data)); if (!$model) { return false; } $eventAfter = new RestoreEvent(); $eventAfter->setTrashed($this->data); $eventAfter->setTrash($this); if (!Event::valid($model, TrashInterface::EventAfterRestore, $eventAfter)) { return false; } $trashEm = new EntityManager($this); $this->data = null; // Use deleteOne, to avoid beforeDelete event, // which should be raised only when really removing document: // when emtying trash return $trashEm->deleteOne(PkManager::prepareFromModel($this)); }
/** * Validates the attribute of the object. * If there is any error, the error message is added to the object. * @param AnnotatedInterface $model the object being validated * @param string $attribute the attribute being validated */ public function isValid(AnnotatedInterface $model, $attribute) { $value = $model->{$attribute}; if ($this->allowEmpty && empty($value)) { return true; } $className = empty($this->className) ? get_class($model) : $this->className; $compareModel = new $className(); $criteria = (new Criteria())->decorateWith($compareModel); $criteria->addCond($attribute, '==', $value); if ($this->criteria !== []) { $criteria->mergeWith($this->criteria); } ScenarioManager::setScenario($compareModel, ValidatorInterface::ScenarioValidate); $finder = new Finder($compareModel); $found = $finder->find($criteria); // Not found entirely if (null === $found) { return true; } // Same pk if (PkManager::compare($found, $model)) { return true; } $label = ManganMeta::create($model)->field($attribute)->label; $this->addError('msgTaken', ['{attribute}' => $label, '{value}' => $value]); return false; }