示例#1
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`');
 }
示例#2
0
 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);
             }
         }
     }
 }