Skip to content

Commit bc76c95

Browse files
mbabkerphansys
authored andcommitted
Use the object managers to check for initialized objects
1 parent 1e5d22b commit bc76c95

File tree

3 files changed

+11
-41
lines changed

3 files changed

+11
-41
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,6 @@ parameters:
540540
count: 1
541541
path: src/Timestampable/Mapping/Driver/Yaml.php
542542

543-
-
544-
message: '#^Access to an undefined property ProxyManager\\Proxy\\GhostObjectInterface&TObject of object\:\:\$identifier\.$#'
545-
identifier: property.notFound
546-
count: 1
547-
path: src/Tool/Wrapper/MongoDocumentWrapper.php
548-
549543
-
550544
message: '#^Call to function property_exists\(\) with \$this\(Gedmo\\Translatable\\Hydrator\\ORM\\ObjectHydrator\) and ''_em'' will always evaluate to false\.$#'
551545
identifier: function.impossibleType

src/Tool/Wrapper/EntityWrapper.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Doctrine\ORM\EntityManagerInterface;
1313
use Doctrine\ORM\Mapping\ClassMetadata;
14-
use Doctrine\Persistence\Proxy as PersistenceProxy;
1514
use Gedmo\Tool\ClassUtils;
1615

1716
/**
@@ -35,11 +34,6 @@ class EntityWrapper extends AbstractWrapper
3534
*/
3635
private $identifier;
3736

38-
/**
39-
* True if entity or proxy is loaded
40-
*/
41-
private bool $initialized = false;
42-
4337
/**
4438
* Wrap entity
4539
*
@@ -124,12 +118,8 @@ public function isEmbeddedAssociation($field)
124118
*/
125119
protected function initialize()
126120
{
127-
if (!$this->initialized) {
128-
if ($this->object instanceof PersistenceProxy) {
129-
if (!$this->object->__isInitialized()) {
130-
$this->object->__load();
131-
}
132-
}
121+
if ($this->om->isUninitializedObject($this->object)) {
122+
$this->om->initializeObject($this->object);
133123
}
134124
}
135125
}

src/Tool/Wrapper/MongoDocumentWrapper.php

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ class MongoDocumentWrapper extends AbstractWrapper
3232
*/
3333
private ?string $identifier = null;
3434

35-
/**
36-
* True if document or proxy is loaded
37-
*/
38-
private bool $initialized = false;
39-
4035
/**
4136
* Wrap document
4237
*
@@ -111,24 +106,15 @@ public function isEmbeddedAssociation($field)
111106
*/
112107
protected function initialize()
113108
{
114-
if (!$this->initialized) {
115-
if ($this->object instanceof GhostObjectInterface) {
116-
$uow = $this->om->getUnitOfWork();
117-
if (!$this->object->isProxyInitialized()) {
118-
$persister = $uow->getDocumentPersister($this->meta->getName());
119-
$identifier = null;
120-
if ($uow->isInIdentityMap($this->object)) {
121-
$identifier = $this->getIdentifier();
122-
} else {
123-
// this may not happen but in case
124-
$getIdentifier = \Closure::bind(fn () => $this->identifier, $this->object, get_class($this->object));
125-
126-
$identifier = $getIdentifier();
127-
}
128-
$this->object->initializeProxy();
129-
$persister->load($identifier, $this->object);
130-
}
131-
}
109+
if (method_exists($this->om, 'isUninitializedObject') && $this->om->isUninitializedObject($this->object)) {
110+
$this->om->initializeObject($this->object);
111+
112+
return;
113+
}
114+
115+
// @todo: Drop support for this fallback when requiring `doctrine/mongodb-odm:^2.6 as a minimum`
116+
if ($this->object instanceof GhostObjectInterface && !$this->object->isProxyInitialized()) {
117+
$this->om->initializeObject($this->object);
132118
}
133119
}
134120
}

0 commit comments

Comments
 (0)