3slab/vdm-library-doctrine-odm-transport-bundle
Installation Add the bundle to your Laravel project via Composer:
composer require 3slab/vdm-library-doctrine-odm-transport-bundle
Register the bundle in config/app.php under providers (if not auto-discovered).
Configuration Publish the default config:
php artisan vendor:publish --provider="3slab\VdmLibraryDoctrineOdmTransportBundle\VdmLibraryDoctrineOdmTransportBundle" --tag="config"
Update config/vdm_library_doctrine_odm_transport.php with your Doctrine ODM connection details (e.g., MongoDB URI, database name).
First Use Case Use the bundle to hydrate a Doctrine ODM document from a Laravel model or DTO:
use VdmLibrary\DoctrineOdmTransportBundle\Transport\DocumentTransport;
$transport = app(DocumentTransport::class);
$document = $transport->hydrateFromArray($arrayData, 'YourDocumentClass');
$transport->persist($document); // Save to MongoDB
Laravel ↔ Doctrine ODM Sync
Use DocumentTransport to convert between Laravel arrays (e.g., API payloads) and Doctrine ODM documents:
// Convert Laravel array to ODM document
$document = $transport->hydrateFromArray($request->all(), 'UserProfile');
// Convert ODM document to array (e.g., for API response)
$arrayData = $transport->extractToArray($document);
Bulk Operations Leverage batch processing for efficiency:
$documents = [];
foreach ($request->all() as $item) {
$documents[] = $transport->hydrateFromArray($item, 'Product');
}
$transport->persistAll($documents); // Bulk save
Form Request Validation Validate incoming data before transport:
public function rules(): array
{
return [
'name' => 'required|string',
'email' => 'required|email',
];
}
public function hydrate(Validator $validator)
{
$data = $validator->validated();
$document = $this->transport->hydrateFromArray($data, 'User');
return $document;
}
API Resource Transformation
Use the transport in Illuminate\Http\Resources\Json\Resource:
public function toArray($request)
{
return $this->transport->extractToArray($this->resource);
}
prePersist) via Laravel events:
// In EventServiceProvider
protected $listen = [
'odm.document.prePersist' => [
\App\Listeners\LogDocumentCreation::class,
],
];
Type Mismatches
DateTime for timestamps). Ensure Laravel arrays match:
// Bad: String timestamp
$data = ['created_at' => '2023-01-01'];
// Good: Convert to DateTime object
$data = ['created_at' => new \DateTime('2023-01-01')];
Missing Document Classes
App\Documents\User). Use:
$transport->hydrateFromArray($data, \App\Documents\User::class);
MongoDB Schema Changes
php artisan cache:clear
config/monolog.php:
'channels' => [
'doctrine' => [
'path' => storage_path('logs/doctrine.log'),
'level' => 'debug',
],
],
Then log ODM events:
$this->container->get('doctrine')->getManager()->getEventManager()->addEventListener(
array('prePersist', 'preUpdate'),
new \Doctrine\ODM\MongoDB\Event\LifecycleEventListener()
);
Custom Hydrators
Extend VdmLibrary\DoctrineOdmTransportBundle\Transport\Hydrator\HydratorInterface for custom logic:
class CustomHydrator implements HydratorInterface
{
public function hydrate(array $data, string $class): object
{
// Custom logic (e.g., nested object hydration)
}
}
Register in config:
'hydrators' => [
'custom' => \App\Hydrators\CustomHydrator::class,
],
Override Default Config Use environment variables to customize:
VDM_ODM_TRANSPORT_DEFAULT_DOCUMENT=App\Documents\User
Laravel Service Container Binding Bind custom transports:
$this->app->bind(DocumentTransport::class, function ($app) {
return new CustomDocumentTransport($app['doctrine']);
});
How can I help you explore Laravel packages today?