Last active
July 20, 2018 14:51
-
-
Save porozhnyy/da9be11601047ffdfc2ee7b6feb88070 to your computer and use it in GitHub Desktop.
Laravel-doctrine odm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MyParams | |
{ | |
protected $status = false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment