-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize proxy when document is not initialized
- Loading branch information
Showing
4 changed files
with
108 additions
and
1 deletion.
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
41 changes: 41 additions & 0 deletions
41
tests/Doctrine/ODM/MongoDB/Tests/Functional/Ticket/GH2349Test.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,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ODM\MongoDB\Tests\Functional\Ticket; | ||
|
||
use Doctrine\ODM\MongoDB\Tests\BaseTest; | ||
use Documents74\GH2349Customer; | ||
use Documents74\GH2349Order; | ||
|
||
/** | ||
* @requires PHP 7.4 | ||
*/ | ||
class GH2349Test extends BaseTest | ||
{ | ||
/** | ||
* @doesNotPerformAssertions | ||
*/ | ||
public function testAccessingUnitializedProperties(): void | ||
{ | ||
$customer = new GH2349Customer('Some Place'); | ||
$order = new GH2349Order($customer); | ||
|
||
$this->dm->persist($customer); | ||
$this->dm->persist($order); | ||
$this->dm->flush(); | ||
$this->dm->clear(); | ||
|
||
$orderId = GH2349Order::ID; | ||
|
||
$order = $this->dm->getRepository(GH2349Order::class)->find($orderId); | ||
|
||
// Fetch a list of Customers from the DB. Any customer object that was referenced in the above order is still | ||
// a proxy object, however it will not have the defaults set for the un-managed $domainEvents property. | ||
$customers = $this->dm->getRepository(GH2349Customer::class)->findAll(); | ||
|
||
foreach ($customers as $customer) { | ||
$customer->doSomeUpdate(); // This will trigger an error as we try to append to the non-existing $domainEvents property | ||
} | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documents74; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
|
||
/** | ||
* @ODM\Document() | ||
*/ | ||
class GH2349Customer | ||
{ | ||
public const ID = '610419a277d50f7609139542'; | ||
|
||
/** @ODM\Id() */ | ||
private string $id; | ||
|
||
/** @ODM\Field() */ | ||
private string $name; | ||
|
||
private array $domainEvents = []; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->id = self::ID; | ||
$this->name = $name; | ||
} | ||
|
||
public function doSomeUpdate(): void | ||
{ | ||
$this->domainEvents[] = 'a new event!'; | ||
} | ||
|
||
public function getEvents(): array | ||
{ | ||
return $this->domainEvents; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Documents74; | ||
|
||
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; | ||
use MongoDB\BSON\ObjectId; | ||
|
||
/** | ||
* @ODM\Document() | ||
*/ | ||
class GH2349Order | ||
{ | ||
public const ID = '610419a277d50f7609139543'; | ||
|
||
/** @ODM\Id() */ | ||
private string $id; | ||
|
||
/** @ODM\ReferenceOne(targetDocument=GH2349Customer::class, storeAs="ref") */ | ||
private GH2349Customer $customer; | ||
|
||
public function __construct(GH2349Customer $customer) | ||
{ | ||
$this->id = (string) new ObjectId(self::ID); | ||
$this->customer = $customer; | ||
} | ||
} |