1. Go to this page and download the library: Download dunglas/doctrine-json-odm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dunglas / doctrine-json-odm example snippets
octrine\DBAL\Types\JsonbType;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use Dunglas\DoctrineJsonOdm\Serializer;
use Dunglas\DoctrineJsonOdm\Type\JsonbDocumentType;
use Dunglas\DoctrineJsonOdm\Type\JsonDocumentType;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
use Symfony\Component\Serializer\Normalizer\UidNormalizer;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
$serializer = new Serializer([new BackedEnumNormalizer(), new UidNormalizer(), new DateTimeNormalizer(), new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()]);
if (!Type::hasType('json_document')) {
Type::addType('json_document', JsonDocumentType::class);
Type::getType('json_document')->setSerializer($serializer);
}
// jsonb_document
namespace App\Entity;
use Doctrine\ORM\Mapping\{Entity, Column, Id, GeneratedValue};
// This is a typical Doctrine ORM entity.
#[Entity]
class Foo
{
#[Column]
#[Id]
#[GeneratedValue]
public int $id;
#[Column]
public string $name;
// Can contain anything: array, objects, nested objects...
#[Column(type: 'json_document', options: ['jsonb' => true])]
public $misc;
// Works with private and protected methods with getters and setters too.
}
namespace App\Entity;
// This is NOT an entity! It's a POPO (Plain Old PHP Object). It can contain anything.
class Bar
{
public string $title;
public float $weight;
}
namespace App\Entity;
// This is NOT an entity. It's another POPO and it can contain anything.
class Baz
{
public string $name;
public int $size;
}
$foo = $entityManager->find(Foo::class, $foo->getId());
var_dump($foo->misc); // Same as what we set earlier
// …
use Dunglas\DoctrineJsonOdm\Serializer;
use Dunglas\DoctrineJsonOdm\TypeMapper;
use App\Something\Foo;
use App\SomethingElse\Bar;
// For using the built-in type mapper:
$typeMapper = new TypeMapper([
'foo' => Foo::class,
'bar' => Bar::class,
]);
// Or implement TypeMapperInterface with your own class:
$typeMapper = new MyTypeMapper();
// Then pass it into the Serializer constructor
Type::getType('json_document')->setSerializer(
new Serializer([new ArrayDenormalizer(), new ObjectNormalizer()], [new JsonEncoder()], $typeMapper)
);
// ...
#[Column(type: 'jsonb_document')]
public $foo;
// ...
// src/Kernel.php
declare(strict_types=1);
namespace App;
use Doctrine\DBAL\Types\Type;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Serializer\Encoder\JsonEncode;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
public function boot(): void
{
parent::boot();
$type = Type::getType('json_document');
$type->setSerializationContext([JsonEncode::OPTIONS => JSON_UNESCAPED_SLASHES]);
$type->setDeserializationContext([/* ... */]);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.