mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 15:02:22 +01:00
Compare commits
97 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
92e2f6db83 | ||
|
|
aa624f64c1 | ||
|
|
e1675eb371 | ||
|
|
cc2b6385a1 | ||
|
|
a64bed9bbb | ||
|
|
3272e1c0af | ||
|
|
69da22d517 | ||
|
|
06109f360f | ||
|
|
06a9ef1127 | ||
|
|
5d21bb158b | ||
|
|
c74df3fab3 | ||
|
|
f2c902ee03 | ||
|
|
4e5e3c5e50 | ||
|
|
da697f218f | ||
|
|
4f47a80deb | ||
|
|
ab89517093 | ||
|
|
48a51d8470 | ||
|
|
ab11244f08 | ||
|
|
a1c2be140d | ||
|
|
4664373bd0 | ||
|
|
97b29bb063 | ||
|
|
b7fff508a4 | ||
|
|
c6fa14ed52 | ||
|
|
0a43e4af8f | ||
|
|
35d301b052 | ||
|
|
083b241c81 | ||
|
|
b9989555fd | ||
|
|
80a79f6d2d | ||
|
|
9a3f5579f1 | ||
|
|
12c721f528 | ||
|
|
9a9c3e8aba | ||
|
|
46a020108d | ||
|
|
b286d6cd2c | ||
|
|
443cf92242 | ||
|
|
eb3b984132 | ||
|
|
04395f98f9 | ||
|
|
0c10010f9f | ||
|
|
be8da83aca | ||
|
|
f5ab687226 | ||
|
|
742eead849 | ||
|
|
f98e871913 | ||
|
|
4b0c11978e | ||
|
|
0ef5610a6c | ||
|
|
e29d0e977d | ||
|
|
d540f73778 | ||
|
|
201d751a26 | ||
|
|
6308b2fd86 | ||
|
|
8f99e84438 | ||
|
|
e36b7755e9 | ||
|
|
7b4d869b31 | ||
|
|
8873109b4f | ||
|
|
5077ae41e5 | ||
|
|
8e1a27b8cc | ||
|
|
e7db1b005f | ||
|
|
72ce662e45 | ||
|
|
673cf0d4d8 | ||
|
|
1cae0534a0 | ||
|
|
6fb3083f63 | ||
|
|
68c17ca1bd | ||
|
|
82cf29407c | ||
|
|
ae74be5e9d | ||
|
|
4163efd2f2 | ||
|
|
d7ac6123ad | ||
|
|
bd260d1be8 | ||
|
|
cd1a52c7e4 | ||
|
|
0d2cb6acd1 | ||
|
|
327418a4b7 | ||
|
|
9f2b367081 | ||
|
|
a9873c86bb | ||
|
|
8ebd98ee92 | ||
|
|
5a220078e9 | ||
|
|
a15543a2ce | ||
|
|
238fb74028 | ||
|
|
6ff2b130d3 | ||
|
|
8c9bfca255 | ||
|
|
c2a2386df9 | ||
|
|
2f98e11562 | ||
|
|
073809cf5c | ||
|
|
e82690d256 | ||
|
|
23c31aec51 | ||
|
|
622ba2dcc7 | ||
|
|
0c1cf853fc | ||
|
|
79d1f07fa2 | ||
|
|
eba01f8d0e | ||
|
|
bd292481bd | ||
|
|
fcc53b260f | ||
|
|
7d61a1e73f | ||
|
|
b3cffe2d12 | ||
|
|
052c7d7698 | ||
|
|
c2713adebc | ||
|
|
51a984be3d | ||
|
|
6007154484 | ||
|
|
22ce0aff37 | ||
|
|
37051d57ce | ||
|
|
4563f2f9a7 | ||
|
|
91201c094a | ||
|
|
a4a15ad243 |
@@ -11,17 +11,23 @@
|
||||
"slug": "latest",
|
||||
"upcoming": true
|
||||
},
|
||||
{
|
||||
"name": "3.5",
|
||||
"branchName": "3.5.x",
|
||||
"slug": "3.5",
|
||||
"upcoming": true
|
||||
},
|
||||
{
|
||||
"name": "3.4",
|
||||
"branchName": "3.4.x",
|
||||
"slug": "3.4",
|
||||
"upcoming": true
|
||||
"current": true
|
||||
},
|
||||
{
|
||||
"name": "3.3",
|
||||
"branchName": "3.3.x",
|
||||
"slug": "3.3",
|
||||
"current": true
|
||||
"maintained": false
|
||||
},
|
||||
{
|
||||
"name": "3.2",
|
||||
|
||||
14
.github/workflows/continuous-integration.yml
vendored
14
.github/workflows/continuous-integration.yml
vendored
@@ -43,17 +43,27 @@ jobs:
|
||||
- "pdo_sqlite"
|
||||
deps:
|
||||
- "highest"
|
||||
native_lazy:
|
||||
- "0"
|
||||
include:
|
||||
- php-version: "8.2"
|
||||
dbal-version: "4@dev"
|
||||
extension: "pdo_sqlite"
|
||||
native_lazy: "0"
|
||||
- php-version: "8.2"
|
||||
dbal-version: "4@dev"
|
||||
extension: "sqlite3"
|
||||
native_lazy: "0"
|
||||
- php-version: "8.1"
|
||||
dbal-version: "default"
|
||||
deps: "lowest"
|
||||
extension: "pdo_sqlite"
|
||||
native_lazy: "0"
|
||||
- php-version: "8.4"
|
||||
dbal-version: "default"
|
||||
deps: "highest"
|
||||
extension: "pdo_sqlite"
|
||||
native_lazy: "1"
|
||||
|
||||
steps:
|
||||
- name: "Checkout"
|
||||
@@ -83,16 +93,18 @@ jobs:
|
||||
run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --coverage-clover=coverage-no-cache.xml"
|
||||
env:
|
||||
ENABLE_SECOND_LEVEL_CACHE: 0
|
||||
ENABLE_NATIVE_LAZY_OBJECTS: ${{ matrix.native_lazy }}
|
||||
|
||||
- name: "Run PHPUnit with Second Level Cache"
|
||||
run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --exclude-group performance,non-cacheable,locking_functional --coverage-clover=coverage-cache.xml"
|
||||
env:
|
||||
ENABLE_SECOND_LEVEL_CACHE: 1
|
||||
ENABLE_NATIVE_LAZY_OBJECTS: ${{ matrix.native_lazy }}
|
||||
|
||||
- name: "Upload coverage file"
|
||||
uses: "actions/upload-artifact@v4"
|
||||
with:
|
||||
name: "phpunit-${{ matrix.extension }}-${{ matrix.php-version }}-${{ matrix.dbal-version }}-${{ matrix.deps }}-coverage"
|
||||
name: "phpunit-${{ matrix.extension }}-${{ matrix.php-version }}-${{ matrix.dbal-version }}-${{ matrix.deps }}-${{ matrix.native_lazy }}-coverage"
|
||||
path: "coverage*.xml"
|
||||
|
||||
|
||||
|
||||
14
README.md
14
README.md
@@ -1,7 +1,7 @@
|
||||
| [4.0.x][4.0] | [3.4.x][3.4] | [3.3.x][3.3] | [2.21.x][2.21] | [2.20.x][2.20] |
|
||||
| [4.0.x][4.0] | [3.5.x][3.5] | [3.4.x][3.4] | [2.21.x][2.21] | [2.20.x][2.20] |
|
||||
|:------------------------------------------------------:|:------------------------------------------------------:|:------------------------------------------------------:|:--------------------------------------------------------:|:--------------------------------------------------------:|
|
||||
| [![Build status][4.0 image]][4.0] | [![Build status][3.4 image]][3.4] | [![Build status][3.3 image]][3.3] | [![Build status][2.21 image]][2.21] | [![Build status][2.20 image]][2.20] |
|
||||
| [![Coverage Status][4.0 coverage image]][4.0 coverage] | [![Coverage Status][3.4 coverage image]][3.4 coverage] | [![Coverage Status][3.3 coverage image]][3.3 coverage] | [![Coverage Status][2.21 coverage image]][2.21 coverage] | [![Coverage Status][2.20 coverage image]][2.20 coverage] |
|
||||
| [![Build status][4.0 image]][4.0] | [![Build status][3.5 image]][3.5] | [![Build status][3.4 image]][3.4] | [![Build status][2.21 image]][2.21] | [![Build status][2.20 image]][2.20] |
|
||||
| [![Coverage Status][4.0 coverage image]][4.0 coverage] | [![Coverage Status][3.5 coverage image]][3.5 coverage] | [![Coverage Status][3.4 coverage image]][3.4 coverage] | [![Coverage Status][2.21 coverage image]][2.21 coverage] | [![Coverage Status][2.20 coverage image]][2.20 coverage] |
|
||||
|
||||
Doctrine ORM is an object-relational mapper for PHP 8.1+ that provides transparent persistence
|
||||
for PHP objects. It sits on top of a powerful database abstraction layer (DBAL). One of its key features
|
||||
@@ -20,14 +20,14 @@ without requiring unnecessary code duplication.
|
||||
[4.0]: https://github.com/doctrine/orm/tree/4.0.x
|
||||
[4.0 coverage image]: https://codecov.io/gh/doctrine/orm/branch/4.0.x/graph/badge.svg
|
||||
[4.0 coverage]: https://codecov.io/gh/doctrine/orm/branch/4.0.x
|
||||
[3.5 image]: https://github.com/doctrine/orm/actions/workflows/continuous-integration.yml/badge.svg?branch=3.5.x
|
||||
[3.5]: https://github.com/doctrine/orm/tree/3.5.x
|
||||
[3.5 coverage image]: https://codecov.io/gh/doctrine/orm/branch/3.5.x/graph/badge.svg
|
||||
[3.5 coverage]: https://codecov.io/gh/doctrine/orm/branch/3.5.x
|
||||
[3.4 image]: https://github.com/doctrine/orm/actions/workflows/continuous-integration.yml/badge.svg?branch=3.4.x
|
||||
[3.4]: https://github.com/doctrine/orm/tree/3.4.x
|
||||
[3.4 coverage image]: https://codecov.io/gh/doctrine/orm/branch/3.4.x/graph/badge.svg
|
||||
[3.4 coverage]: https://codecov.io/gh/doctrine/orm/branch/3.4.x
|
||||
[3.3 image]: https://github.com/doctrine/orm/actions/workflows/continuous-integration.yml/badge.svg?branch=3.3.x
|
||||
[3.3]: https://github.com/doctrine/orm/tree/3.3.x
|
||||
[3.3 coverage image]: https://codecov.io/gh/doctrine/orm/branch/3.3.x/graph/badge.svg
|
||||
[3.3 coverage]: https://codecov.io/gh/doctrine/orm/branch/3.3.x
|
||||
[2.21 image]: https://github.com/doctrine/orm/actions/workflows/continuous-integration.yml/badge.svg?branch=2.21.x
|
||||
[2.21]: https://github.com/doctrine/orm/tree/2.21.x
|
||||
[2.21 coverage image]: https://codecov.io/gh/doctrine/orm/branch/2.21.x/graph/badge.svg
|
||||
|
||||
24
UPGRADE.md
24
UPGRADE.md
@@ -1,8 +1,28 @@
|
||||
# Upgrade to 3.4.1
|
||||
|
||||
## BC BREAK: You can no longer use the `.*` notation to get all fields of an entity in a DTO
|
||||
|
||||
This feature was introduced in 3.4.0, and introduces several issues, so we
|
||||
decide to remove it before it is used too widely.
|
||||
|
||||
# Upgrade to 3.4
|
||||
|
||||
## Discriminator Map class duplicates
|
||||
|
||||
Using the same class several times in a discriminator map is deprecated.
|
||||
In 4.0, this will be an error.
|
||||
|
||||
## `Doctrine\ORM\Mapping\ClassMetadata::$reflFields` deprecated
|
||||
|
||||
To better support property hooks and lazy proxies in the future, `$reflFields` had to
|
||||
be deprecated because we cannot use the PHP internal reflection API directly anymore.
|
||||
|
||||
The property was changed from an array to an object of type `LegacyReflectionFields`
|
||||
that implements `ArrayAccess`.
|
||||
|
||||
Use the new `Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor` API and access
|
||||
through `Doctrine\ORM\Mapping\ClassMetadata::$propertyAccessors` instead.
|
||||
|
||||
# Upgrade to 3.3
|
||||
|
||||
## Deprecate `DatabaseDriver`
|
||||
@@ -13,7 +33,7 @@ The class `Doctrine\ORM\Mapping\Driver\DatabaseDriver` is deprecated without rep
|
||||
|
||||
Output walkers should implement the new `\Doctrine\ORM\Query\OutputWalker` interface and create
|
||||
`Doctrine\ORM\Query\Exec\SqlFinalizer` instances instead of `Doctrine\ORM\Query\Exec\AbstractSqlExecutor`s.
|
||||
The output walker must not base its workings on the query `firstResult`/`maxResult` values, so that the
|
||||
The output walker must not base its workings on the query `firstResult`/`maxResult` values, so that the
|
||||
`SqlFinalizer` can be kept in the query cache and used regardless of the actual `firstResult`/`maxResult` values.
|
||||
Any operation dependent on `firstResult`/`maxResult` should take place within the `SqlFinalizer::createExecutor()`
|
||||
method. Details can be found at https://github.com/doctrine/orm/pull/11188.
|
||||
@@ -124,7 +144,7 @@ WARNING: This was relaxed in ORM 3.2 when partial was re-allowed for array-hydra
|
||||
`Doctrine\ORM\Query::HINT_FORCE_PARTIAL_LOAD` are removed.
|
||||
- `Doctrine\ORM\EntityManager*::getPartialReference()` is removed.
|
||||
|
||||
## BC BREAK: Enforce ArrayCollection Type on `\Doctrine\ORM\QueryBuilder::setParameters(ArrayCollection $parameters)`
|
||||
## BC BREAK: Enforce ArrayCollection Type on `\Doctrine\ORM\QueryBuilder::setParameters(ArrayCollection $parameters)`
|
||||
|
||||
The argument $parameters can no longer be a key=>value array. Only ArrayCollection types are allowed.
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ steps of configuration.
|
||||
|
||||
// ...
|
||||
|
||||
if ($applicationMode == "development") {
|
||||
if ($applicationMode === "development") {
|
||||
$queryCache = new ArrayAdapter();
|
||||
$metadataCache = new ArrayAdapter();
|
||||
} else {
|
||||
@@ -32,13 +32,18 @@ steps of configuration.
|
||||
$driverImpl = new AttributeDriver(['/path/to/lib/MyProject/Entities'], true);
|
||||
$config->setMetadataDriverImpl($driverImpl);
|
||||
$config->setQueryCache($queryCache);
|
||||
$config->setProxyDir('/path/to/myproject/lib/MyProject/Proxies');
|
||||
$config->setProxyNamespace('MyProject\Proxies');
|
||||
|
||||
if ($applicationMode == "development") {
|
||||
$config->setAutoGenerateProxyClasses(true);
|
||||
if (PHP_VERSION_ID > 80400) {
|
||||
$config->enableNativeLazyObjects(true);
|
||||
} else {
|
||||
$config->setAutoGenerateProxyClasses(false);
|
||||
$config->setProxyDir('/path/to/myproject/lib/MyProject/Proxies');
|
||||
$config->setProxyNamespace('MyProject\Proxies');
|
||||
|
||||
if ($applicationMode === "development") {
|
||||
$config->setAutoGenerateProxyClasses(true);
|
||||
} else {
|
||||
$config->setAutoGenerateProxyClasses(false);
|
||||
}
|
||||
}
|
||||
|
||||
$connection = DriverManager::getConnection([
|
||||
@@ -71,8 +76,25 @@ Configuration Options
|
||||
The following sections describe all the configuration options
|
||||
available on a ``Doctrine\ORM\Configuration`` instance.
|
||||
|
||||
Proxy Directory (**REQUIRED**)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Native Lazy Objects (**OPTIONAL**)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
With PHP 8.4 we recommend that you use native lazy objects instead of
|
||||
the code generation approach using the ``symfony/var-exporter`` Ghost trait.
|
||||
|
||||
With Doctrine 4, the minimal requirement will become PHP 8.4 and native lazy objects
|
||||
will become the only approach to lazy loading.
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
$config->enableNativeLazyObjects(true);
|
||||
|
||||
Proxy Directory
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
Required except if you use native lazy objects with PHP 8.4.
|
||||
This setting will be removed in the future.
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
@@ -85,8 +107,11 @@ classes. For a detailed explanation on proxy classes and how they
|
||||
are used in Doctrine, refer to the "Proxy Objects" section further
|
||||
down.
|
||||
|
||||
Proxy Namespace (**REQUIRED**)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Proxy Namespace
|
||||
~~~~~~~~~~~~~~~
|
||||
|
||||
Required except if you use native lazy objects with PHP 8.4.
|
||||
This setting will be removed in the future.
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
@@ -200,6 +225,9 @@ deprecated ``Doctrine\DBAL\Logging\SQLLogger`` interface.
|
||||
Auto-generating Proxy Classes (**OPTIONAL**)
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
This setting is not required if you use native lazy objects with PHP 8.4
|
||||
and will be removed in the future.
|
||||
|
||||
Proxy classes can either be generated manually through the Doctrine
|
||||
Console or automatically at runtime by Doctrine. The configuration
|
||||
option that controls this behavior is:
|
||||
|
||||
@@ -676,6 +676,7 @@ Optional parameters:
|
||||
- **unique**: Determines whether this relation is exclusive between the
|
||||
affected entities and should be enforced as such on the database
|
||||
constraint level. Defaults to false.
|
||||
- **deferrable**: Determines whether this relation constraint can be deferred. Defaults to false.
|
||||
- **nullable**: Determine whether the related entity is required, or if
|
||||
null is an allowed state for the relation. Defaults to true.
|
||||
- **onDelete**: Cascade Action (Database-level)
|
||||
|
||||
@@ -214,6 +214,8 @@ These are the "automatic" mapping rules:
|
||||
| Any other type | ``Types::STRING`` |
|
||||
+-----------------------+-------------------------------+
|
||||
|
||||
.. versionadded:: 2.11
|
||||
|
||||
As of version 2.11 Doctrine can also automatically map typed properties using a
|
||||
PHP 8.1 enum to set the right ``type`` and ``enumType``.
|
||||
|
||||
@@ -224,6 +226,70 @@ and a custom ``Doctrine\ORM\Mapping\TypedFieldMapper`` implementation.
|
||||
|
||||
:doc:`Read more about TypedFieldMapper <typedfieldmapper>`.
|
||||
|
||||
Property Hooks
|
||||
--------------
|
||||
|
||||
.. versionadded:: 3.4
|
||||
|
||||
Doctrine supports mapping hooked properties as long as they have a backed property
|
||||
and are not virtual.
|
||||
|
||||
|
||||
.. configuration-block::
|
||||
|
||||
.. code-block:: attribute
|
||||
|
||||
<?php
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
|
||||
#[Entity]
|
||||
class Message
|
||||
{
|
||||
#[Column(type: Types::INTEGER)]
|
||||
private $id;
|
||||
#[Column(type: Types::STRING)]
|
||||
public string $language = 'de' {
|
||||
// Override the "read" action with arbitrary logic.
|
||||
get => strtoupper($this->language);
|
||||
|
||||
// Override the "write" action with arbitrary logic.
|
||||
set {
|
||||
$this->language = strtolower($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.. code-block:: xml
|
||||
|
||||
<doctrine-mapping>
|
||||
<entity name="Message">
|
||||
<field name="id" type="integer" />
|
||||
<field name="language" />
|
||||
</entity>
|
||||
</doctrine-mapping>
|
||||
|
||||
If you attempt to map a virtual property with ``#[Column]`` an exception will be thrown.
|
||||
|
||||
Some caveats apply to the use of property hooks, as they behave differently when accessing the property through
|
||||
the entity or directly through DQL/EntityRepository. Because the property hook can modify the value of the property in a way
|
||||
that value and raw value are different, you have to use the raw value representation when querying for the property.
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
$queryBuilder = $entityManager->createQueryBuilder();
|
||||
$queryBuilder->select('m')
|
||||
->from(Message::class, 'm')
|
||||
->where('m.language = :language')
|
||||
->setParameter('language', 'de'); // Use lower case here for raw value representation
|
||||
|
||||
$query = $queryBuilder->getQuery();
|
||||
$result = $query->getResult();
|
||||
|
||||
$messageRepository = $entityManager->getRepository(Message::class);
|
||||
$deMessages = $messageRepository->findBy(['language' => 'de']); // Use lower case here for raw value representation
|
||||
|
||||
.. _reference-mapping-types:
|
||||
|
||||
Doctrine Mapping Types
|
||||
|
||||
@@ -588,7 +588,7 @@ And then use the ``NEW`` DQL keyword :
|
||||
$query = $em->createQuery('SELECT NEW CustomerDTO(c.name, e.email, a.city, SUM(o.value)) FROM Customer c JOIN c.email e JOIN c.address a JOIN c.orders o GROUP BY c');
|
||||
$users = $query->getResult(); // array of CustomerDTO
|
||||
|
||||
You can also nest several DTO :
|
||||
You can also nest several DTO :
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
@@ -674,6 +674,16 @@ The ``NAMED`` keyword must precede all DTO you want to instantiate :
|
||||
If two arguments have the same name, a ``DuplicateFieldException`` is thrown.
|
||||
If a field cannot be matched with a property name, a ``NoMatchingPropertyException`` is thrown. This typically happens when using functions without aliasing them.
|
||||
|
||||
You can hydrate an entity nested in a DTO :
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
<?php
|
||||
$query = $em->createQuery('SELECT NEW CustomerDTO(c.name, a AS address) FROM Customer c JOIN c.address a');
|
||||
$users = $query->getResult(); // array of CustomerDTO
|
||||
|
||||
// CustomerDTO => {name : 'DOE', email: null, address : {city: 'New York', zip: '10011', address: 'Abbey Road'}
|
||||
|
||||
Using INDEX BY
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
@@ -1697,12 +1707,13 @@ Select Expressions
|
||||
|
||||
.. code-block:: php
|
||||
|
||||
SelectExpression ::= (IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression | NewObjectExpression) [["AS"] ["HIDDEN"] AliasResultVariable]
|
||||
SimpleSelectExpression ::= (StateFieldPathExpression | IdentificationVariable | FunctionDeclaration | AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] AliasResultVariable]
|
||||
PartialObjectExpression ::= "PARTIAL" IdentificationVariable "." PartialFieldSet
|
||||
PartialFieldSet ::= "{" SimpleStateField {"," SimpleStateField}* "}"
|
||||
NewObjectExpression ::= "NEW" AbstractSchemaName "(" NewObjectArg {"," NewObjectArg}* ")"
|
||||
NewObjectArg ::= (ScalarExpression | "(" Subselect ")" | NewObjectExpression) ["AS" AliasResultVariable]
|
||||
SelectExpression ::= (IdentificationVariable | ScalarExpression | AggregateExpression | FunctionDeclaration | PartialObjectExpression | "(" Subselect ")" | CaseExpression | NewObjectExpression) [["AS"] ["HIDDEN"] AliasResultVariable]
|
||||
SimpleSelectExpression ::= (StateFieldPathExpression | IdentificationVariable | FunctionDeclaration | AggregateExpression | "(" Subselect ")" | ScalarExpression) [["AS"] AliasResultVariable]
|
||||
PartialObjectExpression ::= "PARTIAL" IdentificationVariable "." PartialFieldSet
|
||||
PartialFieldSet ::= "{" SimpleStateField {"," SimpleStateField}* "}"
|
||||
NewObjectExpression ::= "NEW" AbstractSchemaName "(" NewObjectArg {"," NewObjectArg}* ")"
|
||||
NewObjectArg ::= (ScalarExpression | "(" Subselect ")" | NewObjectExpression | EntityAsDtoArgumentExpression) ["AS" AliasResultVariable]
|
||||
EntityAsDtoArgumentExpression ::= IdentificationVariable
|
||||
|
||||
Conditional Expressions
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
@@ -583,7 +583,7 @@ parameters:
|
||||
path: src/EntityManager.php
|
||||
|
||||
-
|
||||
message: '#^Method Doctrine\\ORM\\EntityManager\:\:getReference\(\) should return \(T of object\)\|null but returns Doctrine\\ORM\\Proxy\\InternalProxy\.$#'
|
||||
message: '#^Method Doctrine\\ORM\\EntityManager\:\:getReference\(\) should return \(T of object\)\|null but returns object\.$#'
|
||||
identifier: return.type
|
||||
count: 1
|
||||
path: src/EntityManager.php
|
||||
@@ -984,12 +984,6 @@ parameters:
|
||||
count: 1
|
||||
path: src/Mapping/ClassMetadata.php
|
||||
|
||||
-
|
||||
message: '#^Parameter \#1 \$class of method Doctrine\\Persistence\\Mapping\\ReflectionService\:\:getAccessibleProperty\(\) expects class\-string, string given\.$#'
|
||||
identifier: argument.type
|
||||
count: 1
|
||||
path: src/Mapping/ClassMetadata.php
|
||||
|
||||
-
|
||||
message: '#^Parameter \#1 \$mapping of method Doctrine\\ORM\\Mapping\\ClassMetadata\<T of object\>\:\:validateAndCompleteTypedAssociationMapping\(\) expects array\{type\: 1\|2\|4\|8, fieldName\: string, targetEntity\?\: class\-string\}, non\-empty\-array\<string, mixed\> given\.$#'
|
||||
identifier: argument.type
|
||||
@@ -1032,18 +1026,6 @@ parameters:
|
||||
count: 2
|
||||
path: src/Mapping/ClassMetadata.php
|
||||
|
||||
-
|
||||
message: '#^Parameter \#2 \$class of method Doctrine\\ORM\\Mapping\\ClassMetadata\<T of object\>\:\:getAccessibleProperty\(\) expects class\-string, string given\.$#'
|
||||
identifier: argument.type
|
||||
count: 1
|
||||
path: src/Mapping/ClassMetadata.php
|
||||
|
||||
-
|
||||
message: '#^Parameter \#3 \$embeddedClass of class Doctrine\\ORM\\Mapping\\ReflectionEmbeddedProperty constructor expects class\-string, string given\.$#'
|
||||
identifier: argument.type
|
||||
count: 1
|
||||
path: src/Mapping/ClassMetadata.php
|
||||
|
||||
-
|
||||
message: '#^Property Doctrine\\ORM\\Mapping\\ClassMetadata\:\:\$customRepositoryClassName with generic class Doctrine\\ORM\\EntityRepository does not specify its types\: T$#'
|
||||
identifier: missingType.generics
|
||||
@@ -1578,12 +1560,36 @@ parameters:
|
||||
count: 1
|
||||
path: src/Mapping/JoinTableMapping.php
|
||||
|
||||
-
|
||||
message: '#^Method Doctrine\\ORM\\Mapping\\LegacyReflectionFields\:\:__construct\(\) has parameter \$classMetadata with generic class Doctrine\\ORM\\Mapping\\ClassMetadata but does not specify its types\: T$#'
|
||||
identifier: missingType.generics
|
||||
count: 1
|
||||
path: src/Mapping/LegacyReflectionFields.php
|
||||
|
||||
-
|
||||
message: '#^Parameter \#1 \$class of method Doctrine\\Persistence\\Mapping\\ReflectionService\:\:getAccessibleProperty\(\) expects class\-string, string given\.$#'
|
||||
identifier: argument.type
|
||||
count: 1
|
||||
path: src/Mapping/LegacyReflectionFields.php
|
||||
|
||||
-
|
||||
message: '#^Method Doctrine\\ORM\\Mapping\\MappedSuperclass\:\:__construct\(\) has parameter \$repositoryClass with generic class Doctrine\\ORM\\EntityRepository but does not specify its types\: T$#'
|
||||
identifier: missingType.generics
|
||||
count: 1
|
||||
path: src/Mapping/MappedSuperclass.php
|
||||
|
||||
-
|
||||
message: '#^Method Doctrine\\ORM\\Mapping\\PropertyAccessors\\EnumPropertyAccessor\:\:toEnum\(\) should return array\<BackedEnum\>\|BackedEnum but returns array\<BackedEnum\|int\|string\>\.$#'
|
||||
identifier: return.type
|
||||
count: 1
|
||||
path: src/Mapping/PropertyAccessors/EnumPropertyAccessor.php
|
||||
|
||||
-
|
||||
message: '#^Parameter \#1 \$callback of function array_map expects \(callable\(BackedEnum\|int\|string\)\: mixed\)\|null, array\{class\-string\<BackedEnum\>, ''from''\} given\.$#'
|
||||
identifier: argument.type
|
||||
count: 1
|
||||
path: src/Mapping/PropertyAccessors/EnumPropertyAccessor.php
|
||||
|
||||
-
|
||||
message: '#^Method Doctrine\\ORM\\Mapping\\QuoteStrategy\:\:getColumnAlias\(\) has parameter \$class with generic class Doctrine\\ORM\\Mapping\\ClassMetadata but does not specify its types\: T$#'
|
||||
identifier: missingType.generics
|
||||
@@ -2394,12 +2400,6 @@ parameters:
|
||||
count: 1
|
||||
path: src/Proxy/ProxyFactory.php
|
||||
|
||||
-
|
||||
message: '#^Method Doctrine\\ORM\\Proxy\\ProxyFactory\:\:getProxy\(\) return type with generic interface Doctrine\\ORM\\Proxy\\InternalProxy does not specify its types\: T$#'
|
||||
identifier: missingType.generics
|
||||
count: 1
|
||||
path: src/Proxy/ProxyFactory.php
|
||||
|
||||
-
|
||||
message: '#^Method Doctrine\\ORM\\Proxy\\ProxyFactory\:\:loadProxyClass\(\) has parameter \$class with generic interface Doctrine\\Persistence\\Mapping\\ClassMetadata but does not specify its types\: T$#'
|
||||
identifier: missingType.generics
|
||||
@@ -2868,12 +2868,6 @@ parameters:
|
||||
count: 1
|
||||
path: src/Query/SqlWalker.php
|
||||
|
||||
-
|
||||
message: '#^Property Doctrine\\ORM\\Query\\SqlWalker\:\:\$selectedClasses \(array\<string, array\{class\: Doctrine\\ORM\\Mapping\\ClassMetadata, dqlAlias\: string, resultAlias\: string\|null\}\>\) does not accept non\-empty\-array\<int\|string, array\{class\: Doctrine\\ORM\\Mapping\\ClassMetadata, dqlAlias\: mixed, resultAlias\: string\|null\}\>\.$#'
|
||||
identifier: assign.propertyType
|
||||
count: 1
|
||||
path: src/Query/SqlWalker.php
|
||||
|
||||
-
|
||||
message: '#^Property Doctrine\\ORM\\Query\\SqlWalker\:\:\$selectedClasses with generic class Doctrine\\ORM\\Mapping\\ClassMetadata does not specify its types\: T$#'
|
||||
identifier: missingType.generics
|
||||
|
||||
@@ -45,7 +45,7 @@ parameters:
|
||||
path: src/UnitOfWork.php
|
||||
|
||||
-
|
||||
message: '~^Parameter #1 \$command of method Symfony\\Component\\Console\\Application::add\(\) expects Symfony\\Component\\Console\\Command\\Command, Doctrine\\DBAL\\Tools\\Console\\Command\\ReservedWordsCommand given\.$~'
|
||||
message: '~^Parameter #2 \$command of static method Doctrine\\ORM\\Tools\\Console\\ConsoleRunner::addCommandToApplication\(\) expects Symfony\\Component\\Console\\Command\\Command, Doctrine\\DBAL\\Tools\\Console\\Command\\ReservedWordsCommand given\.$~'
|
||||
path: src/Tools/Console/ConsoleRunner.php
|
||||
|
||||
-
|
||||
|
||||
@@ -30,6 +30,8 @@ use function class_exists;
|
||||
use function is_a;
|
||||
use function strtolower;
|
||||
|
||||
use const PHP_VERSION_ID;
|
||||
|
||||
/**
|
||||
* Configuration container for all configuration options of Doctrine.
|
||||
* It combines all configuration options from DBAL & ORM.
|
||||
@@ -593,6 +595,20 @@ class Configuration extends \Doctrine\DBAL\Configuration
|
||||
$this->attributes['schemaIgnoreClasses'] = $schemaIgnoreClasses;
|
||||
}
|
||||
|
||||
public function isNativeLazyObjectsEnabled(): bool
|
||||
{
|
||||
return $this->attributes['nativeLazyObjects'] ?? false;
|
||||
}
|
||||
|
||||
public function enableNativeLazyObjects(bool $nativeLazyObjects): void
|
||||
{
|
||||
if (PHP_VERSION_ID < 80400) {
|
||||
throw new LogicException('Lazy loading proxies require PHP 8.4 or higher.');
|
||||
}
|
||||
|
||||
$this->attributes['nativeLazyObjects'] = $nativeLazyObjects;
|
||||
}
|
||||
|
||||
/**
|
||||
* To be deprecated in 3.1.0
|
||||
*
|
||||
|
||||
@@ -19,6 +19,7 @@ use LogicException;
|
||||
use ReflectionClass;
|
||||
|
||||
use function array_key_exists;
|
||||
use function array_keys;
|
||||
use function array_map;
|
||||
use function array_merge;
|
||||
use function count;
|
||||
@@ -348,14 +349,29 @@ abstract class AbstractHydrator
|
||||
}
|
||||
}
|
||||
|
||||
$nestedEntities = [];
|
||||
/**@var string $argAlias */
|
||||
foreach ($this->resultSetMapping()->nestedNewObjectArguments as ['ownerIndex' => $ownerIndex, 'argIndex' => $argIndex, 'argAlias' => $argAlias]) {
|
||||
if (array_key_exists($argAlias, $rowData['newObjects'])) {
|
||||
ksort($rowData['newObjects'][$argAlias]['args']);
|
||||
$rowData['newObjects'][$ownerIndex]['args'][$argIndex] = $rowData['newObjects'][$argAlias]['class']->newInstanceArgs($rowData['newObjects'][$argAlias]['args']);
|
||||
unset($rowData['newObjects'][$argAlias]);
|
||||
} elseif (array_key_exists($argAlias, $rowData['data'])) {
|
||||
if (! array_key_exists($argAlias, $nestedEntities)) {
|
||||
$nestedEntities[$argAlias] = '';
|
||||
$rowData['data'][$argAlias] = $this->hydrateNestedEntity($rowData['data'][$argAlias], $argAlias);
|
||||
}
|
||||
|
||||
$rowData['newObjects'][$ownerIndex]['args'][$argIndex] = $rowData['data'][$argAlias];
|
||||
} else {
|
||||
throw new LogicException($argAlias . ' does not exist');
|
||||
}
|
||||
}
|
||||
|
||||
foreach (array_keys($nestedEntities) as $entity) {
|
||||
unset($rowData['data'][$entity]);
|
||||
}
|
||||
|
||||
foreach ($rowData['newObjects'] as $objIndex => $newObject) {
|
||||
ksort($rowData['newObjects'][$objIndex]['args']);
|
||||
$obj = $rowData['newObjects'][$objIndex]['class']->newInstanceArgs($rowData['newObjects'][$objIndex]['args']);
|
||||
@@ -366,6 +382,12 @@ abstract class AbstractHydrator
|
||||
return $rowData;
|
||||
}
|
||||
|
||||
/** @param mixed[] $data pre-hydrated SQL Result Row. */
|
||||
protected function hydrateNestedEntity(array $data, string $dqlAlias): mixed
|
||||
{
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes a row of the result set.
|
||||
*
|
||||
|
||||
@@ -70,6 +70,10 @@ class ObjectHydrator extends AbstractHydrator
|
||||
$parent = $this->resultSetMapping()->parentAliasMap[$dqlAlias];
|
||||
|
||||
if (! isset($this->resultSetMapping()->aliasMap[$parent])) {
|
||||
if (isset($this->resultSetMapping()->nestedEntities[$dqlAlias])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
throw HydrationException::parentObjectOfRelationNotFound($dqlAlias, $parent);
|
||||
}
|
||||
|
||||
@@ -171,7 +175,7 @@ class ObjectHydrator extends AbstractHydrator
|
||||
): PersistentCollection {
|
||||
$oid = spl_object_id($entity);
|
||||
$relation = $class->associationMappings[$fieldName];
|
||||
$value = $class->reflFields[$fieldName]->getValue($entity);
|
||||
$value = $class->propertyAccessors[$fieldName]->getValue($entity);
|
||||
|
||||
if ($value === null || is_array($value)) {
|
||||
$value = new ArrayCollection((array) $value);
|
||||
@@ -186,7 +190,7 @@ class ObjectHydrator extends AbstractHydrator
|
||||
);
|
||||
$value->setOwner($entity, $relation);
|
||||
|
||||
$class->reflFields[$fieldName]->setValue($entity, $value);
|
||||
$class->propertyAccessors[$fieldName]->setValue($entity, $value);
|
||||
$this->uow->setOriginalEntityProperty($oid, $fieldName, $value);
|
||||
|
||||
$this->initializedCollections[$oid . $fieldName] = $value;
|
||||
@@ -346,7 +350,7 @@ class ObjectHydrator extends AbstractHydrator
|
||||
$parentClass = $this->metadataCache[$this->resultSetMapping()->aliasMap[$parentAlias]];
|
||||
$relationField = $this->resultSetMapping()->relationMap[$dqlAlias];
|
||||
$relation = $parentClass->associationMappings[$relationField];
|
||||
$reflField = $parentClass->reflFields[$relationField];
|
||||
$reflField = $parentClass->propertyAccessors[$relationField];
|
||||
|
||||
// Get a reference to the parent object to which the joined element belongs.
|
||||
if ($this->resultSetMapping()->isMixed && isset($this->rootAliases[$parentAlias])) {
|
||||
@@ -446,13 +450,13 @@ class ObjectHydrator extends AbstractHydrator
|
||||
if ($relation->inversedBy !== null) {
|
||||
$inverseAssoc = $targetClass->associationMappings[$relation->inversedBy];
|
||||
if ($inverseAssoc->isToOne()) {
|
||||
$targetClass->reflFields[$inverseAssoc->fieldName]->setValue($element, $parentObject);
|
||||
$targetClass->propertyAccessors[$inverseAssoc->fieldName]->setValue($element, $parentObject);
|
||||
$this->uow->setOriginalEntityProperty(spl_object_id($element), $inverseAssoc->fieldName, $parentObject);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// For sure bidirectional, as there is no inverse side in unidirectional mappings
|
||||
$targetClass->reflFields[$relation->mappedBy]->setValue($element, $parentObject);
|
||||
$targetClass->propertyAccessors[$relation->mappedBy]->setValue($element, $parentObject);
|
||||
$this->uow->setOriginalEntityProperty(spl_object_id($element), $relation->mappedBy, $parentObject);
|
||||
}
|
||||
|
||||
@@ -569,6 +573,16 @@ class ObjectHydrator extends AbstractHydrator
|
||||
}
|
||||
}
|
||||
|
||||
/** @param mixed[] $data pre-hydrated SQL Result Row. */
|
||||
protected function hydrateNestedEntity(array $data, string $dqlAlias): mixed
|
||||
{
|
||||
if (isset($this->resultSetMapping()->nestedEntities[$dqlAlias])) {
|
||||
return $this->getEntity($data, $dqlAlias);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* When executed in a hydrate() loop we may have to clear internal state to
|
||||
* decrease memory consumption.
|
||||
|
||||
@@ -135,7 +135,13 @@ abstract class AssociationMapping implements ArrayAccess
|
||||
continue;
|
||||
}
|
||||
|
||||
assert($mapping instanceof ManyToManyOwningSideMapping);
|
||||
if (! $mapping instanceof ManyToManyOwningSideMapping) {
|
||||
throw new MappingException(
|
||||
"Mapping error on field '" .
|
||||
$mapping->fieldName . "' in " . $mapping->sourceEntity .
|
||||
" : 'joinTable' can only be set on many-to-many owning side.",
|
||||
);
|
||||
}
|
||||
|
||||
$mapping->joinTable = JoinTableMapping::fromMappingArray($value);
|
||||
|
||||
|
||||
@@ -14,9 +14,12 @@ use Doctrine\Instantiator\InstantiatorInterface;
|
||||
use Doctrine\ORM\Cache\Exception\NonCacheableEntityAssociation;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\Id\AbstractIdGenerator;
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\EmbeddablePropertyAccessor;
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\EnumPropertyAccessor;
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor;
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessorFactory;
|
||||
use Doctrine\Persistence\Mapping\ClassMetadata as PersistenceClassMetadata;
|
||||
use Doctrine\Persistence\Mapping\ReflectionService;
|
||||
use Doctrine\Persistence\Reflection\EnumReflectionProperty;
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
use ReflectionClass;
|
||||
@@ -57,8 +60,6 @@ use function strtolower;
|
||||
use function trait_exists;
|
||||
use function trim;
|
||||
|
||||
use const PHP_VERSION_ID;
|
||||
|
||||
/**
|
||||
* A <tt>ClassMetadata</tt> instance holds all the object-relational mapping metadata
|
||||
* of an entity and its associations.
|
||||
@@ -543,9 +544,12 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
/**
|
||||
* The ReflectionProperty instances of the mapped class.
|
||||
*
|
||||
* @var array<string, ReflectionProperty|null>
|
||||
* @var LegacyReflectionFields|array<string, ReflectionProperty>
|
||||
*/
|
||||
public array $reflFields = [];
|
||||
public LegacyReflectionFields|array $reflFields = [];
|
||||
|
||||
/** @var array<string, PropertyAccessors\PropertyAccessor> */
|
||||
public array $propertyAccessors = [];
|
||||
|
||||
private InstantiatorInterface|null $instantiator = null;
|
||||
|
||||
@@ -569,14 +573,24 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
/**
|
||||
* Gets the ReflectionProperties of the mapped class.
|
||||
*
|
||||
* @return ReflectionProperty[]|null[] An array of ReflectionProperty instances.
|
||||
* @phpstan-return array<ReflectionProperty|null>
|
||||
* @return LegacyReflectionFields|ReflectionProperty[] An array of ReflectionProperty instances.
|
||||
* @phpstan-return LegacyReflectionFields|array<string, ReflectionProperty>
|
||||
*/
|
||||
public function getReflectionProperties(): array
|
||||
public function getReflectionProperties(): array|LegacyReflectionFields
|
||||
{
|
||||
return $this->reflFields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ReflectionProperties of the mapped class.
|
||||
*
|
||||
* @return PropertyAccessor[] An array of PropertyAccessor instances.
|
||||
*/
|
||||
public function getPropertyAccessors(): array
|
||||
{
|
||||
return $this->propertyAccessors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a ReflectionProperty for a specific field of the mapped class.
|
||||
*/
|
||||
@@ -585,11 +599,12 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
return $this->reflFields[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ReflectionProperty for the single identifier field.
|
||||
*
|
||||
* @throws BadMethodCallException If the class has a composite identifier.
|
||||
*/
|
||||
public function getPropertyAccessor(string $name): PropertyAccessor|null
|
||||
{
|
||||
return $this->propertyAccessors[$name] ?? null;
|
||||
}
|
||||
|
||||
/** @throws BadMethodCallException If the class has a composite identifier. */
|
||||
public function getSingleIdReflectionProperty(): ReflectionProperty|null
|
||||
{
|
||||
if ($this->isIdentifierComposite) {
|
||||
@@ -599,6 +614,16 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
return $this->reflFields[$this->identifier[0]];
|
||||
}
|
||||
|
||||
/** @throws BadMethodCallException If the class has a composite identifier. */
|
||||
public function getSingleIdPropertyAccessor(): PropertyAccessor|null
|
||||
{
|
||||
if ($this->isIdentifierComposite) {
|
||||
throw new BadMethodCallException('Class ' . $this->name . ' has a composite identifier.');
|
||||
}
|
||||
|
||||
return $this->propertyAccessors[$this->identifier[0]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the identifier values of an entity of this class.
|
||||
*
|
||||
@@ -613,7 +638,7 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
$id = [];
|
||||
|
||||
foreach ($this->identifier as $idField) {
|
||||
$value = $this->reflFields[$idField]->getValue($entity);
|
||||
$value = $this->propertyAccessors[$idField]->getValue($entity);
|
||||
|
||||
if ($value !== null) {
|
||||
$id[$idField] = $value;
|
||||
@@ -624,7 +649,7 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
}
|
||||
|
||||
$id = $this->identifier[0];
|
||||
$value = $this->reflFields[$id]->getValue($entity);
|
||||
$value = $this->propertyAccessors[$id]->getValue($entity);
|
||||
|
||||
if ($value === null) {
|
||||
return [];
|
||||
@@ -643,7 +668,7 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
public function setIdentifierValues(object $entity, array $id): void
|
||||
{
|
||||
foreach ($id as $idField => $idValue) {
|
||||
$this->reflFields[$idField]->setValue($entity, $idValue);
|
||||
$this->propertyAccessors[$idField]->setValue($entity, $idValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -652,7 +677,7 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
*/
|
||||
public function setFieldValue(object $entity, string $field, mixed $value): void
|
||||
{
|
||||
$this->reflFields[$field]->setValue($entity, $value);
|
||||
$this->propertyAccessors[$field]->setValue($entity, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -660,7 +685,7 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
*/
|
||||
public function getFieldValue(object $entity, string $field): mixed
|
||||
{
|
||||
return $this->reflFields[$field]->getValue($entity);
|
||||
return $this->propertyAccessors[$field]->getValue($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -794,76 +819,74 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
{
|
||||
// Restore ReflectionClass and properties
|
||||
$this->reflClass = $reflService->getClass($this->name);
|
||||
$this->reflFields = new LegacyReflectionFields($this, $reflService);
|
||||
$this->instantiator = $this->instantiator ?: new Instantiator();
|
||||
|
||||
$parentReflFields = [];
|
||||
$parentAccessors = [];
|
||||
|
||||
foreach ($this->embeddedClasses as $property => $embeddedClass) {
|
||||
if (isset($embeddedClass->declaredField)) {
|
||||
assert($embeddedClass->originalField !== null);
|
||||
$childProperty = $this->getAccessibleProperty(
|
||||
$reflService,
|
||||
$childAccessor = PropertyAccessorFactory::createPropertyAccessor(
|
||||
$this->embeddedClasses[$embeddedClass->declaredField]->class,
|
||||
$embeddedClass->originalField,
|
||||
);
|
||||
assert($childProperty !== null);
|
||||
$parentReflFields[$property] = new ReflectionEmbeddedProperty(
|
||||
$parentReflFields[$embeddedClass->declaredField],
|
||||
$childProperty,
|
||||
|
||||
$parentAccessors[$property] = new EmbeddablePropertyAccessor(
|
||||
$parentAccessors[$embeddedClass->declaredField],
|
||||
$childAccessor,
|
||||
$this->embeddedClasses[$embeddedClass->declaredField]->class,
|
||||
);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$fieldRefl = $this->getAccessibleProperty(
|
||||
$reflService,
|
||||
$accessor = PropertyAccessorFactory::createPropertyAccessor(
|
||||
$embeddedClass->declared ?? $this->name,
|
||||
$property,
|
||||
);
|
||||
|
||||
$parentReflFields[$property] = $fieldRefl;
|
||||
$this->reflFields[$property] = $fieldRefl;
|
||||
$parentAccessors[$property] = $accessor;
|
||||
$this->propertyAccessors[$property] = $accessor;
|
||||
}
|
||||
|
||||
foreach ($this->fieldMappings as $field => $mapping) {
|
||||
if (isset($mapping->declaredField) && isset($parentReflFields[$mapping->declaredField])) {
|
||||
if (isset($mapping->declaredField) && isset($parentAccessors[$mapping->declaredField])) {
|
||||
assert($mapping->originalField !== null);
|
||||
assert($mapping->originalClass !== null);
|
||||
$childProperty = $this->getAccessibleProperty($reflService, $mapping->originalClass, $mapping->originalField);
|
||||
assert($childProperty !== null);
|
||||
$accessor = PropertyAccessorFactory::createPropertyAccessor($mapping->originalClass, $mapping->originalField);
|
||||
|
||||
if (isset($mapping->enumType)) {
|
||||
$childProperty = new EnumReflectionProperty(
|
||||
$childProperty,
|
||||
if ($mapping->enumType !== null) {
|
||||
$accessor = new EnumPropertyAccessor(
|
||||
$accessor,
|
||||
$mapping->enumType,
|
||||
);
|
||||
}
|
||||
|
||||
$this->reflFields[$field] = new ReflectionEmbeddedProperty(
|
||||
$parentReflFields[$mapping->declaredField],
|
||||
$childProperty,
|
||||
$this->propertyAccessors[$field] = new EmbeddablePropertyAccessor(
|
||||
$parentAccessors[$mapping->declaredField],
|
||||
$accessor,
|
||||
$mapping->originalClass,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->reflFields[$field] = isset($mapping->declared)
|
||||
? $this->getAccessibleProperty($reflService, $mapping->declared, $field)
|
||||
: $this->getAccessibleProperty($reflService, $this->name, $field);
|
||||
$this->propertyAccessors[$field] = isset($mapping->declared)
|
||||
? PropertyAccessorFactory::createPropertyAccessor($mapping->declared, $field)
|
||||
: PropertyAccessorFactory::createPropertyAccessor($this->name, $field);
|
||||
|
||||
if (isset($mapping->enumType) && $this->reflFields[$field] !== null) {
|
||||
$this->reflFields[$field] = new EnumReflectionProperty(
|
||||
$this->reflFields[$field],
|
||||
if ($mapping->enumType !== null) {
|
||||
$this->propertyAccessors[$field] = new EnumPropertyAccessor(
|
||||
$this->propertyAccessors[$field],
|
||||
$mapping->enumType,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->associationMappings as $field => $mapping) {
|
||||
$this->reflFields[$field] = isset($mapping->declared)
|
||||
? $this->getAccessibleProperty($reflService, $mapping->declared, $field)
|
||||
: $this->getAccessibleProperty($reflService, $this->name, $field);
|
||||
$this->propertyAccessors[$field] = isset($mapping->declared)
|
||||
? PropertyAccessorFactory::createPropertyAccessor($mapping->declared, $field)
|
||||
: PropertyAccessorFactory::createPropertyAccessor($this->name, $field);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2659,26 +2682,4 @@ class ClassMetadata implements PersistenceClassMetadata, Stringable
|
||||
|
||||
return $sequencePrefix;
|
||||
}
|
||||
|
||||
/** @phpstan-param class-string $class */
|
||||
private function getAccessibleProperty(ReflectionService $reflService, string $class, string $field): ReflectionProperty|null
|
||||
{
|
||||
$reflectionProperty = $reflService->getAccessibleProperty($class, $field);
|
||||
if ($reflectionProperty?->isReadOnly()) {
|
||||
$declaringClass = $reflectionProperty->class;
|
||||
if ($declaringClass !== $class) {
|
||||
$reflectionProperty = $reflService->getAccessibleProperty($declaringClass, $field);
|
||||
}
|
||||
|
||||
if ($reflectionProperty !== null) {
|
||||
$reflectionProperty = new ReflectionReadonlyProperty($reflectionProperty);
|
||||
}
|
||||
}
|
||||
|
||||
if (PHP_VERSION_ID >= 80400 && $reflectionProperty !== null && count($reflectionProperty->getHooks()) > 0) {
|
||||
throw new LogicException('Doctrine ORM does not support property hooks in this version. Check https://github.com/doctrine/orm/issues/11624 for details of versions that support property hooks.');
|
||||
}
|
||||
|
||||
return $reflectionProperty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,8 @@ use function strlen;
|
||||
use function strtolower;
|
||||
use function substr;
|
||||
|
||||
use const PHP_VERSION_ID;
|
||||
|
||||
/**
|
||||
* The ClassMetadataFactory is used to create ClassMetadata objects that contain all the
|
||||
* metadata mapping information of a class which describes how a class should be mapped
|
||||
@@ -440,8 +442,8 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
|
||||
$subClass->addInheritedFieldMapping($subClassMapping);
|
||||
}
|
||||
|
||||
foreach ($parentClass->reflFields as $name => $field) {
|
||||
$subClass->reflFields[$name] = $field;
|
||||
foreach ($parentClass->propertyAccessors as $name => $field) {
|
||||
$subClass->propertyAccessors[$name] = $field;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -699,6 +701,18 @@ class ClassMetadataFactory extends AbstractClassMetadataFactory
|
||||
protected function wakeupReflection(ClassMetadataInterface $class, ReflectionService $reflService): void
|
||||
{
|
||||
$class->wakeupReflection($reflService);
|
||||
|
||||
if (PHP_VERSION_ID < 80400) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($class->propertyAccessors as $propertyAccessor) {
|
||||
$property = $propertyAccessor->getUnderlyingReflector();
|
||||
|
||||
if ($property->isVirtual()) {
|
||||
throw MappingException::mappingVirtualPropertyNotAllowed($class->name, $property->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function initializeReflection(ClassMetadataInterface $class, ReflectionService $reflService): void
|
||||
|
||||
@@ -684,6 +684,7 @@ class AttributeDriver implements MappingDriver
|
||||
{
|
||||
$mapping = [
|
||||
'name' => $joinColumn->name,
|
||||
'deferrable' => $joinColumn->deferrable,
|
||||
'unique' => $joinColumn->unique,
|
||||
'nullable' => $joinColumn->nullable,
|
||||
'onDelete' => $joinColumn->onDelete,
|
||||
|
||||
@@ -54,6 +54,7 @@ final class FieldMapping implements ArrayAccess
|
||||
*/
|
||||
public string|null $inherited = null;
|
||||
|
||||
/** @var class-string|null */
|
||||
public string|null $originalClass = null;
|
||||
public string|null $originalField = null;
|
||||
public bool|null $quoted = null;
|
||||
@@ -101,7 +102,7 @@ final class FieldMapping implements ArrayAccess
|
||||
* scale?: int|null,
|
||||
* unique?: bool|null,
|
||||
* inherited?: string|null,
|
||||
* originalClass?: string|null,
|
||||
* originalClass?: class-string|null,
|
||||
* originalField?: string|null,
|
||||
* quoted?: bool|null,
|
||||
* declared?: string|null,
|
||||
|
||||
@@ -13,6 +13,7 @@ final class JoinColumnMapping implements ArrayAccess
|
||||
{
|
||||
use ArrayAccessImplementation;
|
||||
|
||||
public bool|null $deferrable = null;
|
||||
public bool|null $unique = null;
|
||||
public bool|null $quoted = null;
|
||||
public string|null $fieldName = null;
|
||||
@@ -33,7 +34,7 @@ final class JoinColumnMapping implements ArrayAccess
|
||||
* @param array<string, mixed> $mappingArray
|
||||
* @phpstan-param array{
|
||||
* name: string,
|
||||
* referencedColumnName: string,
|
||||
* referencedColumnName: string|null,
|
||||
* unique?: bool|null,
|
||||
* quoted?: bool|null,
|
||||
* fieldName?: string|null,
|
||||
@@ -66,7 +67,7 @@ final class JoinColumnMapping implements ArrayAccess
|
||||
}
|
||||
}
|
||||
|
||||
foreach (['unique', 'quoted', 'nullable'] as $boolKey) {
|
||||
foreach (['deferrable', 'unique', 'quoted', 'nullable'] as $boolKey) {
|
||||
if ($this->$boolKey !== null) {
|
||||
$serialized[] = $boolKey;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ trait JoinColumnProperties
|
||||
/** @param array<string, mixed> $options */
|
||||
public function __construct(
|
||||
public readonly string|null $name = null,
|
||||
public readonly string $referencedColumnName = 'id',
|
||||
public readonly string|null $referencedColumnName = null,
|
||||
public readonly bool $deferrable = false,
|
||||
public readonly bool $unique = false,
|
||||
public readonly bool $nullable = true,
|
||||
public readonly mixed $onDelete = null,
|
||||
|
||||
170
src/Mapping/LegacyReflectionFields.php
Normal file
170
src/Mapping/LegacyReflectionFields.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Mapping;
|
||||
|
||||
use ArrayAccess;
|
||||
use Doctrine\Deprecations\Deprecation;
|
||||
use Doctrine\Persistence\Mapping\ReflectionService;
|
||||
use Doctrine\Persistence\Reflection\EnumReflectionProperty;
|
||||
use Generator;
|
||||
use IteratorAggregate;
|
||||
use OutOfBoundsException;
|
||||
use ReflectionProperty;
|
||||
use Traversable;
|
||||
|
||||
use function array_keys;
|
||||
use function assert;
|
||||
use function is_string;
|
||||
use function str_contains;
|
||||
use function str_replace;
|
||||
|
||||
/**
|
||||
* @template-implements ArrayAccess<string, ReflectionProperty|null>
|
||||
* @template-implements IteratorAggregate<string, ReflectionProperty|null>
|
||||
*/
|
||||
class LegacyReflectionFields implements ArrayAccess, IteratorAggregate
|
||||
{
|
||||
/** @var array<string, ReflectionProperty|null> */
|
||||
private array $reflFields = [];
|
||||
|
||||
public function __construct(private ClassMetadata $classMetadata, private ReflectionService $reflectionService)
|
||||
{
|
||||
}
|
||||
|
||||
/** @param string $offset */
|
||||
public function offsetExists($offset): bool // phpcs:ignore
|
||||
{
|
||||
Deprecation::trigger(
|
||||
'doctrine/orm',
|
||||
'https://github.com/doctrine/orm/pull/11659',
|
||||
'Access to ClassMetadata::$reflFields is deprecated and will be removed in Doctrine ORM 4.0.',
|
||||
);
|
||||
|
||||
return isset($this->classMetadata->propertyAccessors[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
*
|
||||
* @psalm-suppress LessSpecificImplementedReturnType
|
||||
*/
|
||||
public function offsetGet($field): mixed // phpcs:ignore
|
||||
{
|
||||
if (isset($this->reflFields[$field])) {
|
||||
return $this->reflFields[$field];
|
||||
}
|
||||
|
||||
Deprecation::trigger(
|
||||
'doctrine/orm',
|
||||
'https://github.com/doctrine/orm/pull/11659',
|
||||
'Access to ClassMetadata::$reflFields is deprecated and will be removed in Doctrine ORM 4.0.',
|
||||
);
|
||||
|
||||
if (isset($this->classMetadata->propertyAccessors[$field])) {
|
||||
$fieldName = str_contains($field, '.') ? $this->classMetadata->fieldMappings[$field]->originalField : $field;
|
||||
$className = $this->classMetadata->name;
|
||||
|
||||
assert(is_string($fieldName));
|
||||
|
||||
if (isset($this->classMetadata->fieldMappings[$field]) && $this->classMetadata->fieldMappings[$field]->originalClass !== null) {
|
||||
$className = $this->classMetadata->fieldMappings[$field]->originalClass;
|
||||
} elseif (isset($this->classMetadata->fieldMappings[$field]) && $this->classMetadata->fieldMappings[$field]->declared !== null) {
|
||||
$className = $this->classMetadata->fieldMappings[$field]->declared;
|
||||
} elseif (isset($this->classMetadata->associationMappings[$field]) && $this->classMetadata->associationMappings[$field]->declared !== null) {
|
||||
$className = $this->classMetadata->associationMappings[$field]->declared;
|
||||
} elseif (isset($this->classMetadata->embeddedClasses[$field]) && $this->classMetadata->embeddedClasses[$field]->declared !== null) {
|
||||
$className = $this->classMetadata->embeddedClasses[$field]->declared;
|
||||
}
|
||||
|
||||
/** @psalm-suppress ArgumentTypeCoercion */
|
||||
$this->reflFields[$field] = $this->getAccessibleProperty($className, $fieldName);
|
||||
|
||||
if (isset($this->classMetadata->fieldMappings[$field])) {
|
||||
if ($this->classMetadata->fieldMappings[$field]->enumType !== null) {
|
||||
$this->reflFields[$field] = new EnumReflectionProperty(
|
||||
$this->reflFields[$field],
|
||||
$this->classMetadata->fieldMappings[$field]->enumType,
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->classMetadata->fieldMappings[$field]->originalField !== null) {
|
||||
$parentField = str_replace('.' . $fieldName, '', $field);
|
||||
$originalClass = $this->classMetadata->fieldMappings[$field]->originalClass;
|
||||
|
||||
if (! str_contains($parentField, '.')) {
|
||||
$parentClass = $this->classMetadata->name;
|
||||
} else {
|
||||
$parentClass = $this->classMetadata->fieldMappings[$parentField]->originalClass;
|
||||
}
|
||||
|
||||
/** @psalm-var class-string $parentClass */
|
||||
/** @psalm-var class-string $originalClass */
|
||||
|
||||
$this->reflFields[$field] = new ReflectionEmbeddedProperty(
|
||||
$this->getAccessibleProperty($parentClass, $parentField),
|
||||
$this->reflFields[$field],
|
||||
$originalClass,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->reflFields[$field];
|
||||
}
|
||||
|
||||
throw new OutOfBoundsException('Unknown field: ' . $this->classMetadata->name . ' ::$' . $field);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $offset
|
||||
* @param ReflectionProperty $value
|
||||
*/
|
||||
public function offsetSet($offset, $value): void // phpcs:ignore
|
||||
{
|
||||
$this->reflFields[$offset] = $value;
|
||||
}
|
||||
|
||||
/** @param string $offset */
|
||||
public function offsetUnset($offset): void // phpcs:ignore
|
||||
{
|
||||
unset($this->reflFields[$offset]);
|
||||
}
|
||||
|
||||
/** @psalm-param class-string $class */
|
||||
private function getAccessibleProperty(string $class, string $field): ReflectionProperty
|
||||
{
|
||||
$reflectionProperty = $this->reflectionService->getAccessibleProperty($class, $field);
|
||||
|
||||
assert($reflectionProperty !== null);
|
||||
|
||||
if ($reflectionProperty->isReadOnly()) {
|
||||
$declaringClass = $reflectionProperty->class;
|
||||
if ($declaringClass !== $class) {
|
||||
$reflectionProperty = $this->reflectionService->getAccessibleProperty($declaringClass, $field);
|
||||
|
||||
assert($reflectionProperty !== null);
|
||||
}
|
||||
|
||||
$reflectionProperty = new ReflectionReadonlyProperty($reflectionProperty);
|
||||
}
|
||||
|
||||
return $reflectionProperty;
|
||||
}
|
||||
|
||||
/** @return Generator<string, ReflectionProperty> */
|
||||
public function getIterator(): Traversable
|
||||
{
|
||||
Deprecation::trigger(
|
||||
'doctrine/orm',
|
||||
'https://github.com/doctrine/orm/pull/11659',
|
||||
'Access to ClassMetadata::$reflFields is deprecated and will be removed in Doctrine ORM 4.0.',
|
||||
);
|
||||
|
||||
$keys = array_keys($this->classMetadata->propertyAccessors);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
yield $key => $this->offsetGet($key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,10 +61,14 @@ final class ManyToManyOwningSideMapping extends ToManyOwningSideMapping implemen
|
||||
{
|
||||
if (isset($mappingArray['joinTable']['joinColumns'])) {
|
||||
foreach ($mappingArray['joinTable']['joinColumns'] as $key => $joinColumn) {
|
||||
if (empty($joinColumn['referencedColumnName'])) {
|
||||
$mappingArray['joinTable']['joinColumns'][$key]['referencedColumnName'] = $namingStrategy->referenceColumnName();
|
||||
}
|
||||
|
||||
if (empty($joinColumn['name'])) {
|
||||
$mappingArray['joinTable']['joinColumns'][$key]['name'] = $namingStrategy->joinKeyColumnName(
|
||||
$mappingArray['sourceEntity'],
|
||||
$joinColumn['referencedColumnName'] ?? null,
|
||||
$joinColumn['referencedColumnName'] ?? $namingStrategy->referenceColumnName(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -72,10 +76,14 @@ final class ManyToManyOwningSideMapping extends ToManyOwningSideMapping implemen
|
||||
|
||||
if (isset($mappingArray['joinTable']['inverseJoinColumns'])) {
|
||||
foreach ($mappingArray['joinTable']['inverseJoinColumns'] as $key => $joinColumn) {
|
||||
if (empty($joinColumn['referencedColumnName'])) {
|
||||
$mappingArray['joinTable']['inverseJoinColumns'][$key]['referencedColumnName'] = $namingStrategy->referenceColumnName();
|
||||
}
|
||||
|
||||
if (empty($joinColumn['name'])) {
|
||||
$mappingArray['joinTable']['inverseJoinColumns'][$key]['name'] = $namingStrategy->joinKeyColumnName(
|
||||
$mappingArray['targetEntity'],
|
||||
$joinColumn['referencedColumnName'] ?? null,
|
||||
$joinColumn['referencedColumnName'] ?? $namingStrategy->referenceColumnName(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -688,4 +688,13 @@ EXCEPTION
|
||||
$entityName,
|
||||
));
|
||||
}
|
||||
|
||||
public static function mappingVirtualPropertyNotAllowed(string $entityName, string $propertyName): self
|
||||
{
|
||||
return new self(sprintf(
|
||||
'Mapping virtual property "%s" on entity "%s" is not allowed.',
|
||||
$propertyName,
|
||||
$entityName,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
53
src/Mapping/PropertyAccessors/EmbeddablePropertyAccessor.php
Normal file
53
src/Mapping/PropertyAccessors/EmbeddablePropertyAccessor.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use Doctrine\Instantiator\Instantiator;
|
||||
use ReflectionProperty;
|
||||
|
||||
/** @internal */
|
||||
class EmbeddablePropertyAccessor implements PropertyAccessor
|
||||
{
|
||||
private static Instantiator|null $instantiator = null;
|
||||
|
||||
public function __construct(
|
||||
private PropertyAccessor $parent,
|
||||
private PropertyAccessor $child,
|
||||
/** @var class-string */
|
||||
private string $embeddedClass,
|
||||
) {
|
||||
}
|
||||
|
||||
public function setValue(object $object, mixed $value): void
|
||||
{
|
||||
$embeddedObject = $this->parent->getValue($object);
|
||||
|
||||
if ($embeddedObject === null) {
|
||||
self::$instantiator ??= new Instantiator();
|
||||
|
||||
$embeddedObject = self::$instantiator->instantiate($this->embeddedClass);
|
||||
|
||||
$this->parent->setValue($object, $embeddedObject);
|
||||
}
|
||||
|
||||
$this->child->setValue($embeddedObject, $value);
|
||||
}
|
||||
|
||||
public function getValue(object $object): mixed
|
||||
{
|
||||
$embeddedObject = $this->parent->getValue($object);
|
||||
|
||||
if ($embeddedObject === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->child->getValue($embeddedObject);
|
||||
}
|
||||
|
||||
public function getUnderlyingReflector(): ReflectionProperty
|
||||
{
|
||||
return $this->child->getUnderlyingReflector();
|
||||
}
|
||||
}
|
||||
85
src/Mapping/PropertyAccessors/EnumPropertyAccessor.php
Normal file
85
src/Mapping/PropertyAccessors/EnumPropertyAccessor.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use BackedEnum;
|
||||
use ReflectionProperty;
|
||||
|
||||
use function array_map;
|
||||
use function is_array;
|
||||
use function reset;
|
||||
|
||||
/** @internal */
|
||||
class EnumPropertyAccessor implements PropertyAccessor
|
||||
{
|
||||
/** @param class-string<BackedEnum> $enumType */
|
||||
public function __construct(private PropertyAccessor $parent, private string $enumType)
|
||||
{
|
||||
}
|
||||
|
||||
public function setValue(object $object, mixed $value): void
|
||||
{
|
||||
if ($value !== null) {
|
||||
$value = $this->toEnum($value);
|
||||
}
|
||||
|
||||
$this->parent->setValue($object, $value);
|
||||
}
|
||||
|
||||
public function getValue(object $object): mixed
|
||||
{
|
||||
$enum = $this->parent->getValue($object);
|
||||
|
||||
if ($enum === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->fromEnum($enum);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param BackedEnum|BackedEnum[] $enum
|
||||
*
|
||||
* @return ($enum is BackedEnum ? (string|int) : (string[]|int[]))
|
||||
*/
|
||||
private function fromEnum($enum) // phpcs:ignore
|
||||
{
|
||||
if (is_array($enum)) {
|
||||
return array_map(static function (BackedEnum $enum) {
|
||||
return $enum->value;
|
||||
}, $enum);
|
||||
}
|
||||
|
||||
return $enum->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-param BackedEnum|BackedEnum[]|int|string|int[]|string[] $value
|
||||
*
|
||||
* @return ($value is int|string|BackedEnum ? BackedEnum : BackedEnum[])
|
||||
*/
|
||||
private function toEnum($value): BackedEnum|array
|
||||
{
|
||||
if ($value instanceof BackedEnum) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (is_array($value)) {
|
||||
$v = reset($value);
|
||||
if ($v instanceof BackedEnum) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
return array_map([$this->enumType, 'from'], $value);
|
||||
}
|
||||
|
||||
return $this->enumType::from($value);
|
||||
}
|
||||
|
||||
public function getUnderlyingReflector(): ReflectionProperty
|
||||
{
|
||||
return $this->parent->getUnderlyingReflector();
|
||||
}
|
||||
}
|
||||
61
src/Mapping/PropertyAccessors/ObjectCastPropertyAccessor.php
Normal file
61
src/Mapping/PropertyAccessors/ObjectCastPropertyAccessor.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use Doctrine\ORM\Proxy\InternalProxy;
|
||||
use ReflectionProperty;
|
||||
|
||||
use function ltrim;
|
||||
|
||||
/** @internal */
|
||||
class ObjectCastPropertyAccessor implements PropertyAccessor
|
||||
{
|
||||
/** @param class-string $class */
|
||||
public static function fromNames(string $class, string $name): self
|
||||
{
|
||||
$reflectionProperty = new ReflectionProperty($class, $name);
|
||||
|
||||
$key = $reflectionProperty->isPrivate() ? "\0" . ltrim($class, '\\') . "\0" . $name : ($reflectionProperty->isProtected() ? "\0*\0" . $name : $name);
|
||||
|
||||
return new self($reflectionProperty, $key);
|
||||
}
|
||||
|
||||
public static function fromReflectionProperty(ReflectionProperty $reflectionProperty): self
|
||||
{
|
||||
$name = $reflectionProperty->getName();
|
||||
$key = $reflectionProperty->isPrivate() ? "\0" . ltrim($reflectionProperty->getDeclaringClass()->getName(), '\\') . "\0" . $name : ($reflectionProperty->isProtected() ? "\0*\0" . $name : $name);
|
||||
|
||||
return new self($reflectionProperty, $key);
|
||||
}
|
||||
|
||||
private function __construct(private ReflectionProperty $reflectionProperty, private string $key)
|
||||
{
|
||||
}
|
||||
|
||||
public function setValue(object $object, mixed $value): void
|
||||
{
|
||||
if (! ($object instanceof InternalProxy && ! $object->__isInitialized())) {
|
||||
$this->reflectionProperty->setValue($object, $value);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$object->__setInitialized(true);
|
||||
|
||||
$this->reflectionProperty->setValue($object, $value);
|
||||
|
||||
$object->__setInitialized(false);
|
||||
}
|
||||
|
||||
public function getValue(object $object): mixed
|
||||
{
|
||||
return ((array) $object)[$this->key] ?? null;
|
||||
}
|
||||
|
||||
public function getUnderlyingReflector(): ReflectionProperty
|
||||
{
|
||||
return $this->reflectionProperty;
|
||||
}
|
||||
}
|
||||
27
src/Mapping/PropertyAccessors/PropertyAccessor.php
Normal file
27
src/Mapping/PropertyAccessors/PropertyAccessor.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use ReflectionProperty;
|
||||
|
||||
/**
|
||||
* A property accessor is a class that allows to read and write properties on objects regardless of visibility.
|
||||
*
|
||||
* We use them while creating objects from database rows in {@link UnitOfWork::createEntity()} or when
|
||||
* computing changesets from objects that are about to be written back to the database in {@link UnitOfWork::computeChangeSet()}.
|
||||
*
|
||||
* This abstraction over ReflectionProperty is necessary, because for several features of either Doctrine or PHP, we
|
||||
* need to handle edge cases in reflection at a central location in the code.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
interface PropertyAccessor
|
||||
{
|
||||
public function setValue(object $object, mixed $value): void;
|
||||
|
||||
public function getValue(object $object): mixed;
|
||||
|
||||
public function getUnderlyingReflector(): ReflectionProperty;
|
||||
}
|
||||
32
src/Mapping/PropertyAccessors/PropertyAccessorFactory.php
Normal file
32
src/Mapping/PropertyAccessors/PropertyAccessorFactory.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use ReflectionProperty;
|
||||
|
||||
use const PHP_VERSION_ID;
|
||||
|
||||
class PropertyAccessorFactory
|
||||
{
|
||||
/** @phpstan-param class-string $className */
|
||||
public static function createPropertyAccessor(string $className, string $propertyName): PropertyAccessor
|
||||
{
|
||||
$reflectionProperty = new ReflectionProperty($className, $propertyName);
|
||||
|
||||
$accessor = PHP_VERSION_ID >= 80400
|
||||
? RawValuePropertyAccessor::fromReflectionProperty($reflectionProperty)
|
||||
: ObjectCastPropertyAccessor::fromReflectionProperty($reflectionProperty);
|
||||
|
||||
if ($reflectionProperty->hasType() && ! $reflectionProperty->getType()->allowsNull()) {
|
||||
$accessor = new TypedNoDefaultPropertyAccessor($accessor, $reflectionProperty);
|
||||
}
|
||||
|
||||
if ($reflectionProperty->isReadOnly()) {
|
||||
$accessor = new ReadonlyAccessor($accessor, $reflectionProperty);
|
||||
}
|
||||
|
||||
return $accessor;
|
||||
}
|
||||
}
|
||||
63
src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php
Normal file
63
src/Mapping/PropertyAccessors/RawValuePropertyAccessor.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use Doctrine\ORM\Proxy\InternalProxy;
|
||||
use LogicException;
|
||||
use ReflectionProperty;
|
||||
|
||||
use function ltrim;
|
||||
|
||||
use const PHP_VERSION_ID;
|
||||
|
||||
/**
|
||||
* This is a PHP 8.4 and up only class and replaces ObjectCastPropertyAccessor.
|
||||
*
|
||||
* It works based on the raw values of a property, which for a case of property hooks
|
||||
* is the backed value. If we kept using setValue/getValue, this would go through the hooks,
|
||||
* which potentially change the data.
|
||||
*/
|
||||
class RawValuePropertyAccessor implements PropertyAccessor
|
||||
{
|
||||
public static function fromReflectionProperty(ReflectionProperty $reflectionProperty): self
|
||||
{
|
||||
$name = $reflectionProperty->getName();
|
||||
$key = $reflectionProperty->isPrivate() ? "\0" . ltrim($reflectionProperty->getDeclaringClass()->getName(), '\\') . "\0" . $name : ($reflectionProperty->isProtected() ? "\0*\0" . $name : $name);
|
||||
|
||||
return new self($reflectionProperty, $key);
|
||||
}
|
||||
|
||||
private function __construct(private ReflectionProperty $reflectionProperty, private string $key)
|
||||
{
|
||||
if (PHP_VERSION_ID < 80400) {
|
||||
throw new LogicException('This class requires PHP 8.4 or higher.');
|
||||
}
|
||||
}
|
||||
|
||||
public function setValue(object $object, mixed $value): void
|
||||
{
|
||||
if (! ($object instanceof InternalProxy && ! $object->__isInitialized())) {
|
||||
$this->reflectionProperty->setRawValueWithoutLazyInitialization($object, $value);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$object->__setInitialized(true);
|
||||
|
||||
$this->reflectionProperty->setRawValue($object, $value);
|
||||
|
||||
$object->__setInitialized(false);
|
||||
}
|
||||
|
||||
public function getValue(object $object): mixed
|
||||
{
|
||||
return ((array) $object)[$this->key] ?? null;
|
||||
}
|
||||
|
||||
public function getUnderlyingReflector(): ReflectionProperty
|
||||
{
|
||||
return $this->reflectionProperty;
|
||||
}
|
||||
}
|
||||
53
src/Mapping/PropertyAccessors/ReadonlyAccessor.php
Normal file
53
src/Mapping/PropertyAccessors/ReadonlyAccessor.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
use ReflectionProperty;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
/** @internal */
|
||||
class ReadonlyAccessor implements PropertyAccessor
|
||||
{
|
||||
public function __construct(private PropertyAccessor $parent, private ReflectionProperty $reflectionProperty)
|
||||
{
|
||||
if (! $this->reflectionProperty->isReadOnly()) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'%s::$%s must be readonly property',
|
||||
$this->reflectionProperty->getDeclaringClass()->getName(),
|
||||
$this->reflectionProperty->getName(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function setValue(object $object, mixed $value): void
|
||||
{
|
||||
if (! $this->reflectionProperty->isInitialized($object)) {
|
||||
$this->parent->setValue($object, $value);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->parent->getValue($object) !== $value) {
|
||||
throw new LogicException(sprintf(
|
||||
'Attempting to change readonly property %s::$%s.',
|
||||
$this->reflectionProperty->getDeclaringClass()->getName(),
|
||||
$this->reflectionProperty->getName(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function getValue(object $object): mixed
|
||||
{
|
||||
return $this->parent->getValue($object);
|
||||
}
|
||||
|
||||
public function getUnderlyingReflector(): ReflectionProperty
|
||||
{
|
||||
return $this->reflectionProperty;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use Closure;
|
||||
use InvalidArgumentException;
|
||||
use ReflectionProperty;
|
||||
|
||||
use function assert;
|
||||
use function sprintf;
|
||||
|
||||
/** @internal */
|
||||
class TypedNoDefaultPropertyAccessor implements PropertyAccessor
|
||||
{
|
||||
private Closure|null $unsetter = null;
|
||||
|
||||
public function __construct(private PropertyAccessor $parent, private ReflectionProperty $reflectionProperty)
|
||||
{
|
||||
if (! $this->reflectionProperty->hasType()) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'%s::$%s must have a type when used with TypedNoDefaultPropertyAccessor',
|
||||
$this->reflectionProperty->getDeclaringClass()->getName(),
|
||||
$this->reflectionProperty->getName(),
|
||||
));
|
||||
}
|
||||
|
||||
if ($this->reflectionProperty->getType()->allowsNull()) {
|
||||
throw new InvalidArgumentException(sprintf(
|
||||
'%s::$%s must not be nullable when used with TypedNoDefaultPropertyAccessor',
|
||||
$this->reflectionProperty->getDeclaringClass()->getName(),
|
||||
$this->reflectionProperty->getName(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
public function setValue(object $object, mixed $value): void
|
||||
{
|
||||
if ($value === null) {
|
||||
if ($this->unsetter === null) {
|
||||
$propertyName = $this->reflectionProperty->getName();
|
||||
$this->unsetter = function () use ($propertyName): void {
|
||||
unset($this->$propertyName);
|
||||
};
|
||||
}
|
||||
|
||||
$unsetter = $this->unsetter->bindTo($object, $this->reflectionProperty->getDeclaringClass()->getName());
|
||||
|
||||
assert($unsetter instanceof Closure);
|
||||
|
||||
$unsetter();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->parent->setValue($object, $value);
|
||||
}
|
||||
|
||||
public function getValue(object $object): mixed
|
||||
{
|
||||
return $this->reflectionProperty->isInitialized($object) ? $this->parent->getValue($object) : null;
|
||||
}
|
||||
|
||||
public function getUnderlyingReflector(): ReflectionProperty
|
||||
{
|
||||
return $this->reflectionProperty;
|
||||
}
|
||||
}
|
||||
@@ -107,6 +107,10 @@ abstract class ToOneOwningSideMapping extends OwningSideMapping implements ToOne
|
||||
if (empty($joinColumn['name'])) {
|
||||
$mappingArray['joinColumns'][$index]['name'] = $namingStrategy->joinColumnName($mappingArray['fieldName'], $name);
|
||||
}
|
||||
|
||||
if (empty($joinColumn['referencedColumnName'])) {
|
||||
$mappingArray['joinColumns'][$index]['referencedColumnName'] = $namingStrategy->referenceColumnName();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -140,7 +140,7 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
|
||||
if ($this->backRefFieldName && $this->getMapping()->isOneToMany()) {
|
||||
assert($this->typeClass !== null);
|
||||
// Set back reference to owner
|
||||
$this->typeClass->reflFields[$this->backRefFieldName]->setValue(
|
||||
$this->typeClass->propertyAccessors[$this->backRefFieldName]->setValue(
|
||||
$element,
|
||||
$this->owner,
|
||||
);
|
||||
@@ -166,7 +166,7 @@ final class PersistentCollection extends AbstractLazyCollection implements Selec
|
||||
if ($this->backRefFieldName && $this->getMapping()->isOneToMany()) {
|
||||
assert($this->typeClass !== null);
|
||||
// Set back reference to owner
|
||||
$this->typeClass->reflFields[$this->backRefFieldName]->setValue(
|
||||
$this->typeClass->propertyAccessors[$this->backRefFieldName]->setValue(
|
||||
$element,
|
||||
$this->owner,
|
||||
);
|
||||
|
||||
@@ -480,7 +480,7 @@ class BasicEntityPersister implements EntityPersister
|
||||
|
||||
$where[] = $versionColumn;
|
||||
$types[] = $this->class->fieldMappings[$versionField]->type;
|
||||
$params[] = $this->class->reflFields[$versionField]->getValue($entity);
|
||||
$params[] = $this->class->propertyAccessors[$versionField]->getValue($entity);
|
||||
|
||||
switch ($versionFieldType) {
|
||||
case Types::SMALLINT:
|
||||
@@ -791,7 +791,7 @@ class BasicEntityPersister implements EntityPersister
|
||||
|
||||
// Complete bidirectional association, if necessary
|
||||
if ($targetEntity !== null && $isInverseSingleValued) {
|
||||
$targetClass->reflFields[$assoc->inversedBy]->setValue($targetEntity, $sourceEntity);
|
||||
$targetClass->propertyAccessors[$assoc->inversedBy]->setValue($targetEntity, $sourceEntity);
|
||||
}
|
||||
|
||||
return $targetEntity;
|
||||
@@ -838,7 +838,7 @@ class BasicEntityPersister implements EntityPersister
|
||||
}
|
||||
} else {
|
||||
$computedIdentifier[$targetClass->getFieldForColumn($targetKeyColumn)] =
|
||||
$sourceClass->reflFields[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
|
||||
$sourceClass->propertyAccessors[$sourceClass->fieldNames[$sourceKeyColumn]]->getValue($sourceEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1055,7 +1055,7 @@ class BasicEntityPersister implements EntityPersister
|
||||
switch (true) {
|
||||
case $sourceClass->containsForeignIdentifier:
|
||||
$field = $sourceClass->getFieldForColumn($sourceKeyColumn);
|
||||
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
|
||||
$value = $sourceClass->propertyAccessors[$field]->getValue($sourceEntity);
|
||||
|
||||
if (isset($sourceClass->associationMappings[$field])) {
|
||||
$value = $this->em->getUnitOfWork()->getEntityIdentifier($value);
|
||||
@@ -1066,7 +1066,7 @@ class BasicEntityPersister implements EntityPersister
|
||||
|
||||
case isset($sourceClass->fieldNames[$sourceKeyColumn]):
|
||||
$field = $sourceClass->fieldNames[$sourceKeyColumn];
|
||||
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
|
||||
$value = $sourceClass->propertyAccessors[$field]->getValue($sourceEntity);
|
||||
|
||||
break;
|
||||
|
||||
@@ -1470,7 +1470,7 @@ class BasicEntityPersister implements EntityPersister
|
||||
{
|
||||
$columns = [];
|
||||
|
||||
foreach ($this->class->reflFields as $name => $field) {
|
||||
foreach ($this->class->propertyAccessors as $name => $field) {
|
||||
if ($this->class->isVersioned && $this->class->versionField === $name) {
|
||||
continue;
|
||||
}
|
||||
@@ -1824,7 +1824,7 @@ class BasicEntityPersister implements EntityPersister
|
||||
foreach ($owningAssoc->targetToSourceKeyColumns as $sourceKeyColumn => $targetKeyColumn) {
|
||||
if ($sourceClass->containsForeignIdentifier) {
|
||||
$field = $sourceClass->getFieldForColumn($sourceKeyColumn);
|
||||
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
|
||||
$value = $sourceClass->propertyAccessors[$field]->getValue($sourceEntity);
|
||||
|
||||
if (isset($sourceClass->associationMappings[$field])) {
|
||||
$value = $this->em->getUnitOfWork()->getEntityIdentifier($value);
|
||||
@@ -1842,7 +1842,7 @@ class BasicEntityPersister implements EntityPersister
|
||||
}
|
||||
|
||||
$field = $sourceClass->fieldNames[$sourceKeyColumn];
|
||||
$value = $sourceClass->reflFields[$field]->getValue($sourceEntity);
|
||||
$value = $sourceClass->propertyAccessors[$field]->getValue($sourceEntity);
|
||||
|
||||
$criteria[$tableAlias . '.' . $targetKeyColumn] = $value;
|
||||
$parameters[] = [
|
||||
|
||||
@@ -460,7 +460,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
|
||||
? $this->class->getIdentifierColumnNames()
|
||||
: [];
|
||||
|
||||
foreach ($this->class->reflFields as $name => $field) {
|
||||
foreach ($this->class->propertyAccessors as $name => $field) {
|
||||
if (
|
||||
isset($this->class->fieldMappings[$name]->inherited)
|
||||
&& ! isset($this->class->fieldMappings[$name]->id)
|
||||
|
||||
@@ -13,16 +13,19 @@ use Doctrine\ORM\UnitOfWork;
|
||||
use Doctrine\ORM\Utility\IdentifierFlattener;
|
||||
use Doctrine\Persistence\Mapping\ClassMetadata;
|
||||
use Doctrine\Persistence\Proxy;
|
||||
use LogicException;
|
||||
use ReflectionClass;
|
||||
use ReflectionProperty;
|
||||
use Symfony\Component\VarExporter\ProxyHelper;
|
||||
|
||||
use function array_combine;
|
||||
use function array_flip;
|
||||
use function array_intersect_key;
|
||||
use function array_keys;
|
||||
use function assert;
|
||||
use function bin2hex;
|
||||
use function chmod;
|
||||
use function class_exists;
|
||||
use function count;
|
||||
use function dirname;
|
||||
use function file_exists;
|
||||
use function file_put_contents;
|
||||
@@ -37,6 +40,7 @@ use function preg_match_all;
|
||||
use function random_bytes;
|
||||
use function rename;
|
||||
use function rtrim;
|
||||
use function sprintf;
|
||||
use function str_replace;
|
||||
use function strpos;
|
||||
use function strrpos;
|
||||
@@ -45,6 +49,7 @@ use function substr;
|
||||
use function ucfirst;
|
||||
|
||||
use const DIRECTORY_SEPARATOR;
|
||||
use const PHP_VERSION_ID;
|
||||
|
||||
/**
|
||||
* This factory is used to create proxy objects for entities at runtime.
|
||||
@@ -127,6 +132,9 @@ EOPHP;
|
||||
/** @var array<class-string, Closure> */
|
||||
private array $proxyFactories = [];
|
||||
|
||||
private readonly string $proxyDir;
|
||||
private readonly string $proxyNs;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is
|
||||
* connected to the given <tt>EntityManager</tt>.
|
||||
@@ -138,15 +146,15 @@ EOPHP;
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly string $proxyDir,
|
||||
private readonly string $proxyNs,
|
||||
string|null $proxyDir = null,
|
||||
string|null $proxyNs = null,
|
||||
bool|int $autoGenerate = self::AUTOGENERATE_NEVER,
|
||||
) {
|
||||
if (! $proxyDir) {
|
||||
if (! $proxyDir && ! $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
throw ORMInvalidArgumentException::proxyDirectoryRequired();
|
||||
}
|
||||
|
||||
if (! $proxyNs) {
|
||||
if (! $proxyNs && ! $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
throw ORMInvalidArgumentException::proxyNamespaceRequired();
|
||||
}
|
||||
|
||||
@@ -154,6 +162,17 @@ EOPHP;
|
||||
throw ORMInvalidArgumentException::invalidAutoGenerateMode($autoGenerate);
|
||||
}
|
||||
|
||||
if ($proxyDir === null && $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
$proxyDir = '';
|
||||
}
|
||||
|
||||
if ($proxyNs === null && $em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
$proxyNs = '';
|
||||
}
|
||||
|
||||
$this->proxyDir = $proxyDir;
|
||||
$this->proxyNs = $proxyNs;
|
||||
|
||||
$this->uow = $em->getUnitOfWork();
|
||||
$this->autoGenerate = (int) $autoGenerate;
|
||||
$this->identifierFlattener = new IdentifierFlattener($this->uow, $em->getMetadataFactory());
|
||||
@@ -163,8 +182,35 @@ EOPHP;
|
||||
* @param class-string $className
|
||||
* @param array<mixed> $identifier
|
||||
*/
|
||||
public function getProxy(string $className, array $identifier): InternalProxy
|
||||
public function getProxy(string $className, array $identifier): object
|
||||
{
|
||||
if ($this->em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
$classMetadata = $this->em->getClassMetadata($className);
|
||||
$entityPersister = $this->uow->getEntityPersister($className);
|
||||
$identifierFlattener = $this->identifierFlattener;
|
||||
|
||||
$proxy = $classMetadata->reflClass->newLazyGhost(static function (object $object) use (
|
||||
$identifier,
|
||||
$entityPersister,
|
||||
$identifierFlattener,
|
||||
$classMetadata,
|
||||
): void {
|
||||
$original = $entityPersister->loadById($identifier, $object);
|
||||
if ($original === null) {
|
||||
throw EntityNotFoundException::fromClassNameAndIdentifier(
|
||||
$classMetadata->getName(),
|
||||
$identifierFlattener->flattenIdentifier($classMetadata, $identifier),
|
||||
);
|
||||
}
|
||||
}, ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE);
|
||||
|
||||
foreach ($identifier as $idField => $value) {
|
||||
$classMetadata->propertyAccessors[$idField]->setValue($proxy, $value);
|
||||
}
|
||||
|
||||
return $proxy;
|
||||
}
|
||||
|
||||
$proxyFactory = $this->proxyFactories[$className] ?? $this->getProxyFactory($className);
|
||||
|
||||
return $proxyFactory($identifier);
|
||||
@@ -182,6 +228,10 @@ EOPHP;
|
||||
*/
|
||||
public function generateProxyClasses(array $classes, string|null $proxyDir = null): int
|
||||
{
|
||||
if ($this->em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$generated = 0;
|
||||
|
||||
foreach ($classes as $class) {
|
||||
@@ -232,8 +282,8 @@ EOPHP;
|
||||
|
||||
$class = $entityPersister->getClassMetadata();
|
||||
|
||||
foreach ($class->getReflectionProperties() as $property) {
|
||||
if (! $property || isset($identifier[$property->getName()])) {
|
||||
foreach ($class->getPropertyAccessors() as $name => $property) {
|
||||
if (isset($identifier[$name])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -262,6 +312,14 @@ EOPHP;
|
||||
foreach ($reflector->getProperties($filter) as $property) {
|
||||
$name = $property->name;
|
||||
|
||||
if (PHP_VERSION_ID >= 80400 && count($property->getHooks()) > 0) {
|
||||
throw new LogicException(sprintf(
|
||||
'Doctrine ORM does not support property hook on %s::%s without using native lazy objects. Check https://github.com/doctrine/orm/issues/11624 for details of versions that support property hooks.',
|
||||
$property->getDeclaringClass()->getName(),
|
||||
$property->getName(),
|
||||
));
|
||||
}
|
||||
|
||||
if ($property->isStatic() || ! isset($identifiers[$name])) {
|
||||
continue;
|
||||
}
|
||||
@@ -279,7 +337,11 @@ EOPHP;
|
||||
$entityPersister = $this->uow->getEntityPersister($className);
|
||||
$initializer = $this->createLazyInitializer($class, $entityPersister, $this->identifierFlattener);
|
||||
$proxyClassName = $this->loadProxyClass($class);
|
||||
$identifierFields = array_intersect_key($class->getReflectionProperties(), $identifiers);
|
||||
$identifierFields = [];
|
||||
|
||||
foreach (array_keys($identifiers) as $identifier) {
|
||||
$identifierFields[$identifier] = $class->getPropertyAccessor($identifier);
|
||||
}
|
||||
|
||||
$proxyFactory = Closure::bind(static function (array $identifier) use ($initializer, $skippedProperties, $identifierFields, $className): InternalProxy {
|
||||
$proxy = self::createLazyGhost(static function (InternalProxy $object) use ($initializer, $identifier): void {
|
||||
|
||||
26
src/Query/AST/EntityAsDtoArgumentExpression.php
Normal file
26
src/Query/AST/EntityAsDtoArgumentExpression.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Query\AST;
|
||||
|
||||
use Doctrine\ORM\Query\SqlWalker;
|
||||
|
||||
/**
|
||||
* EntityAsDtoArgumentExpression ::= IdentificationVariable
|
||||
*
|
||||
* @link www.doctrine-project.org
|
||||
*/
|
||||
class EntityAsDtoArgumentExpression extends Node
|
||||
{
|
||||
public function __construct(
|
||||
public mixed $expression,
|
||||
public string|null $identificationVariable,
|
||||
) {
|
||||
}
|
||||
|
||||
public function dispatch(SqlWalker $walker): string
|
||||
{
|
||||
return $walker->walkEntityAsDtoArgumentExpression($this);
|
||||
}
|
||||
}
|
||||
@@ -1106,6 +1106,50 @@ final class Parser
|
||||
return $this->PathExpression(AST\PathExpression::TYPE_COLLECTION_VALUED_ASSOCIATION);
|
||||
}
|
||||
|
||||
/**
|
||||
* EntityAsDtoArgumentExpression ::= IdentificationVariable
|
||||
*/
|
||||
public function EntityAsDtoArgumentExpression(): AST\EntityAsDtoArgumentExpression
|
||||
{
|
||||
assert($this->lexer->lookahead !== null);
|
||||
$expression = null;
|
||||
$identVariable = null;
|
||||
$peek = $this->lexer->glimpse();
|
||||
$lookaheadType = $this->lexer->lookahead->type;
|
||||
assert($peek !== null);
|
||||
|
||||
assert($lookaheadType === TokenType::T_IDENTIFIER);
|
||||
assert($peek->type !== TokenType::T_DOT);
|
||||
assert($peek->type !== TokenType::T_OPEN_PARENTHESIS);
|
||||
|
||||
$expression = $identVariable = $this->IdentificationVariable();
|
||||
|
||||
// [["AS"] AliasResultVariable]
|
||||
$mustHaveAliasResultVariable = false;
|
||||
|
||||
if ($this->lexer->isNextToken(TokenType::T_AS)) {
|
||||
$this->match(TokenType::T_AS);
|
||||
|
||||
$mustHaveAliasResultVariable = true;
|
||||
}
|
||||
|
||||
$aliasResultVariable = null;
|
||||
|
||||
if ($mustHaveAliasResultVariable || $this->lexer->isNextToken(TokenType::T_IDENTIFIER)) {
|
||||
$token = $this->lexer->lookahead;
|
||||
$aliasResultVariable = $this->AliasResultVariable();
|
||||
|
||||
// Include AliasResultVariable in query components.
|
||||
$this->queryComponents[$aliasResultVariable] = [
|
||||
'resultVariable' => $expression,
|
||||
'nestingLevel' => $this->nestingLevel,
|
||||
'token' => $token,
|
||||
];
|
||||
}
|
||||
|
||||
return new AST\EntityAsDtoArgumentExpression($expression, $identVariable);
|
||||
}
|
||||
|
||||
/**
|
||||
* SelectClause ::= "SELECT" ["DISTINCT"] SelectExpression {"," SelectExpression}
|
||||
*/
|
||||
@@ -1849,6 +1893,8 @@ final class Parser
|
||||
$this->match(TokenType::T_CLOSE_PARENTHESIS);
|
||||
} elseif ($token->type === TokenType::T_NEW) {
|
||||
$expression = $this->NewObjectExpression();
|
||||
} elseif ($token->type === TokenType::T_IDENTIFIER && $peek->type !== TokenType::T_DOT && $peek->type !== TokenType::T_OPEN_PARENTHESIS) {
|
||||
$expression = $this->EntityAsDtoArgumentExpression();
|
||||
} else {
|
||||
$expression = $this->ScalarExpression();
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ class ResultSetMapping
|
||||
/**
|
||||
* Maps last argument for new objects in order to initiate object construction
|
||||
*
|
||||
* @phpstan-var array<int|string, array{ownerIndex: string|int, argIndex: int|string}>
|
||||
* @phpstan-var array<int|string, array{ownerIndex: string|int, argIndex: int|string, argAlias: string}>
|
||||
*/
|
||||
public array $nestedNewObjectArguments = [];
|
||||
|
||||
@@ -187,6 +187,13 @@ class ResultSetMapping
|
||||
*/
|
||||
public array $discriminatorParameters = [];
|
||||
|
||||
/**
|
||||
* Entities nested in Dto's
|
||||
*
|
||||
* @phpstan-var array<string, array<string, (int|string)>>
|
||||
*/
|
||||
public array $nestedEntities = [];
|
||||
|
||||
/**
|
||||
* Adds an entity result to this ResultSetMapping.
|
||||
*
|
||||
|
||||
@@ -575,6 +575,14 @@ class SqlWalker
|
||||
return implode(', ', $sqlParts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks down an EntityAsDtoArgumentExpression AST node, thereby generating the appropriate SQL.
|
||||
*/
|
||||
public function walkEntityAsDtoArgumentExpression(AST\EntityAsDtoArgumentExpression $expr): string
|
||||
{
|
||||
return implode(', ', $this->walkObjectExpression($expr->expression, [], $expr->identificationVariable ?: null));
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks down an IdentificationVariable (no AST node associated), thereby generating the SQL.
|
||||
*/
|
||||
@@ -1356,32 +1364,79 @@ class SqlWalker
|
||||
$partialFieldSet = [];
|
||||
}
|
||||
|
||||
$class = $this->getMetadataForDqlAlias($dqlAlias);
|
||||
$resultAlias = $selectExpression->fieldIdentificationVariable ?: null;
|
||||
$sql .= implode(', ', $this->walkObjectExpression($dqlAlias, $partialFieldSet, $selectExpression->fieldIdentificationVariable ?: null));
|
||||
}
|
||||
|
||||
if (! isset($this->selectedClasses[$dqlAlias])) {
|
||||
$this->selectedClasses[$dqlAlias] = [
|
||||
'class' => $class,
|
||||
'dqlAlias' => $dqlAlias,
|
||||
'resultAlias' => $resultAlias,
|
||||
];
|
||||
}
|
||||
return $sql;
|
||||
}
|
||||
|
||||
$sqlParts = [];
|
||||
/**
|
||||
* Walks down an Object Expression AST node and return Sql Parts
|
||||
*
|
||||
* @param mixed[] $partialFieldSet
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function walkObjectExpression(string $dqlAlias, array $partialFieldSet, string|null $resultAlias): array
|
||||
{
|
||||
$class = $this->getMetadataForDqlAlias($dqlAlias);
|
||||
|
||||
// Select all fields from the queried class
|
||||
foreach ($class->fieldMappings as $fieldName => $mapping) {
|
||||
if ($partialFieldSet && ! in_array($fieldName, $partialFieldSet, true)) {
|
||||
if (! isset($this->selectedClasses[$dqlAlias])) {
|
||||
$this->selectedClasses[$dqlAlias] = [
|
||||
'class' => $class,
|
||||
'dqlAlias' => $dqlAlias,
|
||||
'resultAlias' => $resultAlias,
|
||||
];
|
||||
}
|
||||
|
||||
$sqlParts = [];
|
||||
|
||||
// Select all fields from the queried class
|
||||
foreach ($class->fieldMappings as $fieldName => $mapping) {
|
||||
if ($partialFieldSet && ! in_array($fieldName, $partialFieldSet, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tableName = isset($mapping->inherited)
|
||||
? $this->em->getClassMetadata($mapping->inherited)->getTableName()
|
||||
: $class->getTableName();
|
||||
|
||||
$sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias);
|
||||
$columnAlias = $this->getSQLColumnAlias($mapping->columnName);
|
||||
$quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
|
||||
|
||||
$col = $sqlTableAlias . '.' . $quotedColumnName;
|
||||
|
||||
$type = Type::getType($mapping->type);
|
||||
$col = $type->convertToPHPValueSQL($col, $this->platform);
|
||||
|
||||
$sqlParts[] = $col . ' AS ' . $columnAlias;
|
||||
|
||||
$this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
|
||||
|
||||
$this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name);
|
||||
|
||||
if (! empty($mapping->enumType)) {
|
||||
$this->rsm->addEnumResult($columnAlias, $mapping->enumType);
|
||||
}
|
||||
}
|
||||
|
||||
// Add any additional fields of subclasses (excluding inherited fields)
|
||||
// 1) on Single Table Inheritance: always, since its marginal overhead
|
||||
// 2) on Class Table Inheritance only if partial objects are disallowed,
|
||||
// since it requires outer joining subtables.
|
||||
if ($class->isInheritanceTypeSingleTable() || ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
|
||||
foreach ($class->subClasses as $subClassName) {
|
||||
$subClass = $this->em->getClassMetadata($subClassName);
|
||||
$sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
|
||||
|
||||
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
||||
if (isset($mapping->inherited) || ($partialFieldSet && ! in_array($fieldName, $partialFieldSet, true))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tableName = isset($mapping->inherited)
|
||||
? $this->em->getClassMetadata($mapping->inherited)->getTableName()
|
||||
: $class->getTableName();
|
||||
|
||||
$sqlTableAlias = $this->getSQLTableAlias($tableName, $dqlAlias);
|
||||
$columnAlias = $this->getSQLColumnAlias($mapping->columnName);
|
||||
$quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $class, $this->platform);
|
||||
$quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $subClass, $this->platform);
|
||||
|
||||
$col = $sqlTableAlias . '.' . $quotedColumnName;
|
||||
|
||||
@@ -1392,48 +1447,12 @@ class SqlWalker
|
||||
|
||||
$this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
|
||||
|
||||
$this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name);
|
||||
|
||||
if (! empty($mapping->enumType)) {
|
||||
$this->rsm->addEnumResult($columnAlias, $mapping->enumType);
|
||||
}
|
||||
$this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $subClassName);
|
||||
}
|
||||
|
||||
// Add any additional fields of subclasses (excluding inherited fields)
|
||||
// 1) on Single Table Inheritance: always, since its marginal overhead
|
||||
// 2) on Class Table Inheritance only if partial objects are disallowed,
|
||||
// since it requires outer joining subtables.
|
||||
if ($class->isInheritanceTypeSingleTable() || ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD)) {
|
||||
foreach ($class->subClasses as $subClassName) {
|
||||
$subClass = $this->em->getClassMetadata($subClassName);
|
||||
$sqlTableAlias = $this->getSQLTableAlias($subClass->getTableName(), $dqlAlias);
|
||||
|
||||
foreach ($subClass->fieldMappings as $fieldName => $mapping) {
|
||||
if (isset($mapping->inherited) || ($partialFieldSet && ! in_array($fieldName, $partialFieldSet, true))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$columnAlias = $this->getSQLColumnAlias($mapping->columnName);
|
||||
$quotedColumnName = $this->quoteStrategy->getColumnName($fieldName, $subClass, $this->platform);
|
||||
|
||||
$col = $sqlTableAlias . '.' . $quotedColumnName;
|
||||
|
||||
$type = Type::getType($mapping->type);
|
||||
$col = $type->convertToPHPValueSQL($col, $this->platform);
|
||||
|
||||
$sqlParts[] = $col . ' AS ' . $columnAlias;
|
||||
|
||||
$this->scalarResultAliasMap[$resultAlias][] = $columnAlias;
|
||||
|
||||
$this->rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $subClassName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$sql .= implode(', ', $sqlParts);
|
||||
}
|
||||
}
|
||||
|
||||
return $sql;
|
||||
return $sqlParts;
|
||||
}
|
||||
|
||||
public function walkQuantifiedExpression(AST\QuantifiedExpression $qExpr): string
|
||||
@@ -1549,6 +1568,14 @@ class SqlWalker
|
||||
$sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
|
||||
break;
|
||||
|
||||
case $e instanceof AST\EntityAsDtoArgumentExpression:
|
||||
$alias = $e->identificationVariable ?: $columnAlias;
|
||||
$this->rsm->nestedNewObjectArguments[$columnAlias] = ['ownerIndex' => $objIndex, 'argIndex' => $argIndex, 'argAlias' => $alias];
|
||||
$this->rsm->nestedEntities[$alias] = ['parent' => $objIndex, 'argIndex' => $argIndex, 'type' => 'entity'];
|
||||
|
||||
$sqlSelectExpressions[] = trim($e->dispatch($this));
|
||||
break;
|
||||
|
||||
default:
|
||||
$sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
|
||||
break;
|
||||
|
||||
@@ -9,7 +9,6 @@ use Doctrine\Common\Collections\Criteria;
|
||||
use Doctrine\DBAL\ArrayParameterType;
|
||||
use Doctrine\DBAL\ParameterType;
|
||||
use Doctrine\ORM\Internal\NoUnknownNamedArguments;
|
||||
use Doctrine\ORM\Internal\QueryType;
|
||||
use Doctrine\ORM\Query\Expr;
|
||||
use Doctrine\ORM\Query\Parameter;
|
||||
use Doctrine\ORM\Query\QueryExpressionVisitor;
|
||||
@@ -128,6 +127,11 @@ class QueryBuilder implements Stringable
|
||||
$this->parameters = new ArrayCollection();
|
||||
}
|
||||
|
||||
final protected function getType(): QueryType
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an ExpressionBuilder used for object-oriented construction of query expressions.
|
||||
* This producer method is intended for convenient inline usage. Example:
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Internal;
|
||||
namespace Doctrine\ORM;
|
||||
|
||||
/** @internal To be used inside the QueryBuilder only. */
|
||||
enum QueryType
|
||||
{
|
||||
case Select;
|
||||
28
src/Tools/Console/ApplicationCompatibility.php
Normal file
28
src/Tools/Console/ApplicationCompatibility.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\ORM\Tools\Console;
|
||||
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
|
||||
use function method_exists;
|
||||
|
||||
/**
|
||||
* Forward compatibility with Symfony Console 7.4
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
trait ApplicationCompatibility
|
||||
{
|
||||
private static function addCommandToApplication(Application $application, Command $command): Command|null
|
||||
{
|
||||
if (method_exists(Application::class, 'addCommand')) {
|
||||
// @phpstan-ignore method.notFound (This method will be added in Symfony 7.4)
|
||||
return $application->addCommand($command);
|
||||
}
|
||||
|
||||
return $application->add($command);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,8 @@ use function class_exists;
|
||||
*/
|
||||
final class ConsoleRunner
|
||||
{
|
||||
use ApplicationCompatibility;
|
||||
|
||||
/**
|
||||
* Runs console with the given helper set.
|
||||
*
|
||||
@@ -59,7 +61,10 @@ final class ConsoleRunner
|
||||
$connectionProvider = new ConnectionFromManagerProvider($entityManagerProvider);
|
||||
|
||||
if (class_exists(DBALConsole\Command\ReservedWordsCommand::class)) {
|
||||
$cli->add(new DBALConsole\Command\ReservedWordsCommand($connectionProvider));
|
||||
self::addCommandToApplication(
|
||||
$cli,
|
||||
new DBALConsole\Command\ReservedWordsCommand($connectionProvider),
|
||||
);
|
||||
}
|
||||
|
||||
$cli->addCommands(
|
||||
|
||||
@@ -718,6 +718,10 @@ class SchemaTool
|
||||
if (isset($joinColumn->onDelete)) {
|
||||
$fkOptions['onDelete'] = $joinColumn->onDelete;
|
||||
}
|
||||
|
||||
if (isset($joinColumn->deferrable)) {
|
||||
$fkOptions['deferrable'] = $joinColumn->deferrable;
|
||||
}
|
||||
}
|
||||
|
||||
// Prefer unique constraints over implicit simple indexes created for foreign keys.
|
||||
|
||||
@@ -31,7 +31,6 @@ use function array_map;
|
||||
use function array_push;
|
||||
use function array_search;
|
||||
use function array_values;
|
||||
use function assert;
|
||||
use function class_exists;
|
||||
use function class_parents;
|
||||
use function count;
|
||||
@@ -329,9 +328,8 @@ class SchemaValidator
|
||||
array_filter(
|
||||
array_map(
|
||||
function (FieldMapping $fieldMapping) use ($class): string|null {
|
||||
$fieldName = $fieldMapping->fieldName;
|
||||
assert(isset($class->reflFields[$fieldName]));
|
||||
$propertyType = $class->reflFields[$fieldName]->getType();
|
||||
$fieldName = $fieldMapping->fieldName;
|
||||
$propertyType = $class->propertyAccessors[$fieldName]->getUnderlyingReflector()->getType();
|
||||
|
||||
// If the field type is not a built-in type, we cannot check it
|
||||
if (! Type::hasType($fieldMapping->type)) {
|
||||
|
||||
@@ -586,7 +586,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
|
||||
$actualData = [];
|
||||
|
||||
foreach ($class->reflFields as $name => $refProp) {
|
||||
foreach ($class->propertyAccessors as $name => $refProp) {
|
||||
$value = $refProp->getValue($entity);
|
||||
|
||||
if ($class->isCollectionValuedAssociation($name) && $value !== null) {
|
||||
@@ -706,7 +706,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
|
||||
$newValue = clone $actualValue;
|
||||
$newValue->setOwner($entity, $assoc);
|
||||
$class->reflFields[$propName]->setValue($entity, $newValue);
|
||||
$class->propertyAccessors[$propName]->setValue($entity, $newValue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -745,7 +745,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
|
||||
// Look for changes in associations of the entity
|
||||
foreach ($class->associationMappings as $field => $assoc) {
|
||||
$val = $class->reflFields[$field]->getValue($entity);
|
||||
$val = $class->propertyAccessors[$field]->getValue($entity);
|
||||
if ($val === null) {
|
||||
continue;
|
||||
}
|
||||
@@ -981,7 +981,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
|
||||
$actualData = [];
|
||||
|
||||
foreach ($class->reflFields as $name => $refProp) {
|
||||
foreach ($class->propertyAccessors as $name => $refProp) {
|
||||
if (
|
||||
( ! $class->isIdentifier($name) || ! $class->isIdGeneratorIdentity())
|
||||
&& ($name !== $class->versionField)
|
||||
@@ -1167,7 +1167,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
// is obtained by a new entity because the old one went out of scope.
|
||||
//$this->entityStates[$oid] = self::STATE_NEW;
|
||||
if (! $class->isIdentifierNatural()) {
|
||||
$class->reflFields[$class->identifier[0]]->setValue($entity, null);
|
||||
$class->propertyAccessors[$class->identifier[0]]->setValue($entity, null);
|
||||
}
|
||||
|
||||
if ($invoke !== ListenersInvoker::INVOKE_NONE) {
|
||||
@@ -2029,7 +2029,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
);
|
||||
|
||||
foreach ($associationMappings as $assoc) {
|
||||
$relatedEntities = $class->reflFields[$assoc->fieldName]->getValue($entity);
|
||||
$relatedEntities = $class->propertyAccessors[$assoc->fieldName]->getValue($entity);
|
||||
|
||||
switch (true) {
|
||||
case $relatedEntities instanceof PersistentCollection:
|
||||
@@ -2070,7 +2070,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
);
|
||||
|
||||
foreach ($associationMappings as $assoc) {
|
||||
$relatedEntities = $class->reflFields[$assoc->fieldName]->getValue($entity);
|
||||
$relatedEntities = $class->propertyAccessors[$assoc->fieldName]->getValue($entity);
|
||||
|
||||
switch (true) {
|
||||
case $relatedEntities instanceof PersistentCollection:
|
||||
@@ -2116,7 +2116,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
);
|
||||
|
||||
foreach ($associationMappings as $assoc) {
|
||||
$relatedEntities = $class->reflFields[$assoc->fieldName]->getValue($entity);
|
||||
$relatedEntities = $class->propertyAccessors[$assoc->fieldName]->getValue($entity);
|
||||
|
||||
switch (true) {
|
||||
case $relatedEntities instanceof PersistentCollection:
|
||||
@@ -2179,7 +2179,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
$entitiesToCascade = [];
|
||||
|
||||
foreach ($associationMappings as $assoc) {
|
||||
$relatedEntities = $class->reflFields[$assoc->fieldName]->getValue($entity);
|
||||
$relatedEntities = $class->propertyAccessors[$assoc->fieldName]->getValue($entity);
|
||||
|
||||
switch (true) {
|
||||
case $relatedEntities instanceof Collection:
|
||||
@@ -2235,7 +2235,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
$this->initializeObject($entity);
|
||||
|
||||
assert($class->versionField !== null);
|
||||
$entityVersion = $class->reflFields[$class->versionField]->getValue($entity);
|
||||
$entityVersion = $class->propertyAccessors[$class->versionField]->getValue($entity);
|
||||
|
||||
// phpcs:ignore SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedNotEqualOperator
|
||||
if ($entityVersion != $lockVersion) {
|
||||
@@ -2379,7 +2379,11 @@ class UnitOfWork implements PropertyChangedListener
|
||||
}
|
||||
|
||||
if ($this->isUninitializedObject($entity)) {
|
||||
$entity->__setInitialized(true);
|
||||
if ($this->em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
$class->reflClass->markLazyObjectAsInitialized($entity);
|
||||
} else {
|
||||
$entity->__setInitialized(true);
|
||||
}
|
||||
|
||||
Hydrator::hydrate($entity, (array) $class->reflClass->newInstanceWithoutConstructor());
|
||||
} else {
|
||||
@@ -2404,7 +2408,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
if (isset($class->fieldMappings[$field])) {
|
||||
$class->reflFields[$field]->setValue($entity, $value);
|
||||
$class->propertyAccessors[$field]->setValue($entity, $value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2434,21 +2438,21 @@ class UnitOfWork implements PropertyChangedListener
|
||||
if (isset($data[$field]) && is_object($data[$field]) && isset($this->entityStates[spl_object_id($data[$field])])) {
|
||||
$this->originalEntityData[$oid][$field] = $data[$field];
|
||||
|
||||
$class->reflFields[$field]->setValue($entity, $data[$field]);
|
||||
$targetClass->reflFields[$assoc->mappedBy]->setValue($data[$field], $entity);
|
||||
$class->propertyAccessors[$field]->setValue($entity, $data[$field]);
|
||||
$targetClass->propertyAccessors[$assoc->mappedBy]->setValue($data[$field], $entity);
|
||||
|
||||
continue 2;
|
||||
}
|
||||
|
||||
// Inverse side of x-to-one can never be lazy
|
||||
$class->reflFields[$field]->setValue($entity, $this->getEntityPersister($assoc->targetEntity)->loadOneToOneEntity($assoc, $entity));
|
||||
$class->propertyAccessors[$field]->setValue($entity, $this->getEntityPersister($assoc->targetEntity)->loadOneToOneEntity($assoc, $entity));
|
||||
|
||||
continue 2;
|
||||
}
|
||||
|
||||
// use the entity association
|
||||
if (isset($data[$field]) && is_object($data[$field]) && isset($this->entityStates[spl_object_id($data[$field])])) {
|
||||
$class->reflFields[$field]->setValue($entity, $data[$field]);
|
||||
$class->propertyAccessors[$field]->setValue($entity, $data[$field]);
|
||||
$this->originalEntityData[$oid][$field] = $data[$field];
|
||||
|
||||
break;
|
||||
@@ -2480,7 +2484,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
|
||||
if (! $associatedId) {
|
||||
// Foreign key is NULL
|
||||
$class->reflFields[$field]->setValue($entity, null);
|
||||
$class->propertyAccessors[$field]->setValue($entity, null);
|
||||
$this->originalEntityData[$oid][$field] = null;
|
||||
|
||||
break;
|
||||
@@ -2546,11 +2550,11 @@ class UnitOfWork implements PropertyChangedListener
|
||||
}
|
||||
|
||||
$this->originalEntityData[$oid][$field] = $newValue;
|
||||
$class->reflFields[$field]->setValue($entity, $newValue);
|
||||
$class->propertyAccessors[$field]->setValue($entity, $newValue);
|
||||
|
||||
if ($assoc->inversedBy !== null && $assoc->isOneToOne() && $newValue !== null) {
|
||||
$inverseAssoc = $targetClass->associationMappings[$assoc->inversedBy];
|
||||
$targetClass->reflFields[$inverseAssoc->fieldName]->setValue($newValue, $entity);
|
||||
$targetClass->propertyAccessors[$inverseAssoc->fieldName]->setValue($newValue, $entity);
|
||||
}
|
||||
|
||||
break;
|
||||
@@ -2566,7 +2570,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
if (isset($data[$field]) && $data[$field] instanceof PersistentCollection) {
|
||||
$data[$field]->setOwner($entity, $assoc);
|
||||
|
||||
$class->reflFields[$field]->setValue($entity, $data[$field]);
|
||||
$class->propertyAccessors[$field]->setValue($entity, $data[$field]);
|
||||
$this->originalEntityData[$oid][$field] = $data[$field];
|
||||
|
||||
break;
|
||||
@@ -2577,7 +2581,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
$pColl->setOwner($entity, $assoc);
|
||||
$pColl->setInitialized(false);
|
||||
|
||||
$reflField = $class->reflFields[$field];
|
||||
$reflField = $class->propertyAccessors[$field];
|
||||
$reflField->setValue($entity, $pColl);
|
||||
|
||||
if ($hints['fetchMode'][$class->name][$field] === ClassMetadata::FETCH_EAGER) {
|
||||
@@ -2657,7 +2661,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
$found = $this->getEntityPersister($targetEntity)->loadAll([$mappedBy => $entities], $mapping->orderBy);
|
||||
|
||||
$targetClass = $this->em->getClassMetadata($targetEntity);
|
||||
$targetProperty = $targetClass->getReflectionProperty($mappedBy);
|
||||
$targetProperty = $targetClass->getPropertyAccessor($mappedBy);
|
||||
assert($targetProperty !== null);
|
||||
|
||||
foreach ($found as $targetValue) {
|
||||
@@ -2679,7 +2683,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
$idHash = implode(' ', $id);
|
||||
|
||||
if ($mapping->indexBy !== null) {
|
||||
$indexByProperty = $targetClass->getReflectionProperty($mapping->indexBy);
|
||||
$indexByProperty = $targetClass->getPropertyAccessor($mapping->indexBy);
|
||||
assert($indexByProperty !== null);
|
||||
$collectionBatch[$idHash]->hydrateSet($indexByProperty->getValue($targetValue), $targetValue);
|
||||
} else {
|
||||
@@ -3036,6 +3040,13 @@ class UnitOfWork implements PropertyChangedListener
|
||||
|
||||
if ($obj instanceof PersistentCollection) {
|
||||
$obj->initialize();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
$reflection = $this->em->getClassMetadata($obj::class)->getReflectionClass();
|
||||
$reflection->initializeLazyObject($obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3046,6 +3057,10 @@ class UnitOfWork implements PropertyChangedListener
|
||||
*/
|
||||
public function isUninitializedObject(mixed $obj): bool
|
||||
{
|
||||
if ($this->em->getConfiguration()->isNativeLazyObjectsEnabled() && ! ($obj instanceof Collection) && is_object($obj)) {
|
||||
return $this->em->getClassMetadata($obj::class)->reflClass->isUninitializedLazyObject($obj);
|
||||
}
|
||||
|
||||
return $obj instanceof InternalProxy && ! $obj->__isInitialized();
|
||||
}
|
||||
|
||||
@@ -3244,7 +3259,7 @@ class UnitOfWork implements PropertyChangedListener
|
||||
$idValue = $this->convertSingleFieldIdentifierToPHPValue($class, $generatedId);
|
||||
$oid = spl_object_id($entity);
|
||||
|
||||
$class->reflFields[$idField]->setValue($entity, $idValue);
|
||||
$class->propertyAccessors[$idField]->setValue($entity, $idValue);
|
||||
|
||||
$this->entityIdentifiers[$oid] = [$idField => $idValue];
|
||||
$this->entityStates[$oid] = self::STATE_MANAGED;
|
||||
|
||||
32
tests/Tests/Models/PropertyHooks/MappingVirtualProperty.php
Normal file
32
tests/Tests/Models/PropertyHooks/MappingVirtualProperty.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
// phpcs:ignoreFile
|
||||
namespace Doctrine\Tests\Models\PropertyHooks;
|
||||
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
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: 'property_hooks_user')]
|
||||
class MappingVirtualProperty
|
||||
{
|
||||
#[Id, GeneratedValue, Column(type: Types::INTEGER)]
|
||||
public ?int $id;
|
||||
|
||||
#[Column(type: Types::STRING)]
|
||||
public string $first;
|
||||
|
||||
#[Column(type: Types::STRING)]
|
||||
public string $last;
|
||||
|
||||
#[Column(type: Types::STRING)]
|
||||
public string $fullName {
|
||||
get => $this->first . " " . $this->last;
|
||||
set {
|
||||
[$this->first, $this->last] = explode(' ', $value, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
56
tests/Tests/Models/PropertyHooks/User.php
Normal file
56
tests/Tests/Models/PropertyHooks/User.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
// phpcs:ignoreFile
|
||||
namespace Doctrine\Tests\Models\PropertyHooks;
|
||||
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
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: 'property_hooks_user')]
|
||||
class User
|
||||
{
|
||||
#[Id, GeneratedValue, Column(type: Types::INTEGER)]
|
||||
public ?int $id;
|
||||
|
||||
#[Column(type: Types::STRING)]
|
||||
public string $first {
|
||||
set {
|
||||
if (strlen($value) === 0) {
|
||||
throw new ValueError("Name must be non-empty");
|
||||
}
|
||||
$this->first = $value;
|
||||
}
|
||||
}
|
||||
|
||||
#[Column(type: Types::STRING)]
|
||||
public string $last {
|
||||
set {
|
||||
if (strlen($value) === 0) {
|
||||
throw new ValueError("Name must be non-empty");
|
||||
}
|
||||
$this->last = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public string $fullName {
|
||||
get => $this->first . " " . $this->last;
|
||||
set {
|
||||
[$this->first, $this->last] = explode(' ', $value, 2);
|
||||
}
|
||||
}
|
||||
|
||||
#[Column(type: Types::STRING)]
|
||||
public string $language = 'de' {
|
||||
// Override the "read" action with arbitrary logic.
|
||||
get => strtoupper($this->language);
|
||||
|
||||
// Override the "write" action with arbitrary logic.
|
||||
set {
|
||||
$this->language = strtolower($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,9 +25,10 @@ use Generator;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use PHPUnit\Framework\Attributes\RequiresPhp;
|
||||
use ReflectionClass;
|
||||
use ReflectionProperty;
|
||||
use stdClass;
|
||||
use Symfony\Component\VarExporter\LazyGhostTrait;
|
||||
use TypeError;
|
||||
|
||||
class EntityManagerTest extends OrmTestCase
|
||||
@@ -180,17 +181,12 @@ class EntityManagerTest extends OrmTestCase
|
||||
}
|
||||
|
||||
/** Resetting the EntityManager relies on lazy objects until https://github.com/doctrine/orm/issues/5933 is resolved */
|
||||
#[RequiresPhp('8.4')]
|
||||
public function testLazyGhostEntityManager(): void
|
||||
{
|
||||
$em = new class () extends EntityManager {
|
||||
use LazyGhostTrait;
|
||||
$reflector = new ReflectionClass(EntityManager::class);
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
$em = $em::createLazyGhost(static function ($em): void {
|
||||
$em = $reflector->newLazyGhost($initializer = static function (EntityManager $em): void {
|
||||
$r = new ReflectionProperty(EntityManager::class, 'unitOfWork');
|
||||
$r->setValue($em, new class () extends UnitOfWork {
|
||||
public function __construct()
|
||||
@@ -207,7 +203,7 @@ class EntityManagerTest extends OrmTestCase
|
||||
$em->close();
|
||||
$this->assertFalse($em->isOpen());
|
||||
|
||||
$em->resetLazyObject();
|
||||
$reflector->resetAsLazyGhost($em, $initializer);
|
||||
$this->assertTrue($em->isOpen());
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ use Doctrine\ORM\Exception\EntityIdentityCollisionException;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\ORMInvalidArgumentException;
|
||||
use Doctrine\ORM\PersistentCollection;
|
||||
use Doctrine\ORM\Proxy\InternalProxy;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\UnitOfWork;
|
||||
use Doctrine\Tests\IterableTester;
|
||||
@@ -557,7 +556,7 @@ class BasicFunctionalTest extends OrmFunctionalTestCase
|
||||
$this->_em->persist($article);
|
||||
$this->_em->flush();
|
||||
|
||||
self::assertFalse($userRef->__isInitialized());
|
||||
self::assertTrue($this->isUninitializedObject($userRef));
|
||||
|
||||
$this->_em->clear();
|
||||
|
||||
@@ -592,7 +591,7 @@ class BasicFunctionalTest extends OrmFunctionalTestCase
|
||||
$this->_em->persist($user);
|
||||
$this->_em->flush();
|
||||
|
||||
self::assertFalse($groupRef->__isInitialized());
|
||||
self::assertTrue($this->isUninitializedObject($groupRef));
|
||||
|
||||
$this->_em->clear();
|
||||
|
||||
@@ -940,8 +939,7 @@ class BasicFunctionalTest extends OrmFunctionalTestCase
|
||||
->setParameter(1, $article->id)
|
||||
->setFetchMode(CmsArticle::class, 'user', ClassMetadata::FETCH_EAGER)
|
||||
->getSingleResult();
|
||||
self::assertInstanceOf(InternalProxy::class, $article->user, 'It IS a proxy, ...');
|
||||
self::assertFalse($this->isUninitializedObject($article->user), '...but its initialized!');
|
||||
self::assertFalse($this->isUninitializedObject($article->user));
|
||||
$this->assertQueryCount(2);
|
||||
}
|
||||
|
||||
|
||||
@@ -470,7 +470,7 @@ EXCEPTION
|
||||
public function testItAllowsReadingAttributes(): void
|
||||
{
|
||||
$metadata = $this->_em->getClassMetadata(Card::class);
|
||||
$property = $metadata->getReflectionProperty('suit');
|
||||
$property = $metadata->propertyAccessors['suit']->getUnderlyingReflector();
|
||||
|
||||
$attributes = $property->getAttributes();
|
||||
|
||||
|
||||
133
tests/Tests/ORM/Functional/InvalidMappingDefinitionTest.php
Normal file
133
tests/Tests/ORM/Functional/InvalidMappingDefinitionTest.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Functional;
|
||||
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\MappingException;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
use Doctrine\Tests\OrmFunctionalTestCase;
|
||||
|
||||
/**
|
||||
* Functional tests for the Class Table Inheritance mapping strategy.
|
||||
*/
|
||||
class InvalidMappingDefinitionTest extends OrmFunctionalTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
public function testManyToManyRelationWithJoinTableOnTheWrongSide(): void
|
||||
{
|
||||
$this->expectException(MappingException::class);
|
||||
$this->expectExceptionMessage("Mapping error on field 'owners' in Doctrine\Tests\ORM\Functional\OwnedSideEntity : 'joinTable' can only be set on many-to-many owning side.");
|
||||
|
||||
$this->createSchemaForModels(
|
||||
OwningSideEntity::class,
|
||||
OwnedSideEntity::class,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[Table(name: 'owning_side_entities1')]
|
||||
#[Entity]
|
||||
class OwningSideEntity
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: OwnedSideEntity::class, inversedBy: 'owners')]
|
||||
#[ORM\JoinTable(name: 'owning_owned')]
|
||||
private Collection $relations;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->relations = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getRelations(): Collection
|
||||
{
|
||||
return $this->relations;
|
||||
}
|
||||
|
||||
public function addRelation(OwnedSideEntity $ownedSide): void
|
||||
{
|
||||
if (! $this->relations->contains($ownedSide)) {
|
||||
$this->relations->add($ownedSide);
|
||||
$ownedSide->addOwner($this);
|
||||
}
|
||||
}
|
||||
|
||||
public function removeRelation(OwnedSideEntity $ownedSide): void
|
||||
{
|
||||
if ($this->relations->removeElement($ownedSide)) {
|
||||
$ownedSide->removeOwner($this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[Table(name: 'owned_side_entities1')]
|
||||
#[Entity]
|
||||
class OwnedSideEntity
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type: 'string', length: 255)]
|
||||
private string $data;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: OwningSideEntity::class, mappedBy: 'relations')]
|
||||
#[ORM\JoinTable(name: 'owning_owned')]
|
||||
private Collection $owners;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->owners = new ArrayCollection();
|
||||
}
|
||||
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getData(): string
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
public function setData(string $data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function getOwners(): Collection
|
||||
{
|
||||
return $this->owners;
|
||||
}
|
||||
|
||||
public function addOwner(OwningSideEntity $owningSide): void
|
||||
{
|
||||
if (! $this->owners->contains($owningSide)) {
|
||||
$this->owners->add($owningSide);
|
||||
}
|
||||
}
|
||||
|
||||
public function removeOwner(OwningSideEntity $owningSide): void
|
||||
{
|
||||
$this->owners->removeElement($owningSide);
|
||||
}
|
||||
}
|
||||
@@ -134,7 +134,7 @@ class LifecycleCallbackTest extends OrmFunctionalTestCase
|
||||
LifecycleCallbackTestEntity::$postLoadCallbackInvoked = false; // Reset the tracking of the postLoad invocation
|
||||
|
||||
$reference = $this->_em->getReference(LifecycleCallbackTestEntity::class, $id);
|
||||
self::assertFalse($reference::$postLoadCallbackInvoked);
|
||||
self::assertArrayNotHasKey('postLoadCallbackInvoked', (array) $reference);
|
||||
$this->assertTrue($this->isUninitializedObject($reference));
|
||||
|
||||
$reference->getValue(); // trigger proxy load
|
||||
|
||||
@@ -1230,6 +1230,170 @@ class NewOperatorTest extends OrmFunctionalTestCase
|
||||
self::assertSame($this->fixtures[2]->address->zip, $result[2]->val2->zip);
|
||||
}
|
||||
|
||||
public function testEntityInDtoWithRoot(): void
|
||||
{
|
||||
$dql = '
|
||||
SELECT
|
||||
new CmsDumbDTO(
|
||||
u.id,
|
||||
u,
|
||||
a,
|
||||
e.email
|
||||
)
|
||||
FROM
|
||||
Doctrine\Tests\Models\CMS\CmsUser u
|
||||
LEFT JOIN
|
||||
u.email e
|
||||
LEFT JOIN
|
||||
u.address a
|
||||
ORDER BY
|
||||
u.name';
|
||||
|
||||
$query = $this->getEntityManager()->createQuery($dql);
|
||||
$result = $query->getResult();
|
||||
|
||||
self::assertCount(3, $result);
|
||||
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[0]);
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[1]);
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[2]);
|
||||
|
||||
self::assertInstanceOf(CmsUser::class, $result[0]->val2);
|
||||
self::assertInstanceOf(CmsUser::class, $result[1]->val2);
|
||||
self::assertInstanceOf(CmsUser::class, $result[2]->val2);
|
||||
|
||||
self::assertSame($this->fixtures[0]->name, $result[0]->val2->name);
|
||||
self::assertSame($this->fixtures[1]->name, $result[1]->val2->name);
|
||||
self::assertSame($this->fixtures[2]->name, $result[2]->val2->name);
|
||||
|
||||
self::assertSame($this->fixtures[0]->username, $result[0]->val2->username);
|
||||
self::assertSame($this->fixtures[1]->username, $result[1]->val2->username);
|
||||
self::assertSame($this->fixtures[2]->username, $result[2]->val2->username);
|
||||
|
||||
self::assertSame($this->fixtures[0]->status, $result[0]->val2->status);
|
||||
self::assertSame($this->fixtures[1]->status, $result[1]->val2->status);
|
||||
self::assertSame($this->fixtures[2]->status, $result[2]->val2->status);
|
||||
|
||||
self::assertInstanceOf(CmsAddress::class, $result[0]->val3);
|
||||
self::assertInstanceOf(CmsAddress::class, $result[1]->val3);
|
||||
self::assertInstanceOf(CmsAddress::class, $result[2]->val3);
|
||||
|
||||
self::assertSame($this->fixtures[0]->address->city, $result[0]->val3->city);
|
||||
self::assertSame($this->fixtures[1]->address->city, $result[1]->val3->city);
|
||||
self::assertSame($this->fixtures[2]->address->city, $result[2]->val3->city);
|
||||
|
||||
self::assertSame($this->fixtures[0]->address->country, $result[0]->val3->country);
|
||||
self::assertSame($this->fixtures[1]->address->country, $result[1]->val3->country);
|
||||
self::assertSame($this->fixtures[2]->address->country, $result[2]->val3->country);
|
||||
|
||||
self::assertSame($this->fixtures[0]->email->email, $result[0]->val4);
|
||||
self::assertSame($this->fixtures[1]->email->email, $result[1]->val4);
|
||||
self::assertSame($this->fixtures[2]->email->email, $result[2]->val4);
|
||||
}
|
||||
|
||||
public function testEntityInDtoWithoutRoot(): void
|
||||
{
|
||||
$dql = '
|
||||
SELECT
|
||||
new CmsDumbDTO(
|
||||
u.id,
|
||||
u.name,
|
||||
a,
|
||||
e.email
|
||||
)
|
||||
FROM
|
||||
Doctrine\Tests\Models\CMS\CmsUser u
|
||||
LEFT JOIN
|
||||
u.email e
|
||||
LEFT JOIN
|
||||
u.address a
|
||||
ORDER BY
|
||||
u.name';
|
||||
|
||||
$query = $this->getEntityManager()->createQuery($dql);
|
||||
$result = $query->getResult();
|
||||
|
||||
self::assertCount(3, $result);
|
||||
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[0]);
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[1]);
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[2]);
|
||||
|
||||
self::assertSame($this->fixtures[0]->name, $result[0]->val2);
|
||||
self::assertSame($this->fixtures[1]->name, $result[1]->val2);
|
||||
self::assertSame($this->fixtures[2]->name, $result[2]->val2);
|
||||
|
||||
self::assertInstanceOf(CmsAddress::class, $result[0]->val3);
|
||||
self::assertInstanceOf(CmsAddress::class, $result[1]->val3);
|
||||
self::assertInstanceOf(CmsAddress::class, $result[2]->val3);
|
||||
|
||||
self::assertSame($this->fixtures[0]->address->city, $result[0]->val3->city);
|
||||
self::assertSame($this->fixtures[1]->address->city, $result[1]->val3->city);
|
||||
self::assertSame($this->fixtures[2]->address->city, $result[2]->val3->city);
|
||||
|
||||
self::assertSame($this->fixtures[0]->address->country, $result[0]->val3->country);
|
||||
self::assertSame($this->fixtures[1]->address->country, $result[1]->val3->country);
|
||||
self::assertSame($this->fixtures[2]->address->country, $result[2]->val3->country);
|
||||
|
||||
self::assertSame($this->fixtures[0]->email->email, $result[0]->val4);
|
||||
self::assertSame($this->fixtures[1]->email->email, $result[1]->val4);
|
||||
self::assertSame($this->fixtures[2]->email->email, $result[2]->val4);
|
||||
}
|
||||
|
||||
public function testOnlyObjectInDto(): void
|
||||
{
|
||||
$dql = '
|
||||
SELECT
|
||||
new CmsDumbDTO(
|
||||
a,
|
||||
new CmsDumbDTO(
|
||||
u.name,
|
||||
e.email
|
||||
)
|
||||
)
|
||||
FROM
|
||||
Doctrine\Tests\Models\CMS\CmsUser u
|
||||
LEFT JOIN
|
||||
u.email e
|
||||
LEFT JOIN
|
||||
u.address a
|
||||
ORDER BY
|
||||
u.name';
|
||||
|
||||
$query = $this->getEntityManager()->createQuery($dql);
|
||||
$result = $query->getResult();
|
||||
|
||||
self::assertCount(3, $result);
|
||||
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[0]);
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[1]);
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[2]);
|
||||
|
||||
self::assertInstanceOf(CmsAddress::class, $result[0]->val1);
|
||||
self::assertInstanceOf(CmsAddress::class, $result[1]->val1);
|
||||
self::assertInstanceOf(CmsAddress::class, $result[2]->val1);
|
||||
|
||||
self::assertSame($this->fixtures[0]->address->city, $result[0]->val1->city);
|
||||
self::assertSame($this->fixtures[1]->address->city, $result[1]->val1->city);
|
||||
self::assertSame($this->fixtures[2]->address->city, $result[2]->val1->city);
|
||||
|
||||
self::assertSame($this->fixtures[0]->address->country, $result[0]->val1->country);
|
||||
self::assertSame($this->fixtures[1]->address->country, $result[1]->val1->country);
|
||||
self::assertSame($this->fixtures[2]->address->country, $result[2]->val1->country);
|
||||
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[0]->val2);
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[1]->val2);
|
||||
self::assertInstanceOf(CmsDumbDTO::class, $result[2]->val2);
|
||||
|
||||
self::assertSame($this->fixtures[0]->name, $result[0]->val2->val1);
|
||||
self::assertSame($this->fixtures[1]->name, $result[1]->val2->val1);
|
||||
self::assertSame($this->fixtures[2]->name, $result[2]->val2->val1);
|
||||
|
||||
self::assertSame($this->fixtures[0]->email->email, $result[0]->val2->val2);
|
||||
self::assertSame($this->fixtures[1]->email->email, $result[1]->val2->val2);
|
||||
self::assertSame($this->fixtures[2]->email->email, $result[2]->val2->val2);
|
||||
}
|
||||
|
||||
public function testNamedArguments(): void
|
||||
{
|
||||
$dql = <<<'SQL'
|
||||
|
||||
103
tests/Tests/ORM/Functional/PropertyHooksTest.php
Normal file
103
tests/Tests/ORM/Functional/PropertyHooksTest.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Functional;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
|
||||
use Doctrine\ORM\Mapping\MappingException;
|
||||
use Doctrine\Tests\Models\PropertyHooks\MappingVirtualProperty;
|
||||
use Doctrine\Tests\Models\PropertyHooks\User;
|
||||
use Doctrine\Tests\OrmFunctionalTestCase;
|
||||
use PHPUnit\Framework\Attributes\RequiresPhp;
|
||||
|
||||
#[RequiresPhp('>= 8.4.0')]
|
||||
class PropertyHooksTest extends OrmFunctionalTestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
if ($this->_em->getConnection()->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
|
||||
self::markTestSkipped('MySQL/MariaDB is case-insensitive by default, and the logic of this test relies on case sensitivity.');
|
||||
}
|
||||
|
||||
if (! $this->_em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
$this->markTestSkipped('Property hooks require native lazy objects to be enabled.');
|
||||
}
|
||||
|
||||
$this->createSchemaForModels(
|
||||
User::class,
|
||||
);
|
||||
}
|
||||
|
||||
public function testMapPropertyHooks(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->fullName = 'John Doe';
|
||||
$user->language = 'EN';
|
||||
|
||||
$this->_em->persist($user);
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$user = $this->_em->find(User::class, $user->id);
|
||||
|
||||
self::assertEquals('John', $user->first);
|
||||
self::assertEquals('Doe', $user->last);
|
||||
self::assertEquals('John Doe', $user->fullName);
|
||||
self::assertEquals('EN', $user->language, 'The property hook uppercases the language.');
|
||||
|
||||
$language = $this->_em->createQuery('SELECT u.language FROM ' . User::class . ' u WHERE u.id = :id')
|
||||
->setParameter('id', $user->id)
|
||||
->getSingleScalarResult();
|
||||
|
||||
$this->assertEquals('en', $language, 'Selecting a field from DQL does not go through the property hook, accessing raw data.');
|
||||
|
||||
$this->_em->clear();
|
||||
|
||||
$user = $this->_em->getRepository(User::class)->findOneBy(['language' => 'EN']);
|
||||
|
||||
self::assertNull($user);
|
||||
|
||||
$user = $this->_em->getRepository(User::class)->findOneBy(['language' => 'en']);
|
||||
|
||||
self::assertNotNull($user);
|
||||
}
|
||||
|
||||
public function testTriggerLazyLoadingWhenAccessingPropertyHooks(): void
|
||||
{
|
||||
$user = new User();
|
||||
$user->fullName = 'Ludwig von Beethoven';
|
||||
$user->language = 'DE';
|
||||
|
||||
$this->_em->persist($user);
|
||||
$this->_em->flush();
|
||||
$this->_em->clear();
|
||||
|
||||
$user = $this->_em->getReference(User::class, $user->id);
|
||||
|
||||
$this->assertTrue($this->_em->getUnitOfWork()->isUninitializedObject($user));
|
||||
|
||||
self::assertEquals('Ludwig', $user->first);
|
||||
self::assertEquals('von Beethoven', $user->last);
|
||||
self::assertEquals('Ludwig von Beethoven', $user->fullName);
|
||||
self::assertEquals('DE', $user->language, 'The property hook uppercases the language.');
|
||||
|
||||
$this->assertFalse($this->_em->getUnitOfWork()->isUninitializedObject($user));
|
||||
|
||||
$this->_em->clear();
|
||||
|
||||
$user = $this->_em->getReference(User::class, $user->id);
|
||||
|
||||
self::assertEquals('Ludwig von Beethoven', $user->fullName);
|
||||
}
|
||||
|
||||
public function testMappingVirtualPropertyIsNotSupported(): void
|
||||
{
|
||||
$this->expectException(MappingException::class);
|
||||
$this->expectExceptionMessage('Mapping virtual property "fullName" on entity "Doctrine\Tests\Models\PropertyHooks\MappingVirtualProperty" is not allowed.');
|
||||
|
||||
$this->_em->getClassMetadata(MappingVirtualProperty::class);
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,10 @@ class ProxiesLikeEntitiesTest extends OrmFunctionalTestCase
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
if ($this->_em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
self::markTestSkipped('This test is not applicable when lazy proxy is enabled.');
|
||||
}
|
||||
|
||||
$this->createSchemaForModels(
|
||||
CmsUser::class,
|
||||
CmsTag::class,
|
||||
|
||||
@@ -6,7 +6,6 @@ namespace Doctrine\Tests\ORM\Functional;
|
||||
|
||||
use Doctrine\Common\Proxy\Proxy as CommonProxy;
|
||||
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
|
||||
use Doctrine\ORM\Proxy\InternalProxy;
|
||||
use Doctrine\Tests\Models\Company\CompanyAuction;
|
||||
use Doctrine\Tests\Models\ECommerce\ECommerceProduct;
|
||||
use Doctrine\Tests\Models\ECommerce\ECommerceProduct2;
|
||||
@@ -242,13 +241,16 @@ class ReferenceProxyTest extends OrmFunctionalTestCase
|
||||
#[Group('DDC-1604')]
|
||||
public function testCommonPersistenceProxy(): void
|
||||
{
|
||||
if ($this->_em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
self::markTestSkipped('Test only works with proxy generation disabled.');
|
||||
}
|
||||
|
||||
$id = $this->createProduct();
|
||||
|
||||
$entity = $this->_em->getReference(ECommerceProduct::class, $id);
|
||||
assert($entity instanceof ECommerceProduct);
|
||||
$className = DefaultProxyClassNameResolver::getClass($entity);
|
||||
|
||||
self::assertInstanceOf(InternalProxy::class, $entity);
|
||||
self::assertTrue($this->isUninitializedObject($entity));
|
||||
self::assertEquals(ECommerceProduct::class, $className);
|
||||
|
||||
@@ -257,7 +259,7 @@ class ReferenceProxyTest extends OrmFunctionalTestCase
|
||||
$proxyFileName = $this->_em->getConfiguration()->getProxyDir() . DIRECTORY_SEPARATOR . str_replace('\\', '', $restName) . '.php';
|
||||
self::assertTrue(file_exists($proxyFileName), 'Proxy file name cannot be found generically.');
|
||||
|
||||
$entity->__load();
|
||||
$this->initializeObject($entity);
|
||||
self::assertFalse($this->isUninitializedObject($entity));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ use Doctrine\Tests\OrmFunctionalTestCase;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
|
||||
use function array_filter;
|
||||
use function array_values;
|
||||
use function implode;
|
||||
use function str_starts_with;
|
||||
|
||||
@@ -42,6 +43,20 @@ class PostgreSqlSchemaToolTest extends OrmFunctionalTestCase
|
||||
|
||||
self::assertCount(0, $sql, implode("\n", $sql));
|
||||
}
|
||||
|
||||
public function testSetDeferrableForeignKey(): void
|
||||
{
|
||||
$schema = $this->getSchemaForModels(
|
||||
EntityWithSelfReferencingAssociation::class,
|
||||
);
|
||||
|
||||
$table = $schema->getTable('entitywithselfreferencingassociation');
|
||||
$fks = array_values($table->getForeignKeys());
|
||||
|
||||
self::assertCount(1, $fks);
|
||||
|
||||
self::assertTrue($fks[0]->getOption('deferrable'));
|
||||
}
|
||||
}
|
||||
|
||||
#[Table(name: 'stonewood.screen')]
|
||||
@@ -98,3 +113,20 @@ class DDC1657Avatar
|
||||
#[Column(name: 'pk', type: 'integer', nullable: false)]
|
||||
private int $pk;
|
||||
}
|
||||
|
||||
#[Table(name: 'entitywithselfreferencingassociation')]
|
||||
#[Entity]
|
||||
class EntityWithSelfReferencingAssociation
|
||||
{
|
||||
/**
|
||||
* Identifier
|
||||
*/
|
||||
#[Id]
|
||||
#[GeneratedValue(strategy: 'IDENTITY')]
|
||||
#[Column(type: 'integer', nullable: false)]
|
||||
private int $id;
|
||||
|
||||
#[ManyToOne(targetEntity: self::class)]
|
||||
#[JoinColumn(deferrable: true)]
|
||||
private self $parent;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ use Doctrine\ORM\Cache\EntityCacheKey;
|
||||
use Doctrine\ORM\Cache\Exception\CacheException;
|
||||
use Doctrine\ORM\Cache\QueryCacheEntry;
|
||||
use Doctrine\ORM\Cache\QueryCacheKey;
|
||||
use Doctrine\ORM\Proxy\InternalProxy;
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\Query\ResultSetMapping;
|
||||
use Doctrine\Tests\Models\Cache\Attraction;
|
||||
@@ -939,7 +938,6 @@ class SecondLevelCacheQueryCacheTest extends SecondLevelCacheFunctionalTestCase
|
||||
self::assertNotNull($state1->getCountry());
|
||||
$this->assertQueryCount(1);
|
||||
self::assertInstanceOf(State::class, $state1);
|
||||
self::assertInstanceOf(InternalProxy::class, $state1->getCountry());
|
||||
self::assertEquals($countryName, $state1->getCountry()->getName());
|
||||
self::assertEquals($stateId, $state1->getId());
|
||||
|
||||
@@ -957,7 +955,6 @@ class SecondLevelCacheQueryCacheTest extends SecondLevelCacheFunctionalTestCase
|
||||
self::assertNotNull($state2->getCountry());
|
||||
$this->assertQueryCount(0);
|
||||
self::assertInstanceOf(State::class, $state2);
|
||||
self::assertInstanceOf(InternalProxy::class, $state2->getCountry());
|
||||
self::assertEquals($countryName, $state2->getCountry()->getName());
|
||||
self::assertEquals($stateId, $state2->getId());
|
||||
}
|
||||
@@ -1031,7 +1028,6 @@ class SecondLevelCacheQueryCacheTest extends SecondLevelCacheFunctionalTestCase
|
||||
|
||||
$this->assertQueryCount(1);
|
||||
self::assertInstanceOf(State::class, $state1);
|
||||
self::assertInstanceOf(InternalProxy::class, $state1->getCountry());
|
||||
self::assertInstanceOf(City::class, $state1->getCities()->get(0));
|
||||
self::assertInstanceOf(State::class, $state1->getCities()->get(0)->getState());
|
||||
self::assertSame($state1, $state1->getCities()->get(0)->getState());
|
||||
@@ -1048,7 +1044,6 @@ class SecondLevelCacheQueryCacheTest extends SecondLevelCacheFunctionalTestCase
|
||||
|
||||
$this->assertQueryCount(0);
|
||||
self::assertInstanceOf(State::class, $state2);
|
||||
self::assertInstanceOf(InternalProxy::class, $state2->getCountry());
|
||||
self::assertInstanceOf(City::class, $state2->getCities()->get(0));
|
||||
self::assertInstanceOf(State::class, $state2->getCities()->get(0)->getState());
|
||||
self::assertSame($state2, $state2->getCities()->get(0)->getState());
|
||||
|
||||
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Functional;
|
||||
|
||||
use Doctrine\ORM\Proxy\InternalProxy;
|
||||
use Doctrine\Tests\Models\Cache\Country;
|
||||
use Doctrine\Tests\Models\Cache\State;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
@@ -198,8 +197,6 @@ class SecondLevelCacheRepositoryTest extends SecondLevelCacheFunctionalTestCase
|
||||
self::assertInstanceOf(State::class, $entities[1]);
|
||||
self::assertInstanceOf(Country::class, $entities[0]->getCountry());
|
||||
self::assertInstanceOf(Country::class, $entities[0]->getCountry());
|
||||
self::assertInstanceOf(InternalProxy::class, $entities[0]->getCountry());
|
||||
self::assertInstanceOf(InternalProxy::class, $entities[1]->getCountry());
|
||||
|
||||
// load from cache
|
||||
$this->getQueryLog()->reset()->enable();
|
||||
@@ -212,8 +209,6 @@ class SecondLevelCacheRepositoryTest extends SecondLevelCacheFunctionalTestCase
|
||||
self::assertInstanceOf(State::class, $entities[1]);
|
||||
self::assertInstanceOf(Country::class, $entities[0]->getCountry());
|
||||
self::assertInstanceOf(Country::class, $entities[1]->getCountry());
|
||||
self::assertInstanceOf(InternalProxy::class, $entities[0]->getCountry());
|
||||
self::assertInstanceOf(InternalProxy::class, $entities[1]->getCountry());
|
||||
|
||||
// invalidate cache
|
||||
$this->_em->persist(new State('foo', $this->_em->find(Country::class, $this->countries[0]->getId())));
|
||||
@@ -231,8 +226,6 @@ class SecondLevelCacheRepositoryTest extends SecondLevelCacheFunctionalTestCase
|
||||
self::assertInstanceOf(State::class, $entities[1]);
|
||||
self::assertInstanceOf(Country::class, $entities[0]->getCountry());
|
||||
self::assertInstanceOf(Country::class, $entities[1]->getCountry());
|
||||
self::assertInstanceOf(InternalProxy::class, $entities[0]->getCountry());
|
||||
self::assertInstanceOf(InternalProxy::class, $entities[1]->getCountry());
|
||||
|
||||
// load from cache
|
||||
$this->getQueryLog()->reset()->enable();
|
||||
@@ -245,7 +238,5 @@ class SecondLevelCacheRepositoryTest extends SecondLevelCacheFunctionalTestCase
|
||||
self::assertInstanceOf(State::class, $entities[1]);
|
||||
self::assertInstanceOf(Country::class, $entities[0]->getCountry());
|
||||
self::assertInstanceOf(Country::class, $entities[1]->getCountry());
|
||||
self::assertInstanceOf(InternalProxy::class, $entities[0]->getCountry());
|
||||
self::assertInstanceOf(InternalProxy::class, $entities[1]->getCountry());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,11 +57,11 @@ class DDC1238Test extends OrmFunctionalTestCase
|
||||
|
||||
$user2 = $this->_em->getReference(DDC1238User::class, $userId);
|
||||
|
||||
//$user->__load();
|
||||
//$this->initializeObject($user);
|
||||
|
||||
self::assertIsInt($user->getId(), 'Even if a proxy is detached, it should still have an identifier');
|
||||
|
||||
$user2->__load();
|
||||
$this->initializeObject($user2);
|
||||
|
||||
self::assertIsInt($user2->getId(), 'The managed instance still has an identifier');
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ class DDC168Test extends OrmFunctionalTestCase
|
||||
$this->oldMetadata = $this->_em->getClassMetadata(CompanyEmployee::class);
|
||||
|
||||
$metadata = clone $this->oldMetadata;
|
||||
ksort($metadata->reflFields);
|
||||
ksort($metadata->propertyAccessors);
|
||||
$this->_em->getMetadataFactory()->setMetadataFor(CompanyEmployee::class, $metadata);
|
||||
}
|
||||
|
||||
|
||||
@@ -32,6 +32,10 @@ class GH10808Test extends OrmFunctionalTestCase
|
||||
|
||||
public function testDQLDeferredEagerLoad(): void
|
||||
{
|
||||
if ($this->_em->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
self::markTestSkipped('Test requires lazy loading to be disabled');
|
||||
}
|
||||
|
||||
$appointment = new GH10808Appointment();
|
||||
|
||||
$this->_em->persist($appointment);
|
||||
|
||||
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||
namespace Doctrine\Tests\ORM\Functional;
|
||||
|
||||
use DateTime;
|
||||
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty as CommonRuntimePublicReflectionProperty;
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\DiscriminatorColumn;
|
||||
use Doctrine\ORM\Mapping\DiscriminatorMap;
|
||||
@@ -17,15 +16,11 @@ use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\InheritanceType;
|
||||
use Doctrine\ORM\Mapping\MappedSuperclass;
|
||||
use Doctrine\ORM\Mapping\MappingException;
|
||||
use Doctrine\ORM\Mapping\ReflectionEmbeddedProperty;
|
||||
use Doctrine\ORM\Query\QueryException;
|
||||
use Doctrine\Persistence\Reflection\RuntimeReflectionProperty;
|
||||
use Doctrine\Tests\OrmFunctionalTestCase;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use ReflectionProperty;
|
||||
|
||||
use function class_exists;
|
||||
use function sprintf;
|
||||
|
||||
#[Group('DDC-93')]
|
||||
@@ -45,30 +40,6 @@ class ValueObjectsTest extends OrmFunctionalTestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function testMetadataHasReflectionEmbeddablesAccessible(): void
|
||||
{
|
||||
$classMetadata = $this->_em->getClassMetadata(DDC93Person::class);
|
||||
|
||||
if (class_exists(CommonRuntimePublicReflectionProperty::class)) {
|
||||
self::assertInstanceOf(
|
||||
CommonRuntimePublicReflectionProperty::class,
|
||||
$classMetadata->getReflectionProperty('address'),
|
||||
);
|
||||
} elseif (class_exists(RuntimeReflectionProperty::class)) {
|
||||
self::assertInstanceOf(
|
||||
RuntimeReflectionProperty::class,
|
||||
$classMetadata->getReflectionProperty('address'),
|
||||
);
|
||||
} else {
|
||||
self::assertInstanceOf(
|
||||
ReflectionProperty::class,
|
||||
$classMetadata->getReflectionProperty('address'),
|
||||
);
|
||||
}
|
||||
|
||||
self::assertInstanceOf(ReflectionEmbeddedProperty::class, $classMetadata->getReflectionProperty('address.street'));
|
||||
}
|
||||
|
||||
public function testCRUD(): void
|
||||
{
|
||||
$person = new DDC93Person();
|
||||
|
||||
@@ -9,7 +9,6 @@ use Doctrine\ORM\Internal\Hydration\HydrationException;
|
||||
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\PersistentCollection;
|
||||
use Doctrine\ORM\Proxy\InternalProxy;
|
||||
use Doctrine\ORM\Proxy\ProxyFactory;
|
||||
use Doctrine\ORM\Query\ResultSetMapping;
|
||||
use Doctrine\Tests\Mocks\ArrayResultFactory;
|
||||
@@ -1030,7 +1029,7 @@ class ObjectHydratorTest extends HydrationTestCase
|
||||
'Proxies',
|
||||
ProxyFactory::AUTOGENERATE_ALWAYS,
|
||||
) extends ProxyFactory {
|
||||
public function getProxy(string $className, array $identifier): InternalProxy
|
||||
public function getProxy(string $className, array $identifier): object
|
||||
{
|
||||
TestCase::assertSame(ECommerceShipping::class, $className);
|
||||
TestCase::assertSame(['id' => 42], $identifier);
|
||||
@@ -1084,7 +1083,7 @@ class ObjectHydratorTest extends HydrationTestCase
|
||||
'Proxies',
|
||||
ProxyFactory::AUTOGENERATE_ALWAYS,
|
||||
) extends ProxyFactory {
|
||||
public function getProxy(string $className, array $identifier): InternalProxy
|
||||
public function getProxy(string $className, array $identifier): object
|
||||
{
|
||||
TestCase::assertSame(ECommerceShipping::class, $className);
|
||||
TestCase::assertSame(['id' => 42], $identifier);
|
||||
|
||||
@@ -119,9 +119,9 @@ class BasicInheritanceMappingTest extends OrmTestCase
|
||||
$class2 = unserialize(serialize($class));
|
||||
$class2->wakeupReflection(new RuntimeReflectionService());
|
||||
|
||||
self::assertArrayHasKey('mapped1', $class2->reflFields);
|
||||
self::assertArrayHasKey('mapped2', $class2->reflFields);
|
||||
self::assertArrayHasKey('mappedRelated1', $class2->reflFields);
|
||||
self::assertArrayHasKey('mapped1', $class2->propertyAccessors);
|
||||
self::assertArrayHasKey('mapped2', $class2->propertyAccessors);
|
||||
self::assertArrayHasKey('mappedRelated1', $class2->propertyAccessors);
|
||||
}
|
||||
|
||||
#[Group('DDC-1203')]
|
||||
|
||||
@@ -10,10 +10,10 @@ use Doctrine\ORM\Mapping\Column;
|
||||
use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\GeneratedValue;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessor;
|
||||
use Doctrine\ORM\Mapping\Table;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use ReflectionProperty;
|
||||
|
||||
class ClassMetadataLoadEventTest extends OrmTestCase
|
||||
{
|
||||
@@ -26,8 +26,8 @@ class ClassMetadataLoadEventTest extends OrmTestCase
|
||||
$evm->addEventListener(Events::loadClassMetadata, $this);
|
||||
$classMetadata = $metadataFactory->getMetadataFor(LoadEventTestEntity::class);
|
||||
self::assertTrue($classMetadata->hasField('about'));
|
||||
self::assertArrayHasKey('about', $classMetadata->reflFields);
|
||||
self::assertInstanceOf(ReflectionProperty::class, $classMetadata->reflFields['about']);
|
||||
self::assertArrayHasKey('about', $classMetadata->propertyAccessors);
|
||||
self::assertInstanceOf(PropertyAccessor::class, $classMetadata->propertyAccessors['about']);
|
||||
}
|
||||
|
||||
public function loadClassMetadata(LoadClassMetadataEventArgs $eventArgs): void
|
||||
|
||||
@@ -56,6 +56,7 @@ use PHPUnit\Framework\Attributes\WithoutErrorHandler;
|
||||
use ReflectionClass;
|
||||
use stdClass;
|
||||
|
||||
use function array_keys;
|
||||
use function assert;
|
||||
use function class_exists;
|
||||
use function count;
|
||||
@@ -80,7 +81,7 @@ class ClassMetadataTest extends OrmTestCase
|
||||
$cm->initializeReflection(new RuntimeReflectionService());
|
||||
|
||||
// Test initial state
|
||||
self::assertTrue(count($cm->getReflectionProperties()) === 0);
|
||||
self::assertTrue(count($cm->getPropertyAccessors()) === 0);
|
||||
self::assertInstanceOf(ReflectionClass::class, $cm->reflClass);
|
||||
self::assertEquals(CmsUser::class, $cm->name);
|
||||
self::assertEquals(CmsUser::class, $cm->rootEntityName);
|
||||
@@ -106,7 +107,7 @@ class ClassMetadataTest extends OrmTestCase
|
||||
$cm->wakeupReflection(new RuntimeReflectionService());
|
||||
|
||||
// Check state
|
||||
self::assertTrue(count($cm->getReflectionProperties()) > 0);
|
||||
self::assertTrue(count($cm->getPropertyAccessors()) > 0);
|
||||
self::assertEquals('Doctrine\Tests\Models\CMS', $cm->namespace);
|
||||
self::assertInstanceOf(ReflectionClass::class, $cm->reflClass);
|
||||
self::assertEquals(CmsUser::class, $cm->name);
|
||||
@@ -989,7 +990,7 @@ class ClassMetadataTest extends OrmTestCase
|
||||
self::assertInstanceOf(MyArrayObjectEntity::class, $classMetadata->newInstance());
|
||||
}
|
||||
|
||||
public function testWakeupReflectionWithEmbeddableAndStaticReflectionService(): void
|
||||
public function testWakeupReflectionWithEmbeddable(): void
|
||||
{
|
||||
if (! class_exists(StaticReflectionService::class)) {
|
||||
self::markTestSkipped('This test is not supported by the current installed doctrine/persistence version');
|
||||
@@ -999,7 +1000,7 @@ class ClassMetadataTest extends OrmTestCase
|
||||
|
||||
$classMetadata->mapEmbedded(
|
||||
[
|
||||
'fieldName' => 'test',
|
||||
'fieldName' => 'embedded',
|
||||
'class' => TestEntity1::class,
|
||||
'columnPrefix' => false,
|
||||
],
|
||||
@@ -1009,14 +1010,14 @@ class ClassMetadataTest extends OrmTestCase
|
||||
'fieldName' => 'test.embeddedProperty',
|
||||
'type' => 'string',
|
||||
'originalClass' => TestEntity1::class,
|
||||
'declaredField' => 'test',
|
||||
'originalField' => 'embeddedProperty',
|
||||
'declaredField' => 'embedded',
|
||||
'originalField' => 'name',
|
||||
];
|
||||
|
||||
$classMetadata->mapField($field);
|
||||
$classMetadata->wakeupReflection(new StaticReflectionService());
|
||||
|
||||
self::assertEquals(['test' => null, 'test.embeddedProperty' => null], $classMetadata->getReflectionProperties());
|
||||
self::assertEquals(['embedded', 'test.embeddedProperty'], array_keys($classMetadata->getPropertyAccessors()));
|
||||
}
|
||||
|
||||
public function testGetColumnNamesWithGivenFieldNames(): void
|
||||
|
||||
@@ -17,6 +17,7 @@ final class JoinColumnMappingTest extends TestCase
|
||||
{
|
||||
$mapping = new JoinColumnMapping('foo', 'id');
|
||||
|
||||
$mapping->deferrable = true;
|
||||
$mapping->unique = true;
|
||||
$mapping->quoted = true;
|
||||
$mapping->fieldName = 'bar';
|
||||
@@ -29,6 +30,7 @@ final class JoinColumnMappingTest extends TestCase
|
||||
$resurrectedMapping = unserialize(serialize($mapping));
|
||||
assert($resurrectedMapping instanceof JoinColumnMapping);
|
||||
|
||||
self::assertTrue($resurrectedMapping->deferrable);
|
||||
self::assertSame('foo', $resurrectedMapping->name);
|
||||
self::assertTrue($resurrectedMapping->unique);
|
||||
self::assertTrue($resurrectedMapping->quoted);
|
||||
|
||||
@@ -62,6 +62,7 @@ use Doctrine\Tests\Models\TypedProperties\UserTyped;
|
||||
use Doctrine\Tests\Models\TypedProperties\UserTypedWithCustomTypedField;
|
||||
use Doctrine\Tests\Models\Upsertable\Insertable;
|
||||
use Doctrine\Tests\Models\Upsertable\Updatable;
|
||||
use Doctrine\Tests\ORM\Mapping\NamingStrategy\CustomPascalNamingStrategy;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
use PHPUnit\Framework\Attributes\Depends;
|
||||
use stdClass;
|
||||
@@ -946,6 +947,16 @@ abstract class MappingDriverTestCase extends OrmTestCase
|
||||
|
||||
self::assertEquals(Suit::class, $metadata->fieldMappings['suit']->enumType);
|
||||
}
|
||||
|
||||
public function testCustomNamingStrategyIsRespected(): void
|
||||
{
|
||||
$ns = new CustomPascalNamingStrategy();
|
||||
$metadata = $this->createClassMetadata(BlogPostComment::class, $ns);
|
||||
|
||||
self::assertEquals('id', $metadata->fieldNames['Id']);
|
||||
self::assertEquals('Id', $metadata->associationMappings['blogPost']->joinColumns[0]->referencedColumnName);
|
||||
self::assertFalse($metadata->associationMappings['blogPost']->joinColumns[0]->nullable);
|
||||
}
|
||||
}
|
||||
|
||||
#[ORM\Entity()]
|
||||
@@ -1547,3 +1558,51 @@ abstract class GH10288EnumTypePerson
|
||||
class GH10288EnumTypeBoss extends GH10288EnumTypePerson
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Two small related entities to test default namings with barebone attributes
|
||||
*/
|
||||
#[Entity]
|
||||
class BlogPost
|
||||
{
|
||||
#[Id]
|
||||
#[Column]
|
||||
#[GeneratedValue(strategy: 'NONE')]
|
||||
public int $id;
|
||||
}
|
||||
|
||||
#[Entity]
|
||||
class BlogPostComment
|
||||
{
|
||||
#[Id]
|
||||
#[Column]
|
||||
#[GeneratedValue(strategy: 'AUTO')]
|
||||
public int $id;
|
||||
|
||||
#[ORM\ManyToOne]
|
||||
#[ORM\JoinColumn(nullable: false)]
|
||||
public BlogPost $blogPost;
|
||||
|
||||
public static function loadMetadata(ClassMetadata $metadata): void
|
||||
{
|
||||
$metadata->mapField(
|
||||
[
|
||||
'id' => true,
|
||||
'fieldName' => 'id',
|
||||
'type' => 'integer',
|
||||
],
|
||||
);
|
||||
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
|
||||
|
||||
$metadata->mapManyToOne(
|
||||
[
|
||||
'fieldName' => 'blogPost',
|
||||
'targetEntity' => BlogPost::class,
|
||||
'joinColumns' =>
|
||||
[
|
||||
0 => ['nullable' => false],
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Mapping\NamingStrategy;
|
||||
|
||||
use Doctrine\ORM\Mapping\NamingStrategy;
|
||||
use LogicException;
|
||||
|
||||
use function sprintf;
|
||||
use function str_contains;
|
||||
use function strrpos;
|
||||
use function strtolower;
|
||||
use function substr;
|
||||
use function ucfirst;
|
||||
|
||||
/**
|
||||
* Fully customized naming strategy changing all namings to a PascalCase model. Included to test some behaviours
|
||||
* regarding fully custom naming strategies.
|
||||
*/
|
||||
class CustomPascalNamingStrategy implements NamingStrategy
|
||||
{
|
||||
/**
|
||||
* Returns a table name for an entity class.
|
||||
*
|
||||
* @param string $className The fully-qualified class name
|
||||
*
|
||||
* @return string A table name
|
||||
*/
|
||||
public function classToTableName(string $className): string
|
||||
{
|
||||
if (str_contains($className, '\\')) {
|
||||
return substr($className, strrpos($className, '\\') + 1);
|
||||
}
|
||||
|
||||
return $className;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a column name for a property.
|
||||
*
|
||||
* @param string $propertyName A property name
|
||||
* @param string|null $className The fully-qualified class name
|
||||
*
|
||||
* @return string A column name
|
||||
*/
|
||||
public function propertyToColumnName(string $propertyName, string|null $className = null): string
|
||||
{
|
||||
if ($className !== null && strtolower($propertyName) === strtolower($this->classToTableName($className)) . 'id') {
|
||||
return 'Id';
|
||||
}
|
||||
|
||||
return ucfirst($propertyName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a column name for an embedded property.
|
||||
*/
|
||||
public function embeddedFieldToColumnName(string $propertyName, string $embeddedColumnName, string|null $className = null, $embeddedClassName = null): string
|
||||
{
|
||||
throw new LogicException(sprintf('Method %s is not implemented', __METHOD__));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default reference column name.
|
||||
*
|
||||
* @return string A column name
|
||||
*/
|
||||
public function referenceColumnName(): string
|
||||
{
|
||||
return 'Id';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a join column name for a property.
|
||||
*
|
||||
* @return string A join column name
|
||||
*/
|
||||
public function joinColumnName(string $propertyName, string $className): string
|
||||
{
|
||||
return ucfirst($propertyName) . $this->referenceColumnName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a join table name.
|
||||
*
|
||||
* @param string $sourceEntity The source entity
|
||||
* @param string $targetEntity The target entity
|
||||
* @param string|null $propertyName A property name
|
||||
*
|
||||
* @return string A join table name
|
||||
*/
|
||||
public function joinTableName(string $sourceEntity, string $targetEntity, string|null $propertyName = null): string
|
||||
{
|
||||
return $this->classToTableName($sourceEntity) . $this->classToTableName($targetEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the foreign key column name for the given parameters.
|
||||
*
|
||||
* @param string $entityName An entity
|
||||
* @param string|null $referencedColumnName A property
|
||||
*
|
||||
* @return string A join column name
|
||||
*/
|
||||
public function joinKeyColumnName(string $entityName, string|null $referencedColumnName = null): string
|
||||
{
|
||||
return $this->classToTableName($entityName) . ($referencedColumnName ?: $this->referenceColumnName());
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace Doctrine\Tests\ORM\Mapping;
|
||||
use Doctrine\ORM\Mapping\DefaultNamingStrategy;
|
||||
use Doctrine\ORM\Mapping\NamingStrategy;
|
||||
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
|
||||
use Doctrine\Tests\ORM\Mapping\NamingStrategy\CustomPascalNamingStrategy;
|
||||
use Doctrine\Tests\ORM\Mapping\NamingStrategy\JoinColumnClassNamingStrategy;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
@@ -33,6 +34,11 @@ class NamingStrategyTest extends OrmTestCase
|
||||
return new UnderscoreNamingStrategy(CASE_UPPER);
|
||||
}
|
||||
|
||||
private static function customNaming(): CustomPascalNamingStrategy
|
||||
{
|
||||
return new CustomPascalNamingStrategy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Data Provider for NamingStrategy#classToTableName
|
||||
*
|
||||
@@ -56,6 +62,10 @@ class NamingStrategyTest extends OrmTestCase
|
||||
[self::underscoreNamingUpper(), 'NAME', '\Some\Class\Name'],
|
||||
[self::underscoreNamingUpper(), 'NAME2_TEST', '\Some\Class\Name2Test'],
|
||||
[self::underscoreNamingUpper(), 'NAME2TEST', '\Some\Class\Name2test'],
|
||||
|
||||
// CustomPascalNamingStrategy
|
||||
[self::customNaming(), 'SomeClassName', 'SomeClassName'],
|
||||
[self::customNaming(), 'Name2Test', '\Some\Class\Name2Test'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -89,6 +99,10 @@ class NamingStrategyTest extends OrmTestCase
|
||||
[self::underscoreNamingUpper(), 'SOME_PROPERTY', 'SOME_PROPERTY', 'Some\Class'],
|
||||
[self::underscoreNamingUpper(), 'BASE64_ENCODED', 'base64Encoded', 'Some\Class'],
|
||||
[self::underscoreNamingUpper(), 'BASE64ENCODED', 'base64encoded', 'Some\Class'],
|
||||
|
||||
// CustomPascalNamingStrategy
|
||||
[self::customNaming(), 'SomeProperty', 'someProperty', 'Some\Class'],
|
||||
[self::customNaming(), 'Base64Encoded', 'base64Encoded', 'Some\Class'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -116,6 +130,9 @@ class NamingStrategyTest extends OrmTestCase
|
||||
// UnderscoreNamingStrategy
|
||||
[self::underscoreNamingLower(), 'id'],
|
||||
[self::underscoreNamingUpper(), 'ID'],
|
||||
|
||||
// CustomPascalNamingStrategy
|
||||
[self::customNaming(), 'Id'],
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\EnumPropertyAccessor;
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessorFactory;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
|
||||
class EnumPropertyAccessorTest extends OrmTestCase
|
||||
{
|
||||
public function testEnumSetEnumGetValue(): void
|
||||
{
|
||||
$object = new EnumClass();
|
||||
$accessor = PropertyAccessorFactory::createPropertyAccessor(EnumClass::class, 'enum');
|
||||
|
||||
$accessor = new EnumPropertyAccessor($accessor, EnumType::class);
|
||||
|
||||
$accessor->setValue($object, EnumType::A);
|
||||
|
||||
$this->assertEquals($object->enum, EnumType::A);
|
||||
$this->assertEquals(EnumType::A->value, $accessor->getValue($object));
|
||||
}
|
||||
|
||||
public function testEnumSetDatabaseGetValue(): void
|
||||
{
|
||||
$object = new EnumClass();
|
||||
$accessor = PropertyAccessorFactory::createPropertyAccessor(EnumClass::class, 'enum');
|
||||
|
||||
$accessor = new EnumPropertyAccessor($accessor, EnumType::class);
|
||||
|
||||
$accessor->setValue($object, EnumType::A->value);
|
||||
|
||||
$this->assertEquals($object->enum, EnumType::A);
|
||||
$this->assertEquals(EnumType::A->value, $accessor->getValue($object));
|
||||
}
|
||||
|
||||
public function testEnumSetDatabaseArrayGetValue(): void
|
||||
{
|
||||
$object = new EnumClass();
|
||||
$accessor = PropertyAccessorFactory::createPropertyAccessor(EnumClass::class, 'enumList');
|
||||
|
||||
$accessor = new EnumPropertyAccessor($accessor, EnumType::class);
|
||||
|
||||
$accessor->setValue($object, $values = [EnumType::A->value, EnumType::B->value, EnumType::C->value]);
|
||||
|
||||
$this->assertEquals($object->enumList, [EnumType::A, EnumType::B, EnumType::C]);
|
||||
$this->assertEquals($values, $accessor->getValue($object));
|
||||
}
|
||||
}
|
||||
|
||||
class EnumClass
|
||||
{
|
||||
public EnumType $enum;
|
||||
public array $enumList;
|
||||
}
|
||||
|
||||
enum EnumType: string
|
||||
{
|
||||
case A = 'a';
|
||||
case B = 'b';
|
||||
case C = 'c';
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\ObjectCastPropertyAccessor;
|
||||
use Doctrine\ORM\Proxy\InternalProxy;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
|
||||
class ObjectCastPropertyAccessorTest extends OrmTestCase
|
||||
{
|
||||
public function testSetGetPublicPropertyValue(): void
|
||||
{
|
||||
$object = new ObjectClass();
|
||||
$accessor = ObjectCastPropertyAccessor::fromNames(ObjectClass::class, 'property');
|
||||
|
||||
$accessor->setValue($object, 'value');
|
||||
|
||||
$this->assertEquals($object->property, 'value');
|
||||
$this->assertEquals('value', $accessor->getValue($object));
|
||||
}
|
||||
|
||||
public function testSetGetPrivatePropertyValue(): void
|
||||
{
|
||||
$object = new ObjectClass();
|
||||
$accessor = ObjectCastPropertyAccessor::fromNames(ObjectClass::class, 'property2');
|
||||
|
||||
$accessor->setValue($object, 'value');
|
||||
|
||||
$this->assertEquals($object->getProperty2(), 'value');
|
||||
$this->assertEquals('value', $accessor->getValue($object));
|
||||
}
|
||||
|
||||
public function testSetGetInternalProxyValue(): void
|
||||
{
|
||||
$object = new ObjectClassInternalProxy();
|
||||
$accessor = ObjectCastPropertyAccessor::fromNames(ObjectClassInternalProxy::class, 'property');
|
||||
|
||||
$accessor->setValue($object, 'value');
|
||||
|
||||
$this->assertEquals($object->property, 'value');
|
||||
$this->assertEquals('value', $accessor->getValue($object));
|
||||
$this->assertFalse($object->isInitialized);
|
||||
$this->assertEquals(2, $object->counter);
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectClass
|
||||
{
|
||||
/** @var string */
|
||||
public $property;
|
||||
/** @var string */
|
||||
private $property2;
|
||||
|
||||
public function getProperty2(): string
|
||||
{
|
||||
return $this->property2;
|
||||
}
|
||||
}
|
||||
|
||||
class ObjectClassInternalProxy implements InternalProxy
|
||||
{
|
||||
/** @var string */
|
||||
public $property;
|
||||
public bool $isInitialized = false;
|
||||
public int $counter = 0;
|
||||
|
||||
public function __setInitialized(bool $initialized): void
|
||||
{
|
||||
$this->isInitialized = $initialized;
|
||||
$this->counter++;
|
||||
}
|
||||
|
||||
public function __load(): void
|
||||
{
|
||||
}
|
||||
|
||||
/** Returns whether this proxy is initialized or not. */
|
||||
public function __isInitialized(): bool
|
||||
{
|
||||
return $this->isInitialized;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\RawValuePropertyAccessor;
|
||||
use Doctrine\Tests\Models\PropertyHooks\User;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
use PHPUnit\Framework\Attributes\RequiresPhp;
|
||||
use ReflectionObject;
|
||||
|
||||
use function trim;
|
||||
|
||||
#[RequiresPhp(versionRequirement: '>= 8.4.0')]
|
||||
class RawValuePropertyAccessorTest extends OrmTestCase
|
||||
{
|
||||
public function testSetGetValue(): void
|
||||
{
|
||||
$object = new User();
|
||||
$reflection = new ReflectionObject($object);
|
||||
$accessorFirst = RawValuePropertyAccessor::fromReflectionProperty($reflection->getProperty('first'));
|
||||
$accessorLast = RawValuePropertyAccessor::fromReflectionProperty($reflection->getProperty('last'));
|
||||
|
||||
$accessorFirst->setValue($object, 'Benjamin');
|
||||
$accessorLast->setValue($object, 'Eberlei');
|
||||
|
||||
self::assertEquals('Benjamin Eberlei', $object->fullName);
|
||||
self::assertEquals('Benjamin', $accessorFirst->getValue($object));
|
||||
self::assertEquals('Eberlei', $accessorLast->getValue($object));
|
||||
|
||||
$accessorFirst->setValue($object, '');
|
||||
$accessorLast->setValue($object, '');
|
||||
|
||||
self::assertEquals('', trim($object->fullName));
|
||||
}
|
||||
|
||||
public function testSetGetValueWithLanguage(): void
|
||||
{
|
||||
$object = new User();
|
||||
$reflection = new ReflectionObject($object);
|
||||
$accessor = RawValuePropertyAccessor::fromReflectionProperty($reflection->getProperty('language'));
|
||||
|
||||
$accessor->setValue($object, 'en');
|
||||
|
||||
self::assertEquals('EN', $object->language);
|
||||
self::assertEquals('en', $accessor->getValue($object));
|
||||
|
||||
$accessor->setValue($object, 'EN');
|
||||
|
||||
self::assertEquals('EN', $object->language);
|
||||
self::assertEquals('EN', $accessor->getValue($object));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessorFactory;
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\ReadonlyAccessor;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
use LogicException;
|
||||
|
||||
class ReadOnlyAccessorTest extends OrmTestCase
|
||||
{
|
||||
public function testReadOnlyProperty(): void
|
||||
{
|
||||
$object = new ReadOnlyClass();
|
||||
$accessor = PropertyAccessorFactory::createPropertyAccessor(ReadOnlyClass::class, 'property');
|
||||
|
||||
$this->assertInstanceOf(ReadonlyAccessor::class, $accessor);
|
||||
|
||||
$accessor->setValue($object, 1);
|
||||
|
||||
$this->assertEquals($object->property, 1);
|
||||
$this->assertEquals(1, $accessor->getValue($object));
|
||||
}
|
||||
|
||||
public function testReadOnlyPropertyOnlyOnce(): void
|
||||
{
|
||||
$object = new ReadOnlyClass();
|
||||
$accessor = PropertyAccessorFactory::createPropertyAccessor(ReadOnlyClass::class, 'property');
|
||||
|
||||
$this->assertInstanceOf(ReadonlyAccessor::class, $accessor);
|
||||
|
||||
$accessor->setValue($object, 1);
|
||||
$this->expectException(LogicException::class);
|
||||
$accessor->setValue($object, 2);
|
||||
}
|
||||
}
|
||||
|
||||
class ReadOnlyClass
|
||||
{
|
||||
public readonly int $property;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Mapping\PropertyAccessors;
|
||||
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\PropertyAccessorFactory;
|
||||
use Doctrine\ORM\Mapping\PropertyAccessors\TypedNoDefaultPropertyAccessor;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
|
||||
class TypedNoDefaultPropertyAccessorTest extends OrmTestCase
|
||||
{
|
||||
public function testSetValueWithoutDefault(): void
|
||||
{
|
||||
$accessor = PropertyAccessorFactory::createPropertyAccessor(TypedClass::class, 'property');
|
||||
|
||||
$this->assertInstanceOf(TypedNoDefaultPropertyAccessor::class, $accessor);
|
||||
|
||||
$object = new TypedClass();
|
||||
$accessor->setValue($object, 42);
|
||||
$this->assertEquals(42, $accessor->getValue($object));
|
||||
}
|
||||
|
||||
public function testSetNullWithoutDefault(): void
|
||||
{
|
||||
$accessor = PropertyAccessorFactory::createPropertyAccessor(TypedClass::class, 'property');
|
||||
|
||||
$object = new TypedClass();
|
||||
$accessor->setValue($object, null);
|
||||
$this->assertNull($accessor->getValue($object));
|
||||
|
||||
$accessor->setValue($object, 42);
|
||||
$this->assertEquals(42, $accessor->getValue($object));
|
||||
|
||||
$accessor->setValue($object, null);
|
||||
$this->assertNull($accessor->getValue($object));
|
||||
}
|
||||
}
|
||||
|
||||
class TypedClass
|
||||
{
|
||||
public int $property;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
|
||||
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
|
||||
|
||||
<entity name="Doctrine\Tests\ORM\Mapping\BlogPost">
|
||||
|
||||
<id name="id" type="integer" column="id">
|
||||
<generator strategy="NONE"/>
|
||||
</id>
|
||||
|
||||
</entity>
|
||||
|
||||
</doctrine-mapping>
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
|
||||
https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
|
||||
|
||||
<entity name="Doctrine\Tests\ORM\Mapping\BlogPostComment">
|
||||
|
||||
<id name="id" type="integer">
|
||||
<generator strategy="NONE"/>
|
||||
</id>
|
||||
|
||||
<many-to-one field="blogPost" target-entity="Doctrine\Tests\ORM\Mapping\BlogPost">
|
||||
<join-columns>
|
||||
<join-column nullable="false"/>
|
||||
</join-columns>
|
||||
</many-to-one>
|
||||
|
||||
</entity>
|
||||
|
||||
</doctrine-mapping>
|
||||
@@ -20,6 +20,8 @@ use Doctrine\Tests\Models\Company\CompanyPerson;
|
||||
use Doctrine\Tests\Models\ECommerce\ECommerceFeature;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use PHPUnit\Framework\Attributes\RequiresPhp;
|
||||
use ReflectionClass;
|
||||
use ReflectionProperty;
|
||||
use stdClass;
|
||||
|
||||
@@ -127,7 +129,6 @@ class ProxyFactoryTest extends OrmTestCase
|
||||
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
|
||||
|
||||
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]);
|
||||
assert($proxy instanceof Proxy);
|
||||
|
||||
$persister
|
||||
->expects(self::atLeastOnce())
|
||||
@@ -140,7 +141,19 @@ class ProxyFactoryTest extends OrmTestCase
|
||||
} catch (EntityNotFoundException) {
|
||||
}
|
||||
|
||||
self::assertFalse($proxy->__isInitialized());
|
||||
self::assertUninitializedLazyObject($proxy);
|
||||
}
|
||||
|
||||
private static function assertUninitializedLazyObject(object $proxy): void
|
||||
{
|
||||
if ($proxy instanceof Proxy) {
|
||||
self::assertFalse($proxy->__isInitialized());
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$reflectionClass = new ReflectionClass($proxy);
|
||||
self::assertTrue($reflectionClass->isUninitializedLazyObject($proxy));
|
||||
}
|
||||
|
||||
#[Group('DDC-2432')]
|
||||
@@ -153,7 +166,6 @@ class ProxyFactoryTest extends OrmTestCase
|
||||
$this->uowMock->setEntityPersister(ECommerceFeature::class, $persister);
|
||||
|
||||
$proxy = $this->proxyFactory->getProxy(ECommerceFeature::class, ['id' => 42]);
|
||||
assert($proxy instanceof Proxy);
|
||||
|
||||
$persister
|
||||
->expects(self::atLeastOnce())
|
||||
@@ -167,11 +179,15 @@ class ProxyFactoryTest extends OrmTestCase
|
||||
} catch (EntityNotFoundException) {
|
||||
}
|
||||
|
||||
self::assertFalse($proxy->__isInitialized());
|
||||
self::assertUninitializedLazyObject($proxy);
|
||||
}
|
||||
|
||||
public function testProxyClonesParentFields(): void
|
||||
{
|
||||
if ($this->emMock->getConfiguration()->isNativeLazyObjectsEnabled()) {
|
||||
self::markTestSkipped('This test is not relevant when native lazy objects are enabled');
|
||||
}
|
||||
|
||||
$companyEmployee = new CompanyEmployee();
|
||||
$companyEmployee->setSalary(1000); // A property on the CompanyEmployee
|
||||
$companyEmployee->setName('Bob'); // A property on the parent class, CompanyPerson
|
||||
@@ -209,6 +225,24 @@ class ProxyFactoryTest extends OrmTestCase
|
||||
self::assertSame(1000, $cloned->getSalary(), 'Expect properties on the CompanyEmployee class to be cloned');
|
||||
self::assertSame('Bob', $cloned->getName(), 'Expect properties on the CompanyPerson class to be cloned');
|
||||
}
|
||||
|
||||
#[RequiresPhp('8.4')]
|
||||
public function testProxyFactoryAcceptsNullProxyArgsWhenNativeLazyObjectsAreEnabled(): void
|
||||
{
|
||||
$this->emMock->getConfiguration()->enableNativeLazyObjects(true);
|
||||
$this->proxyFactory = new ProxyFactory(
|
||||
$this->emMock,
|
||||
null,
|
||||
null,
|
||||
);
|
||||
$proxy = $this->proxyFactory->getProxy(
|
||||
ECommerceFeature::class,
|
||||
['id' => 42],
|
||||
);
|
||||
$reflection = new ReflectionClass($proxy);
|
||||
|
||||
self::assertTrue($reflection->isUninitializedLazyObject($proxy));
|
||||
}
|
||||
}
|
||||
|
||||
abstract class AbstractClass
|
||||
|
||||
@@ -15,6 +15,7 @@ use Doctrine\ORM\Query\Expr\Join;
|
||||
use Doctrine\ORM\Query\Parameter;
|
||||
use Doctrine\ORM\Query\ParameterTypeInferer;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Doctrine\ORM\QueryType;
|
||||
use Doctrine\Tests\Mocks\EntityManagerMock;
|
||||
use Doctrine\Tests\Models\Cache\State;
|
||||
use Doctrine\Tests\Models\CMS\CmsArticle;
|
||||
@@ -23,6 +24,7 @@ use Doctrine\Tests\Models\CMS\CmsUser;
|
||||
use Doctrine\Tests\OrmTestCase;
|
||||
use InvalidArgumentException;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use function array_filter;
|
||||
use function class_exists;
|
||||
@@ -1326,4 +1328,25 @@ class QueryBuilderTest extends OrmTestCase
|
||||
self::assertEquals(100, $qb->getParameter('dcValue1')->getValue());
|
||||
self::assertEquals(Types::INTEGER, $qb->getParameter('dcValue1')->getType());
|
||||
}
|
||||
|
||||
public function testType(): void
|
||||
{
|
||||
$qb = new class ($this->entityManager) extends QueryBuilder {
|
||||
public function test(): void
|
||||
{
|
||||
TestCase::assertSame(QueryType::Select, $this->getType());
|
||||
|
||||
$this->delete();
|
||||
TestCase::assertSame(QueryType::Delete, $this->getType());
|
||||
|
||||
$this->update();
|
||||
TestCase::assertSame(QueryType::Update, $this->getType());
|
||||
|
||||
$this->select();
|
||||
TestCase::assertSame(QueryType::Select, $this->getType());
|
||||
}
|
||||
};
|
||||
|
||||
$qb->test();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Tools\Console\Command;
|
||||
|
||||
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
|
||||
use Doctrine\ORM\Tools\Console\Command\ClearCache\CollectionRegionCommand;
|
||||
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
|
||||
use Doctrine\Tests\Models\Cache\State;
|
||||
@@ -15,6 +16,8 @@ use Symfony\Component\Console\Tester\CommandTester;
|
||||
#[Group('DDC-2183')]
|
||||
class ClearCacheCollectionRegionCommandTest extends OrmFunctionalTestCase
|
||||
{
|
||||
use ApplicationCompatibility;
|
||||
|
||||
private Application $application;
|
||||
|
||||
private CollectionRegionCommand $command;
|
||||
@@ -28,7 +31,7 @@ class ClearCacheCollectionRegionCommandTest extends OrmFunctionalTestCase
|
||||
$this->command = new CollectionRegionCommand(new SingleManagerProvider($this->_em));
|
||||
|
||||
$this->application = new Application();
|
||||
$this->application->add($this->command);
|
||||
self::addCommandToApplication($this->application, $this->command);
|
||||
}
|
||||
|
||||
public function testClearAllRegion(): void
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Tools\Console\Command;
|
||||
|
||||
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
|
||||
use Doctrine\ORM\Tools\Console\Command\ClearCache\EntityRegionCommand;
|
||||
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
|
||||
use Doctrine\Tests\Models\Cache\Country;
|
||||
@@ -18,6 +19,8 @@ use function trim;
|
||||
#[Group('DDC-2183')]
|
||||
class ClearCacheEntityRegionCommandTest extends OrmFunctionalTestCase
|
||||
{
|
||||
use ApplicationCompatibility;
|
||||
|
||||
private Application $application;
|
||||
|
||||
private EntityRegionCommand $command;
|
||||
@@ -31,7 +34,7 @@ class ClearCacheEntityRegionCommandTest extends OrmFunctionalTestCase
|
||||
$this->command = new EntityRegionCommand(new SingleManagerProvider($this->_em));
|
||||
|
||||
$this->application = new Application();
|
||||
$this->application->add($this->command);
|
||||
self::addCommandToApplication($this->application, $this->command);
|
||||
}
|
||||
|
||||
public function testClearAllRegion(): void
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Tools\Console\Command;
|
||||
|
||||
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
|
||||
use Doctrine\ORM\Tools\Console\Command\ClearCache\QueryRegionCommand;
|
||||
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
|
||||
use Doctrine\Tests\OrmFunctionalTestCase;
|
||||
@@ -14,6 +15,8 @@ use Symfony\Component\Console\Tester\CommandTester;
|
||||
#[Group('DDC-2183')]
|
||||
class ClearCacheQueryRegionCommandTest extends OrmFunctionalTestCase
|
||||
{
|
||||
use ApplicationCompatibility;
|
||||
|
||||
private Application $application;
|
||||
|
||||
private QueryRegionCommand $command;
|
||||
@@ -27,7 +30,7 @@ class ClearCacheQueryRegionCommandTest extends OrmFunctionalTestCase
|
||||
$this->command = new QueryRegionCommand(new SingleManagerProvider($this->_em));
|
||||
|
||||
$this->application = new Application();
|
||||
$this->application->add($this->command);
|
||||
self::addCommandToApplication($this->application, $this->command);
|
||||
}
|
||||
|
||||
public function testClearAllRegion(): void
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace Doctrine\Tests\ORM\Tools\Console\Command;
|
||||
use Doctrine\ORM\Configuration;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Mapping\MappingException;
|
||||
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
|
||||
use Doctrine\ORM\Tools\Console\Command\InfoCommand;
|
||||
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
|
||||
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
|
||||
@@ -18,6 +19,8 @@ use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
class InfoCommandTest extends OrmFunctionalTestCase
|
||||
{
|
||||
use ApplicationCompatibility;
|
||||
|
||||
private Application $application;
|
||||
private InfoCommand $command;
|
||||
private CommandTester $tester;
|
||||
@@ -28,7 +31,7 @@ class InfoCommandTest extends OrmFunctionalTestCase
|
||||
|
||||
$this->application = new Application();
|
||||
|
||||
$this->application->add(new InfoCommand(new SingleManagerProvider($this->_em)));
|
||||
self::addCommandToApplication($this->application, new InfoCommand(new SingleManagerProvider($this->_em)));
|
||||
|
||||
$this->command = $this->application->find('orm:info');
|
||||
$this->tester = new CommandTester($this->command);
|
||||
@@ -58,7 +61,7 @@ class InfoCommandTest extends OrmFunctionalTestCase
|
||||
->willReturn($configuration);
|
||||
|
||||
$application = new Application();
|
||||
$application->add(new InfoCommand(new SingleManagerProvider($em)));
|
||||
self::addCommandToApplication($application, new InfoCommand(new SingleManagerProvider($em)));
|
||||
|
||||
$command = $application->find('orm:info');
|
||||
$tester = new CommandTester($command);
|
||||
@@ -96,7 +99,7 @@ class InfoCommandTest extends OrmFunctionalTestCase
|
||||
->willThrowException(new MappingException('exception message'));
|
||||
|
||||
$application = new Application();
|
||||
$application->add(new InfoCommand(new SingleManagerProvider($em)));
|
||||
self::addCommandToApplication($application, new InfoCommand(new SingleManagerProvider($em)));
|
||||
|
||||
$command = $application->find('orm:info');
|
||||
$tester = new CommandTester($command);
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Tools\Console\Command;
|
||||
|
||||
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
|
||||
use Doctrine\ORM\Tools\Console\Command\MappingDescribeCommand;
|
||||
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
|
||||
use Doctrine\Tests\Models\Cache\AttractionInfo;
|
||||
@@ -19,6 +20,8 @@ use Symfony\Component\Console\Tester\CommandTester;
|
||||
#[CoversClass(MappingDescribeCommand::class)]
|
||||
class MappingDescribeCommandTest extends OrmFunctionalTestCase
|
||||
{
|
||||
use ApplicationCompatibility;
|
||||
|
||||
private Application $application;
|
||||
|
||||
private MappingDescribeCommand $command;
|
||||
@@ -30,7 +33,7 @@ class MappingDescribeCommandTest extends OrmFunctionalTestCase
|
||||
parent::setUp();
|
||||
|
||||
$this->application = new Application();
|
||||
$this->application->add(new MappingDescribeCommand(new SingleManagerProvider($this->_em)));
|
||||
self::addCommandToApplication($this->application, new MappingDescribeCommand(new SingleManagerProvider($this->_em)));
|
||||
|
||||
$this->command = $this->application->find('orm:mapping:describe');
|
||||
$this->tester = new CommandTester($this->command);
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Tests\ORM\Tools\Console\Command;
|
||||
|
||||
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
|
||||
use Doctrine\ORM\Tools\Console\Command\RunDqlCommand;
|
||||
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
|
||||
use Doctrine\Tests\Models\Generic\DateTimeModel;
|
||||
@@ -20,6 +21,8 @@ use function trim;
|
||||
#[CoversClass(RunDqlCommand::class)]
|
||||
class RunDqlCommandTest extends OrmFunctionalTestCase
|
||||
{
|
||||
use ApplicationCompatibility;
|
||||
|
||||
private Application $application;
|
||||
|
||||
private RunDqlCommand $command;
|
||||
@@ -35,7 +38,7 @@ class RunDqlCommandTest extends OrmFunctionalTestCase
|
||||
$this->command = new RunDqlCommand(new SingleManagerProvider($this->_em));
|
||||
|
||||
$this->application = new Application();
|
||||
$this->application->add($this->command);
|
||||
self::addCommandToApplication($this->application, $this->command);
|
||||
|
||||
$this->tester = new CommandTester($this->command);
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Doctrine\Tests\ORM\Tools\Console\Command;
|
||||
|
||||
use Doctrine\DBAL\Platforms\SQLitePlatform;
|
||||
use Doctrine\DBAL\Schema\SchemaDiff;
|
||||
use Doctrine\ORM\Tools\Console\ApplicationCompatibility;
|
||||
use Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand;
|
||||
use Doctrine\ORM\Tools\Console\EntityManagerProvider\SingleManagerProvider;
|
||||
use Doctrine\Tests\OrmFunctionalTestCase;
|
||||
@@ -22,6 +23,8 @@ use function method_exists;
|
||||
#[CoversClass(ValidateSchemaCommand::class)]
|
||||
class ValidateSchemaCommandTest extends OrmFunctionalTestCase
|
||||
{
|
||||
use ApplicationCompatibility;
|
||||
|
||||
private ValidateSchemaCommand $command;
|
||||
|
||||
private CommandTester $tester;
|
||||
@@ -39,7 +42,7 @@ class ValidateSchemaCommandTest extends OrmFunctionalTestCase
|
||||
}
|
||||
|
||||
$application = new Application();
|
||||
$application->add(new ValidateSchemaCommand(new SingleManagerProvider($this->_em)));
|
||||
self::addCommandToApplication($application, new ValidateSchemaCommand(new SingleManagerProvider($this->_em)));
|
||||
|
||||
$this->command = $application->find('orm:validate-schema');
|
||||
$this->tester = new CommandTester($this->command);
|
||||
|
||||
@@ -20,6 +20,7 @@ use Doctrine\ORM\Mapping\Entity;
|
||||
use Doctrine\ORM\Mapping\GeneratedValue;
|
||||
use Doctrine\ORM\Mapping\Id;
|
||||
use Doctrine\ORM\Mapping\ManyToOne;
|
||||
use Doctrine\ORM\Mapping\MappingException;
|
||||
use Doctrine\ORM\Mapping\Version;
|
||||
use Doctrine\ORM\OptimisticLockException;
|
||||
use Doctrine\ORM\ORMInvalidArgumentException;
|
||||
@@ -40,6 +41,7 @@ use PHPUnit\Framework\MockObject\MockObject;
|
||||
use stdClass;
|
||||
|
||||
use function enum_exists;
|
||||
use function is_object;
|
||||
use function random_int;
|
||||
use function uniqid;
|
||||
|
||||
@@ -278,7 +280,19 @@ class UnitOfWorkTest extends OrmTestCase
|
||||
$user->username = 'John';
|
||||
$user->avatar = $invalidValue;
|
||||
|
||||
$this->expectException(ORMInvalidArgumentException::class);
|
||||
if (
|
||||
is_object($invalidValue) &&
|
||||
! $invalidValue instanceof ArrayCollection &&
|
||||
$this->_emMock->getConfiguration()->isNativeLazyObjectsEnabled()
|
||||
) {
|
||||
// in the case of stdClass, the changeset is rejected because
|
||||
// stdClass is not a valid entity
|
||||
// when using native lazy objects, this happens because UnitOfWork::isUninitializedObject()
|
||||
// needs to load the class metadata to do its job
|
||||
$this->expectException(MappingException::class);
|
||||
} else {
|
||||
$this->expectException(ORMInvalidArgumentException::class);
|
||||
}
|
||||
|
||||
$this->_unitOfWork->computeChangeSet($metadata, $user);
|
||||
}
|
||||
|
||||
@@ -194,6 +194,7 @@ use function strtolower;
|
||||
use function var_export;
|
||||
|
||||
use const PHP_EOL;
|
||||
use const PHP_VERSION_ID;
|
||||
|
||||
/**
|
||||
* Base testcase class for all functional ORM testcases.
|
||||
@@ -938,6 +939,19 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
|
||||
$this->isSecondLevelCacheEnabled = true;
|
||||
}
|
||||
|
||||
$enableNativeLazyObjects = getenv('ENABLE_NATIVE_LAZY_OBJECTS');
|
||||
|
||||
if ($enableNativeLazyObjects === false) {
|
||||
// If the environment variable is not set, we default to true.
|
||||
// This is OK because environment variables are always strings, and
|
||||
// we are comparing it to a boolean.
|
||||
$enableNativeLazyObjects = true;
|
||||
}
|
||||
|
||||
if (PHP_VERSION_ID >= 80400 && $enableNativeLazyObjects) {
|
||||
$config->enableNativeLazyObjects(true);
|
||||
}
|
||||
|
||||
$config->setMetadataDriverImpl(
|
||||
$mappingDriver ?? new AttributeDriver([
|
||||
realpath(__DIR__ . '/Models/Cache'),
|
||||
@@ -1120,4 +1134,9 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
|
||||
{
|
||||
return $this->_em->getUnitOfWork()->isUninitializedObject($entity);
|
||||
}
|
||||
|
||||
final protected function initializeObject(object $entity): void
|
||||
{
|
||||
$this->_em->getUnitOfWork()->initializeObject($entity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,14 @@ use function class_exists;
|
||||
use function explode;
|
||||
use function fwrite;
|
||||
use function get_debug_type;
|
||||
use function getenv;
|
||||
use function in_array;
|
||||
use function sprintf;
|
||||
use function str_starts_with;
|
||||
use function strlen;
|
||||
use function substr;
|
||||
|
||||
use const PHP_VERSION_ID;
|
||||
use const STDERR;
|
||||
|
||||
/**
|
||||
@@ -89,8 +91,23 @@ class TestUtil
|
||||
|
||||
public static function configureProxies(Configuration $configuration): void
|
||||
{
|
||||
$enableNativeLazyObjects = getenv('ENABLE_NATIVE_LAZY_OBJECTS');
|
||||
|
||||
if ($enableNativeLazyObjects === false) {
|
||||
// If the environment variable is not set, we default to true.
|
||||
// This is OK because environment variables are always strings, and
|
||||
// we are comparing it to a boolean.
|
||||
$enableNativeLazyObjects = true;
|
||||
}
|
||||
|
||||
$configuration->setProxyDir(__DIR__ . '/Proxies');
|
||||
$configuration->setProxyNamespace('Doctrine\Tests\Proxies');
|
||||
|
||||
if (PHP_VERSION_ID >= 80400 && $enableNativeLazyObjects) {
|
||||
$configuration->enableNativeLazyObjects(true);
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private static function initializeDatabase(): void
|
||||
|
||||
Reference in New Issue
Block a user