Skip to content

Commit

Permalink
[Metadata] Fix modifying metadata by serializing it early
Browse files Browse the repository at this point in the history
Metadata cannot be stored as `object` ORM type, because objects are
compared (===) by reference, not by value, what means that changes
would not be persisted.

The only working way I have found is to store metadata as `text`
type and serialize it on MetadataContainer::setMetadata() method
call. However, after every metadata modification we have to use that
method once again, if not - changes won't be persisted.
  • Loading branch information
pamil committed Apr 5, 2016
1 parent 4594bee commit 0b9b9d2
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 22 deletions.
12 changes: 5 additions & 7 deletions src/Sylius/Bundle/CoreBundle/Behat/MetadataContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,7 @@ public function iShouldNotSeeTwitterApplicationCardForm()
*/
public function thereIsTheFollowingMetadata($metadataName, TableNode $table)
{
/** @var MetadataContainerInterface $metadata */
$metadata = $this->getFactory('metadata_container')->createNew();

$pageMetadata = new PageMetadata();

$metadata->setId($metadataName);
$metadata->setMetadata($pageMetadata);

foreach ($table->getRowsHash() as $key => $value) {
if ($this->createNewMetadataObjectIfNeeded($pageMetadata, $key, $value)) {
continue;
Expand All @@ -201,6 +194,11 @@ public function thereIsTheFollowingMetadata($metadataName, TableNode $table)
$this->propertyAccessor->setValue($pageMetadata, $key, $value);
}

/** @var MetadataContainerInterface $metadata */
$metadata = $this->getFactory('metadata_container')->createNew();
$metadata->setId($metadataName);
$metadata->setMetadata($pageMetadata);

$em = $this->getEntityManager();
$em->persist($metadata);
$em->flush();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Sylius\Bundle\MetadataBundle\DependencyInjection;

use Sylius\Bundle\MetadataBundle\Controller\MetadataController;
use Sylius\Bundle\MetadataBundle\Form\Type\MetadataContainerType;
use Sylius\Bundle\MetadataBundle\Model\MetadataContainer;
use Sylius\Bundle\MetadataBundle\Form\Type\MetadataContainerType;
use Sylius\Bundle\ResourceBundle\SyliusResourceBundle;
use Sylius\Component\Metadata\Factory\MetadataContainerFactory;
use Sylius\Component\Metadata\Model\MetadataContainerInterface;
Expand Down
9 changes: 2 additions & 7 deletions src/Sylius/Bundle/MetadataBundle/Model/MetadataContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
namespace Sylius\Bundle\MetadataBundle\Model;

use Sylius\Component\Metadata\Model\MetadataContainer as BaseMetadataContainer;
use Sylius\Component\Metadata\Model\MetadataContainerInterface;
use Sylius\Component\Metadata\Model\MetadataInterface;

/**
* @author Kamil Kokot <kamil.kokot@lakion.com>
*/
class MetadataContainer extends BaseMetadataContainer implements MetadataContainerInterface
class MetadataContainer extends BaseMetadataContainer
{
/**
* @var MetadataInterface
Expand All @@ -43,10 +42,6 @@ public function getMetadata()
public function setMetadata(MetadataInterface $metadata)
{
$this->metadataAsObject = $metadata;
}

public function serializeMetadata()
{
$this->metadata = serialize($this->metadataAsObject);
$this->metadata = serialize($metadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,9 @@
-->

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping">

<mapped-superclass name="Sylius\Bundle\MetadataBundle\Model\MetadataContainer" table="sylius_metadata">
<id name="id" column="id" type="string" />

<field name="metadata" column="metadata" type="text" />

<lifecycle-callbacks>
<lifecycle-callback type="prePersist" method="serializeMetadata" />
<lifecycle-callback type="preUpdate" method="serializeMetadata" />
</lifecycle-callbacks>
</mapped-superclass>

</doctrine-mapping>

0 comments on commit 0b9b9d2

Please sign in to comment.