Skip to content

Commit 5f9da0d

Browse files
authored
Merge pull request #984 from einorler/singular-fields
Added a possibility to have singular embedded fields
2 parents fec2527 + 1b2907a commit 5f9da0d

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

Annotation/Embedded.php

+2
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ final class Embedded extends AbstractAnnotation implements PropertiesAwareInterf
2727
* @Doctrine\Common\Annotations\Annotation\Required
2828
*/
2929
public $class;
30+
31+
public $singular = false;
3032
}

Mapping/Converter.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,18 @@ protected function denormalize(array $raw, string $namespace)
8787
$setter = $fieldMeta['setter'];
8888

8989
if ($fieldMeta['embeded']) {
90-
$this->addClassMetadata($fieldMeta['class'], $fieldMeta['sub_properties']);
91-
$iterator = new ObjectIterator($fieldMeta['class'], $value, $this);
90+
$this->addClassMetadata($fieldMeta['class'], $fieldMeta['sub_properties']);
9291

93-
if ($fieldMeta['public']) {
94-
$object->{$fieldMeta['name']} = $iterator;
92+
if ($fieldMeta['singular']) {
93+
$object->$setter($this->denormalize($value, $fieldMeta['class']));
9594
} else {
96-
$object->$setter($iterator);
95+
$iterator = new ObjectIterator($fieldMeta['class'], $value, $this);
96+
97+
if ($fieldMeta['public']) {
98+
$object->{$fieldMeta['name']} = $iterator;
99+
} else {
100+
$object->$setter($iterator);
101+
}
97102
}
98103
} else {
99104
if ($fieldMeta['type'] == 'date') {

Mapping/DocumentParser.php

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ private function getClassMetadata(\ReflectionClass $class): array
130130
$fieldMapping['type'] = $this->getObjectMappingType($embeddedClass);
131131
$fieldMapping['properties'] = $this->getClassMetadata($embeddedClass);
132132
$embeddedFields[$name] = $annotation->class;
133+
$embeddedFields['singular'] = $annotation->singular;
133134
}
134135

135136
$mapping[$annotation->getName() ?? Caser::snake($name)] = array_filter($fieldMapping);
@@ -207,6 +208,7 @@ public function getPropertyMetadata(\ReflectionClass $class, bool $subClass = fa
207208
if ($annotation instanceof Embedded) {
208209
$propertyMetadata['embeded'] = true;
209210
$propertyMetadata['class'] = $annotation->class;
211+
$propertyMetadata['singular'] = $annotation->singular;
210212
$propertyMetadata['sub_properties'] = $this->getPropertyMetadata(
211213
new \ReflectionClass($annotation->class),
212214
true

Resources/doc/mapping.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -209,26 +209,31 @@ class Product
209209
/**
210210
* @var ContentMetaObject
211211
*
212-
* @ES\Embedded(class="App\Document\CategoryObject")
212+
* @ES\Embedded(class="App\Document\CategoryObject", singular=true)
213213
*/
214214
private $category;
215215
216216
//...
217217
218-
public function __construct()
218+
public funtion setCategory($category)
219219
{
220-
$this->category = new ArrayCollection();
220+
$this->category = $category;
221221
}
222222
223-
public funtion addCategory($category)
223+
public function getCategory($category)
224224
{
225-
$this->category->add($category)
225+
return $this->category;
226226
}
227227
228228
//...
229229
}
230230
```
231231

232+
Please note that if you want the category to be embedded as a singular
233+
object (not an array of objects), you need to use the `singular=true` in the
234+
annotation, otherwise it will be interpreted as a collection. Read more on
235+
embedding collections bellow.
236+
232237
And the `Category` object will look like (it's a separate class):
233238

234239
```php

0 commit comments

Comments
 (0)