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 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); } } } }