PHP code example of goodwix / doctrine-json-odm

1. Go to this page and download the library: Download goodwix/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/ */

    

goodwix / doctrine-json-odm example snippets




return [
    // ...
    Goodwix\DoctrineJsonOdm\Bridge\Symfony\DoctrineJsonOdmBundle::class => ['all' => true],
];


namespace App\ODM;

use Goodwix\DoctrineJsonOdm\Annotation\ODM;

/**
 * @ODM()
 */
class Document
{
    /** @var string */
    public $title;

    /** @var string */
    public $description;
}

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use App\ODM\Document;

/**
 * @ORM\Entity()
 */
class DocumentStorage
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var int
     */
    public $id;

    /**
     * @ORM\Column(type=Document::class, nullable=true)
     *
     * @var Document
     */
    public $document;
}

$documentStorage = $entityManager->find(DocumentStorage::class, $id);
$documentStorage->document->title = 'ODM document title';
$documentStorage->document->description = 'ODM document description';



use Goodwix\DoctrineJsonOdm\Type\ODMType;
use Symfony\Component\Serializer\SerializerInterface;

class Document { }

ODMType::registerODMType(
    Document::class,
    new class implements SerializerInterface
    {
        public function serialize($data, $format, array $context = [])  { /* Implement serialize() method. */ }
        public function deserialize($data, $type, $format, array $context = [])  { /* Implement deserialize() method. */ }
    }
);



declare(strict_types=1);

namespace App\Whatever;

use Symfony\Component\Serializer\Annotation\DiscriminatorMap;

#### PHP8 Attribute ####
#[DiscriminatorMap(
    typeProperty: 'type',
    mapping: [
        'myChildTypeName' => 'App\Whatever\Child',
        'myChild2TypeName' => 'App\Whatever\Child2',
    ]
)]
####> PHP8 Attribute ####


#### PHP < PHP8 ####
/**
 * @DiscriminatorMap(typeProperty="type", mapping={
 *    "myChildTypeName"="App\Whatever\Child",
 *    "myChild2TypeName"="App\Whatever\Child2"
 * })
 */
####> PHP < PHP8 ####


abstract class MyAbstract
{
}

class Child extends MyAbstract
{}


class Child2 extends MyAbstract
{}