コード例 #1
0
 public function testIfWillDeleteEmbeddedImage()
 {
     $fileName = __DIR__ . '/logo-1024.png';
     $md5 = md5_file($fileName);
     // NOTE: Must work fine even if _id is not set
     $model = new ModelWithEmbeddedImage();
     $model->file = new Image();
     $model->file->set($fileName);
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     /* @var $found ModelWithEmbeddedImage */
     $file = $found->file->get()->getBytes();
     $this->assertSame($md5, md5($file));
     // Resize image
     $params = new ImageParams();
     $params->width = 100;
     $params->height = 100;
     $resized = $found->file->get($params)->getBytes();
     // Check if was resized
     $this->assertTrue($file > $resized);
     $mangan = new Mangan();
     $gfs = $mangan->getDbInstance()->getGridFS();
     $tmp = $mangan->getDbInstance()->getGridFS(File::TmpPrefix);
     $criteria = ['parentId' => $found->file->_id];
     $this->assertSame(1, $gfs->count($criteria));
     $this->assertSame(1, $tmp->count($criteria));
     $deleted = $found->delete();
     $this->assertTrue($deleted);
     $this->assertSame(0, $gfs->count($criteria));
     $this->assertSame(0, $tmp->count($criteria));
 }
コード例 #2
0
 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`');
 }
コード例 #3
0
ファイル: ImageTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillResizeSavedImage()
 {
     // Temp file location
     $fileName = __DIR__ . '/logo-1024.png';
     $md5 = md5_file($fileName);
     $model = new ModelWithEmbeddedFile();
     $model->file = new Image();
     $model->file->set($fileName);
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     /* @var $found ModelWithEmbeddedFile */
     $file = $found->file->get()->getBytes();
     $this->assertSame($fileName, $found->file->filename);
     $this->assertSame($md5, md5($file));
     $image = $found->file;
     $params = new ImageParams();
     $params->width = 100;
     $params->height = 100;
     /* @var $image Image */
     $scaledName = tempnam('/tmp/', 'image-test') . '.png';
     $image->get($params)->write($scaledName);
     $this->assertTrue(file_exists($scaledName));
     $gd = new GD($scaledName);
     $dimensions = (object) $gd->getCurrentDimensions();
     codecept_debug($dimensions);
     $this->assertSame($params->width, $dimensions->width);
     $this->assertSame($params->height, $dimensions->height);
 }
コード例 #4
0
ファイル: SingleTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillDeepEmbed()
 {
     $model = new PlainDeepEmbedded();
     $model->_id = new MongoId();
     $withPlain = new WithPlainEmbedded();
     $stats = new SimplePlainEmbedded();
     $stats->active = false;
     $stats->name = 'buried stats';
     $stats->visits = 100002;
     $withPlain->title = 'first level';
     $withPlain->stats = $stats;
     $model->withPlain = $withPlain;
     $model->title = 'deep blue';
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     $this->assertNotNull($found);
     $this->assertTrue($found instanceof PlainDeepEmbedded);
     $this->assertSame($found->title, $model->title);
     $this->assertNotNull($found->withPlain);
     $this->assertTrue($found->withPlain instanceof WithPlainEmbedded);
     $this->assertSame($found->withPlain->title, $model->withPlain->title);
     $this->assertNotNull($found->withPlain->stats);
     $this->assertTrue($found->withPlain->stats instanceof SimplePlainEmbedded);
 }
コード例 #5
0
ファイル: DbRefManager.php プロジェクト: maslosoft/mangan
 /**
  * Save referenced model
  * @param AnnotatedInterface $referenced
  * @param DbRef $dbRef
  */
 public static function save(AnnotatedInterface $referenced, DbRef $dbRef)
 {
     // Ensure ref is same as referenced model
     PkManager::applyToModel($referenced, $dbRef->pk);
     $em = new EntityManager($referenced);
     $em->save();
 }
コード例 #6
0
ファイル: SaveTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillPreventSaveOfModelIfNotValid()
 {
     $model = new ModelWithRequiredValidator();
     $em = new EntityManager($model);
     $saved = $em->save();
     $this->assertFalse($saved);
     $model->login = '******';
     $saved2 = $em->save();
     $this->assertTrue($saved2);
 }
コード例 #7
0
 public function testIfWillAllowNullForMongoId()
 {
     $model = new ModelWithNullableMongoId();
     $em = new EntityManager($model);
     $em->save();
     $this->assertInstanceOf(\MongoId::class, $model->_id);
     $this->assertNull($model->parentId);
     $model->parentId = new \MongoId();
     $em->save();
     $this->assertInstanceOf(\MongoId::class, $model->_id);
     $this->assertInstanceOf(\MongoId::class, $model->parentId);
 }
コード例 #8
0
ファイル: EmbedTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillProperlyStoreAndLoadTree()
 {
     $model = new ModelWithEmbedTree();
     $id = $model->_id = new MongoId();
     $model->name = 'plants';
     $model->children = [new ModelWithEmbedTree('trees', [new ModelWithEmbedTree('oaks', [new ModelWithEmbedTree('Quercus'), new ModelWithEmbedTree('Cerris')]), new ModelWithEmbedTree('chestnuts', [new ModelWithEmbedTree('Sativa'), new ModelWithEmbedTree('Castanea')])])];
     $this->checkTree($model);
     $em = new EntityManager($model);
     $em->save();
     $found = (new Finder($model))->findByPk($id);
     $this->assertSame((string) $id, (string) $found->_id);
     $this->checkTree($found);
 }
コード例 #9
0
ファイル: SingleTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillSaveAndLoadRelatedModel()
 {
     $model = new ModelWithSingleSimpleRelation();
     $id = $model->_id = new MongoId();
     $model->stats = new RelatedStats();
     $model->stats->name = 'one';
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $found = $finder->findByPk($id);
     $this->assertInstanceOf(RelatedStats::class, $model->stats);
     $this->assertSame($model->stats->name, $found->stats->name);
 }
コード例 #10
0
ファイル: FindByTest.php プロジェクト: maslosoft/mangantest
 public function testIfCanFindByAttributes()
 {
     $model = new WithBaseAttributes();
     $model->int = 10;
     $em = new EntityManager($model);
     $em->insert();
     $model = new WithBaseAttributes();
     $model->int = 20;
     $em->insert($model);
     $finder = new Finder($model);
     $found = $finder->findByAttributes(['int' => 10]);
     $this->assertInstanceof(WithBaseAttributes::class, $found);
     $this->assertSame(10, $found->int);
 }
コード例 #11
0
ファイル: ExistsTest.php プロジェクト: maslosoft/mangantest
 public function testIfExistsByCriteria()
 {
     $model = new WithBaseAttributes();
     $model->int = 10;
     $em = new EntityManager($model);
     $em->insert();
     $model = new WithBaseAttributes();
     $model->int = 20;
     $em->insert($model);
     $finder = new Finder($model);
     $criteria = new Criteria();
     $criteria->int = 10;
     $this->assertTrue($finder->exists($criteria));
     $criteria = new Criteria();
     $criteria->int = 100;
     $this->assertFalse($finder->exists($criteria));
 }
コード例 #12
0
ファイル: SingleTest.php プロジェクト: maslosoft/mangantest
 public function testIfPlainObjectWillRefer()
 {
     $model = new WithPlainDbRef();
     $model->_id = new MongoId();
     $model->title = 'stats';
     $model->stats = new SimplePlainDbRef();
     $model->stats->active = true;
     $model->stats->name = 'www';
     $model->stats->visits = 10000;
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     $this->assertNotNull($found);
     $this->assertSame($found->title, $model->title);
     $this->assertTrue($found instanceof WithPlainDbRef);
     $this->assertTrue($found->stats instanceof SimplePlainDbRef);
 }
コード例 #13
0
ファイル: ConvertTest.php プロジェクト: maslosoft/mangantest
 public function testIfWIllConvertDbRefToJsonArray()
 {
     $model = new WithPlainDbRef();
     $model->_id = new MongoId();
     $model->title = 'stats';
     $model->stats = new SimplePlainDbRef();
     $model->stats->active = true;
     $model->stats->name = 'www';
     $model->stats->visits = 10000;
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     $json = JsonArray::fromModel($found);
     $this->assertSame($found->title, $json['title']);
     $this->assertSame($found->stats->active, $json['stats']['active']);
     $this->assertSame($found->stats->name, $json['stats']['name']);
 }
コード例 #14
0
ファイル: SimpleTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillProperlyStoreAndLoadTree()
 {
     $model = new ModelWithSimpleTree();
     $id = $model->_id = new MongoId();
     $model->name = 'plants';
     $model->children = [new ModelWithSimpleTree('trees', [new ModelWithSimpleTree('oaks', [new ModelWithSimpleTree('Quercus'), new ModelWithSimpleTree('Cerris')]), new ModelWithSimpleTree('chestnuts', [new ModelWithSimpleTree('Sativa'), new ModelWithSimpleTree('Castanea')])])];
     $this->checkTree($model);
     $em = new EntityManager($model);
     $em->save();
     $found = (new Finder($model))->findByPk($id);
     $this->assertSame((string) $id, (string) $found->_id);
     $this->checkTree($found);
     // Find some sub tree
     $attributes = ['name' => 'oaks'];
     $found2 = (new Finder($model))->findByAttributes($attributes);
     $this->assertNotNull($found2);
     $this->assertSame(2, count($found2->children), 'That has 2 child nodes');
     $this->assertSame('Cerris', $found2->children[1]->name, 'That has 2nd child node of name Cerris');
 }
コード例 #15
0
ファイル: SecretTest.php プロジェクト: maslosoft/mangantest
 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');
 }
コード例 #16
0
ファイル: ModifierTest.php プロジェクト: maslosoft/mangantest
 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);
 }
コード例 #17
0
ファイル: Finder.php プロジェクト: maslosoft/mangan
 /**
  * Constructor
  *
  * @param object $model Model instance
  * @param EntityManagerInterface $em
  * @param Mangan $mangan
  */
 public function __construct($model, $em = null, $mangan = null)
 {
     $this->model = $model;
     $this->sm = new ScopeManager($model);
     if (null === $mangan) {
         $mangan = Mangan::fromModel($model);
     }
     $this->em = $em ?: EntityManager::create($model, $mangan);
     $this->mn = $mangan;
     $this->withCursor($this->mn->useCursor);
 }
コード例 #18
0
ファイル: PlainTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillFindPlainDocumentWithCompositePkWithWrongPkTypes()
 {
     //exit;
     $model = new CompositePrimaryKey();
     $model->title = 'fooo';
     $IdOne = new MongoId();
     $IdTwo = 2;
     $IdThree = (string) new MongoId();
     $model->primaryOne = $IdOne;
     $model->primaryTwo = $IdTwo;
     $model->primaryThree = $IdThree;
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     // Wrong types on purpose
     $found = $finder->findByPk(['primaryOne' => (string) $IdOne, 'primaryTwo' => (string) $IdTwo, 'primaryThree' => new MongoId($IdThree)]);
     $this->assertNotNull($found);
     $this->assertTrue($found instanceof CompositePrimaryKey);
     $this->assertSame($model->title, $found->title);
 }
コード例 #19
0
ファイル: I18NTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillStoreI18NFields()
 {
     $model = new ModelWithI18N();
     $model->setLanguages(['en', 'pl']);
     $model->_id = new MongoId();
     $model->setLang('en');
     $model->title = 'english';
     $model->active = true;
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model);
     $found = $finder->findByPk($model->_id);
     $this->assertNotNull($found);
     $found->setLang('en');
     $this->assertSame($model->title, $found->title);
     $this->assertSame($model->active, $found->active);
     $found->setLang('pl');
     $model->title = 'english';
     $model->active = true;
 }
コード例 #20
0
 public function testIfWillSkipUnsafeAttributeOnSaveWhenUpdatingModel()
 {
     $model = new ModelWithUnsafeAttribute();
     $model->active = true;
     $em = new EntityManager($model);
     $em->save();
     $finder = new Finder($model, $em);
     $found = $finder->findByPk($model->_id);
     $this->assertNotNull($found, 'That model was saved');
     $this->assertTrue($found->active, 'That value was set');
     // Update model from external data
     // NOTE: Creating model from external data will not work, as there is no way to take value from
     $data = ['active' => false];
     $model2 = SafeArray::toModel($data, null, $found);
     $this->assertTrue($model2->active, 'That value was ignored on mass set, as it is unsafe');
     $em2 = new EntityManager($model2);
     $em2->save();
     $found2 = $finder->findByPk($found->_id);
     $this->assertTrue($found2->active, 'That value was not updated in db');
     $this->assertSame(1, $finder->count(), 'That only one model was saved');
 }
コード例 #21
0
 public function testIfWillFindAllUsingCursor()
 {
     $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);
     $finder = new Finder($model);
     $cursor = $finder->withCursor()->findAll();
     $this->assertSame(3, count($cursor));
     $this->assertInstanceOf(Cursor::class, $cursor);
     foreach ($cursor as $found) {
         $this->assertInstanceof(WithBaseAttributes::class, $found);
         $this->assertSame('foo', $found->string);
     }
 }
コード例 #22
0
ファイル: UpdateTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillUpdateByModifyDocument()
 {
     $model = new ModelWithI18N();
     $model->_id = new \MongoId();
     $model->active = true;
     $model->title = 'foo';
     $em = new EntityManager($model);
     $finder = new Finder($model);
     $em->save();
     $found = $finder->findByPk($model->_id);
     $this->assertSame($model->title, $found->title);
     $this->assertSame($model->active, $found->active);
     $em = new EntityManager($found);
     $found->title = 'bar';
     // This attribute should be ignored
     $found->active = false;
     $em->update(['title'], true);
     $updated = $finder->findByPk($model->_id);
     $this->assertSame($found->title, $updated->title);
     $this->assertSame($model->active, $updated->active);
 }
コード例 #23
0
ファイル: CountTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillCountByAttributes()
 {
     $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);
     $attributesCount = $finder->countByAttributes(['string' => 'foo']);
     $this->assertSame(3, $attributesCount);
 }
コード例 #24
0
ファイル: ArrayTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillReferenceArrayOfDifferentTypeDocuments()
 {
     $model = new WithPlainDbRefArrayDifferentTypes();
     $id = new MongoId();
     $model->_id = $id;
     $model->title = 'some title';
     $data = [['active' => false, 'name' => 'stats one', 'visits' => 100, '_type' => SimplePlainDbRef::class], ['active' => true, 'name' => 'stats two', 'visits' => 1000, '_type' => SimplePlainDbRefSecond::class], ['active' => false, 'name' => 'stats three', 'visits' => 10000, '_type' => SimplePlainDbRefSecond::class]];
     $stats = [];
     foreach ($data as $key => $value) {
         $referenced = new $value['_type']();
         foreach ($value as $field => $fieldValue) {
             if ($field == '_type') {
                 continue;
             }
             $referenced->{$field} = $fieldValue;
         }
         $stats[$key] = $referenced;
         $model->stats[$key] = $referenced;
     }
     $em = new EntityManager($model);
     $em->insert();
     $finder = new Finder($model);
     $found = $finder->findByPk($id);
     $this->assertNotNull($found);
     $this->assertTrue($found instanceof WithPlainDbRefArray);
     $this->assertSame(count($stats), count($found->stats));
     foreach ($data as $key => $value) {
         $this->assertNotNull($found->stats[$key]);
         //			$this->write(sprintf('Should be of type: %s', $value['_type']));
         $this->assertSame(get_class($found->stats[$key]), $value['_type']);
         $this->assertTrue($found->stats[$key] instanceof $value['_type']);
         foreach ($value as $field => $fieldValue) {
             if ($field == '_type') {
                 continue;
             }
             $this->assertSame($found->stats[$key]->{$field}, $fieldValue);
         }
     }
 }
コード例 #25
0
ファイル: RelatedDecorator.php プロジェクト: maslosoft/mangan
 public function write($model, $name, &$dbValues, $transformatorClass = TransformatorInterface::class)
 {
     if (!empty($model->{$name})) {
         // Store empty field to trigger decorator read
         $dbValues[$name] = null;
         $fieldMeta = ManganMeta::create($model)->field($name);
         $relMeta = $fieldMeta->related;
         if ($relMeta->single) {
             $models = [$model->{$name}];
         } else {
             $models = $model->{$name};
         }
         $order = 0;
         foreach ($models as $relModel) {
             $fields = [];
             foreach ($relMeta->join as $source => $rel) {
                 $fields[] = $rel;
                 assert(isset($model->{$source}));
                 $relModel->{$rel} = $model->{$source};
             }
             if (!empty($relMeta->orderField)) {
                 $fields[] = $relMeta->orderField;
                 $fields = array_unique($fields);
                 $relModel->order = $order;
                 $order++;
             }
             $em = new EntityManager($relModel);
             if ($relMeta->updatable) {
                 // Update whole model
                 $em->upsert();
             } else {
                 // Update only relation info
                 $criteria = PkManager::prepareFromModel($relModel);
                 $em->updateOne($criteria, $fields);
             }
         }
     }
 }
コード例 #26
0
 public function testIfWillResolveNotFoundClass()
 {
     define('BogusClass', 'SomeClass');
     $model = new WithPlainEmbedded();
     $model->_id = new MongoId();
     $model->stats = new SimplePlainEmbedded();
     $em = new EntityManager($model);
     $em->save();
     $pkCriteria = PkManager::prepareFromModel($model)->getConditions();
     $set = ['$set' => ['stats._class' => BogusClass]];
     $em->getCollection()->update($pkCriteria, $set);
     $finder = new Finder($model);
     try {
         $finder->findByPk($model->_id);
         $this->assertFalse(true);
     } catch (ManganException $ex) {
         $this->assertTrue(true);
     }
     // Attach class not found handlers
     new NotFoundResolver($model, [BogusClass => SimplePlainEmbedded::class]);
     $found = $finder->findByPk($model->_id);
     $this->assertInstanceOf(WithPlainEmbedded::class, $found);
     $this->assertInstanceOf(SimplePlainEmbedded::class, $found->stats);
 }
コード例 #27
0
 public function testIfWillDeleteSimpleDocumentFromRealSignal()
 {
     $model = new SimpleModel();
     $model->_id = new MongoId();
     $model->title = 'Connecticut is a state';
     // Signal save index
     EntityManager::create($model)->save();
     $mnl = Manganel::fly();
     $client = $mnl->getClient();
     $get = ['index' => $mnl->index, 'type' => CollectionNamer::nameCollection($model), 'id' => (string) $model->_id];
     $found = $client->get($get)['_source'];
     $this->assertSame($model->title, $found['title']);
     // Signal delete index
     EntityManager::create($model)->delete();
     try {
         $client->get($get);
         $this->assertFalse(true, 'That missing exception was not thrown');
     } catch (Missing404Exception $e) {
         $this->assertTrue(true, 'That missing exception was thrown');
     }
 }
コード例 #28
0
ファイル: FindAllTest.php プロジェクト: maslosoft/mangantest
 public function testIfWillFindAllByCompositePks()
 {
     $pks[] = ['primaryOne' => new \MongoId(), 'primaryTwo' => 1, 'primaryThree' => 'one', 'title' => 'xxx'];
     $pks[] = ['primaryOne' => new \MongoId(), 'primaryTwo' => 2, 'primaryThree' => 'two', 'title' => 'yyy'];
     $pks[] = ['primaryOne' => new \MongoId(), 'primaryTwo' => 3, 'primaryThree' => 'three', 'title' => 'zzz'];
     foreach ($pks as $i => $keys) {
         $model = new CompositePrimaryKey();
         $em = new EntityManager($model);
         foreach ($keys as $field => $value) {
             $model->{$field} = $value;
         }
         $em->insert();
     }
     $model = new CompositePrimaryKey();
     $em = new EntityManager($model);
     $model->primaryOne = new \MongoId();
     $model->primaryTwo = 12;
     $model->primaryThree = 'ddd';
     $em->insert();
     $finder = new Finder($model);
     $all = $finder->findAllByPk($pks);
     $this->assertSame(3, count($all));
     foreach ($all as $found) {
         $this->assertInstanceof(CompositePrimaryKey::class, $found);
         $this->assertTrue(in_array($found->title, ['xxx', 'yyy', 'zzz']));
     }
 }
コード例 #29
0
ファイル: TrashableTrait.php プロジェクト: maslosoft/mangan
 /**
  * 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));
 }
コード例 #30
0
 /**
  * Get entity manager instance
  * @return EntityManagerInterface|EntityManager
  */
 private function _getEm()
 {
     if (null === $this->_em) {
         $this->_em = EntityManager::create($this);
     }
     return $this->_em;
 }