Skip to content

Instantly share code, notes, and snippets.

@porozhnyy
Last active July 20, 2018 14:51
Show Gist options
  • Save porozhnyy/da9be11601047ffdfc2ee7b6feb88070 to your computer and use it in GitHub Desktop.
Save porozhnyy/da9be11601047ffdfc2ee7b6feb88070 to your computer and use it in GitHub Desktop.
Laravel-doctrine odm
<?php
use ObjectNormalizer; //see https://github.com/Hunternnm/laravel-doctrine-odm/blob/master/src/ObjectNormalizer.php
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer as BaseObjectNormalizer;
class MyEntity
{
/**
* @var MyParams
* @ORM\Column(type="json_document", options={"jsonb": true}) //see https://github.com/Hunternnm/laravel-doctrine-odm/blob/master/src/JsonDocumentType.php
*/
protected $myParams;
public function setMyParams($params)
{
if (\is_object($params)) {
$this->myParams = $params;
}
if (\is_array($params)) {
$this->myParams = (new Serializer(
[new ObjectNormalizer(new BaseObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter()))],
[new JsonEncoder()]
))->deserialize(json_encode(array_merge(['#type' => MyParams::class], $params)), '', 'json');
}
}
}
<?php
class MyParams
{
protected $status = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment