Skip to content

Commit

Permalink
feat(NodeType): NodeTypeImporter now removes extra fields from databa…
Browse files Browse the repository at this point in the history
…se when not present on .json files
  • Loading branch information
ambroisemaupate committed Jul 26, 2023
1 parent d2cd965 commit c59919e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/RoadizCoreBundle/src/Entity/NodeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class NodeType extends AbstractEntity implements NodeTypeInterface
* @var Collection<int, NodeTypeField>
*/
#[
ORM\OneToMany(mappedBy: "nodeType", targetEntity: NodeTypeField::class, cascade: ["persist", "merge"]),
ORM\OneToMany(mappedBy: "nodeType", targetEntity: NodeTypeField::class, cascade: ["all"]),
ORM\OrderBy(["position" => "ASC"]),
Serializer\Groups(["node_type"]),
SymfonySerializer\Groups(["node_type"]),
Expand Down
3 changes: 0 additions & 3 deletions lib/RoadizCoreBundle/src/Importer/NodeTypesImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
use RZ\Roadiz\Core\Handlers\HandlerFactoryInterface;
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\EntityHandler\NodeTypeHandler;
use RZ\Roadiz\CoreBundle\Message\UpdateNodeTypeSchemaMessage;
use RZ\Roadiz\CoreBundle\Serializer\ObjectConstructor\TypedObjectConstructorInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;

class NodeTypesImporter implements EntityImporterInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use JMS\Serializer\DeserializationContext;
use JMS\Serializer\Exception\ObjectConstructionException;
use JMS\Serializer\Metadata\ClassMetadata;
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
use RZ\Roadiz\CoreBundle\Entity\NodeType;

class NodeTypeObjectConstructor extends AbstractTypedObjectConstructor
Expand All @@ -18,6 +20,39 @@ public function supports(string $className, array $data): bool
return $className === NodeType::class && array_key_exists('name', $data);
}

public function construct(
DeserializationVisitorInterface $visitor,
ClassMetadata $metadata,
$data,
array $type,
DeserializationContext $context
): ?object
{
$nodeType = parent::construct($visitor, $metadata, $data, $type, $context);

if ($nodeType instanceof NodeType && \is_array($data) && \array_key_exists('fields', $data)) {
$nodeType = $this->removeExtraFields($nodeType, $data);
}

return $nodeType;
}

protected function removeExtraFields(NodeType $nodeType, array $data): NodeType
{
$fieldsName = array_map(function ($field) {
return $field['name'];
}, $data['fields']);

foreach ($nodeType->getFields() as $field) {
if (!\in_array($field->getName(), $fieldsName)) {
$nodeType->getFields()->removeElement($field);
$field->setNodeType(null);
}
}

return $nodeType;
}

/**
* @inheritDoc
*/
Expand Down

0 comments on commit c59919e

Please sign in to comment.