PHP code example of vuryss / doctrine-lazy-json-odm

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

    

vuryss / doctrine-lazy-json-odm example snippets


return [
    // ...
    \Vuryss\DoctrineLazyJsonOdm\DoctrineLazyJsonOdmBundle::class => ['all' => true],
];



use Doctrine\ORM\Mapping as ORM;
use Vuryss\DoctrineLazyJsonOdm\Lazy\LazyJsonArray;
use Vuryss\DoctrineLazyJsonOdm\Type\LazyJsonDocumentType;

#[ORM\Entity]
#[ORM\Table(name: 'users')]
class User
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 255)]
    private string $name;

    #[ORM\Column(type: 'string', length: 255, unique: true)]
    private string $email;

    // Single object stored as JSON with lazy loading
    #[ORM\Column(type: LazyJsonDocumentType::NAME, nullable: true)]
    private ?Profile $profile = null;

    // Array of objects stored as JSON with lazy loading
    #[ORM\Column(type: LazyJsonDocumentType::NAME, nullable: true)]
    private ?LazyJsonArray $orders = null;

    // Getters and setters...
}



readonly class Profile
{
    public function __construct(
        public string $firstName,
        public string $lastName,
        public int $age,
        public ?string $bio = null,
        public ?Address $address = null,
        public array $socialLinks = [],
    ) {
    }

    public function getFullName(): string
    {
        return $this->firstName . ' ' . $this->lastName;
    }
}

readonly class Address
{
    public function __construct(
        public string $street,
        public string $city,
        public string $postalCode,
        public string $country,
        public ?string $state = null,
    ) {
    }
}



// Create and persist entities
$address = new Address('123 Main St', 'New York', '10001', 'USA');
$profile = new Profile('John', 'Doe', 30, 'Software Developer', $address);

$user = new User('John Doe', '[email protected]');
$user->setProfile($profile);

$entityManager->persist($user);
$entityManager->flush();

// Retrieve and access lazy-loaded data
$user = $userRepository->find(1);

// Profile is lazy-loaded only when accessed
$fullName = $user->getProfile()->getFullName(); // Triggers lazy loading
$city = $user->getProfile()->address->city; // Nested objects work transparently



use Vuryss\DoctrineLazyJsonOdm\Lazy\LazyJsonArray;

// Create array of objects
$orderItems = [
    new OrderItem(1, 'Laptop', 1, '999.99', '999.99'),
    new OrderItem(2, 'Mouse', 1, '29.99', '29.99'),
];

$user->setOrders(new LazyJsonArray($orderItems));
$entityManager->flush();

// Access lazy array
$orders = $user->getOrders(); // LazyJsonArray instance
$firstOrder = $orders[0]; // Lazy loading triggered here
$orderCount = $orders->count(); // Available without loading items

// Iterate over lazy array
foreach ($user->getOrders() as $order) {
    echo $order->productName . "\n";
}



// Use type aliases to reduce JSON size
$typeMap = [
    'u' => User::class,
    'p' => Profile::class,
    'a' => Address::class,
];

// JSON with full class names: ~200 bytes
// {"#type":"App\\Entity\\Profile","firstName":"John",...}

// JSON with aliases: ~150 bytes
// {"#type":"p","firstName":"John",...}

// Access only what you need to benefit from lazy loading
$user = $userRepository->find(1);
$name = $user->getName(); // No JSON deserialization yet
$profileName = $user->getProfile()->getFullName(); // Profile loaded here

// Lazy arrays
$orders = $user->getOrders(); // No loading yet
$orderCount = $orders->count(); // Loading all orders
$firstOrder = $orders[0]; // Already loaded

// Without lazy loading: ~50MB memory for 10,000 user profiles
// With lazy loading: ~5MB memory (90% reduction)

// JSON size comparison:
// Full class names: {"#type":"App\\Entity\\Profile",...} (~200 bytes)
// Type aliases: {"#type":"p",...} (~140 bytes) - 30% smaller
bash
# PHP CS Fixer
vendor/bin/php-cs-fixer fix --using-cache=no

# PHPStan
vendor/bin/phpstan analyse -v