-
-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
doc: Review cookbook on blending ORM and ODM (#2656)
* Review cookbook on blending ORM and ODM * ORM test requires pdo_sqlite * Rewrite introduction
- Loading branch information
Showing
6 changed files
with
198 additions
and
47 deletions.
There are no files selected for viewing
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
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
34 changes: 34 additions & 0 deletions
34
tests/Documentation/BlendingOrm/BlendingOrmEventSubscriber.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\BlendingOrm; | ||
|
||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
use Doctrine\ORM\Event\PostLoadEventArgs; | ||
|
||
class BlendingOrmEventSubscriber | ||
{ | ||
public function __construct( | ||
private readonly DocumentManager $dm, | ||
) { | ||
} | ||
|
||
public function postLoad(PostLoadEventArgs $eventArgs): void | ||
{ | ||
$order = $eventArgs->getObject(); | ||
|
||
if (! $order instanceof Order) { | ||
return; | ||
} | ||
|
||
// Reference to the Product document, without loading it | ||
$product = $this->dm->getReference(Product::class, $order->getProductId()); | ||
|
||
$eventArgs->getObjectManager() | ||
->getClassMetadata(Order::class) | ||
->reflClass | ||
->getProperty('product') | ||
->setValue($order, $product); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\BlendingOrm; | ||
|
||
use Doctrine\DBAL\DriverManager; | ||
use Doctrine\ODM\MongoDB\Tests\BaseTestCase; | ||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Events; | ||
use Doctrine\ORM\ORMSetup; | ||
use PHPUnit\Framework\Attributes\RequiresPhpExtension; | ||
|
||
#[RequiresPhpExtension('pdo_sqlite')] | ||
class BlendingOrmTest extends BaseTestCase | ||
{ | ||
public function testTest(): void | ||
{ | ||
$dm = $this->dm; | ||
|
||
// Init ORM | ||
$config = ORMSetup::createAttributeMetadataConfiguration( | ||
paths: [__DIR__], | ||
isDevMode: true, | ||
); | ||
$connection = DriverManager::getConnection([ | ||
'driver' => 'pdo_sqlite', | ||
'path' => ':memory:', | ||
], $config); | ||
$connection->executeQuery('CREATE TABLE orders (id INTEGER PRIMARY KEY, productId TEXT)'); | ||
$em = new EntityManager($connection, $config); | ||
$em->getEventManager() | ||
->addEventListener( | ||
[Events::postLoad], | ||
new BlendingOrmEventSubscriber($dm), | ||
); | ||
|
||
// Init Product document and Order entity | ||
$product = new Product(); | ||
$product->title = 'Test Product'; | ||
$dm->persist($product); | ||
$dm->flush(); | ||
|
||
$order = new Order(); | ||
$order->setProduct($product); | ||
$em->persist($order); | ||
$em->flush(); | ||
$em->clear(); | ||
|
||
$order = $em->find(Order::class, $order->id); | ||
// The Product document is loaded from the DocumentManager | ||
$this->assertSame($product, $order->getProduct()); | ||
|
||
$em->clear(); | ||
$dm->clear(); | ||
|
||
$order = $em->find(Order::class, $order->id); | ||
// New Product instance, not the same as the one in the DocumentManager | ||
$this->assertNotSame($product, $order->getProduct()); | ||
$this->assertSame($product->id, $order->getProduct()->id); | ||
$this->assertSame($product->title, $order->getProduct()->title); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\BlendingOrm; | ||
|
||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
#[Entity] | ||
#[Table(name: 'orders')] | ||
class Order | ||
{ | ||
#[Id] | ||
#[Column(type: 'integer')] | ||
#[GeneratedValue(strategy: 'AUTO')] | ||
public int $id; | ||
|
||
#[Column(type: 'string')] | ||
private string $productId; | ||
|
||
private Product $product; | ||
|
||
public function getProductId(): ?string | ||
{ | ||
return $this->productId; | ||
} | ||
|
||
public function setProduct(Product $product): void | ||
{ | ||
$this->productId = $product->id; | ||
$this->product = $product; | ||
} | ||
|
||
public function getProduct(): ?Product | ||
{ | ||
return $this->product; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documentation\BlendingOrm; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Field; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; | ||
|
||
#[Document] | ||
class Product | ||
{ | ||
#[Id] | ||
public string $id; | ||
|
||
#[Field] | ||
public string $title; | ||
} |