Compare commits

...

78 Commits
3.3.4 ... 3.4.0

Author SHA1 Message Date
Grégoire Paris
4664373bd0 Merge pull request #11985 from doctrine/3.3.x-merge-up-into-3.4.x_KXrSCX8l
Merge release 3.3.4 into 3.4.x
2025-06-14 13:47:14 +02:00
Grégoire Paris
97b29bb063 Merge pull request #11973 from eltharin/add_constructor
Add constructor argument
2025-06-07 09:41:24 +02:00
eltharin
b7fff508a4 add argument in constructor 2025-06-06 18:39:24 +02:00
Grégoire Paris
c6fa14ed52 Merge pull request #11971 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-06-06 13:05:27 +02:00
Grégoire Paris
0a43e4af8f Merge pull request #11946 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-05-25 18:35:30 +02:00
Olivier Massot
35d301b052 Association Mappings: replace assertions by explicit exceptions (#11896) 2025-05-16 08:37:08 +02:00
Grégoire Paris
083b241c81 Merge pull request #11846 from eltharin/all_fields
add capability to use allfields sql notation
2025-05-08 10:55:51 +02:00
Grégoire Paris
b9989555fd Merge pull request #11927 from greg0ire/3.4.x
Merge 3.3.x up into 3.4.x
2025-05-02 20:26:12 +02:00
Grégoire Paris
80a79f6d2d Merge remote-tracking branch 'origin/3.3.x' into 3.4.x 2025-05-02 19:48:18 +02:00
Grégoire Paris
9a3f5579f1 Merge pull request #11921 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-04-24 18:59:58 +02:00
eltharin
12c721f528 add capability to use allfields sql notation
in a dto, this PR allow to call u.* to get all fileds fo u entity in one call,
2025-04-22 21:40:54 +02:00
Grégoire Paris
9a9c3e8aba Merge pull request #11847 from eltharin/newentityInDto
add capability to hydrate an entity in a dto
2025-04-22 21:00:16 +02:00
eltharin
46a020108d add capability to hydrate an entity in a dto
this PR allow to hydrate data in an entity  nested in a dto
2025-04-21 14:29:19 +02:00
Grégoire Paris
b286d6cd2c Merge pull request #11902 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-04-14 23:38:21 +02:00
Benjamin Eberlei
443cf92242 Merge pull request #11852 from beberlei/PropertyHookSupport
Final tests and adjustments to allow mapping properties with hooks.
Property hooks are not supported when using `symfony/var-exporter`.
2025-04-12 11:33:27 +02:00
Benjamin Eberlei
eb3b984132 Add support for PHP 8.4 Lazy Objects RFC with configuration flag (#11853)
* Introduce PHP 8.4 lazy proxy/ghost API.

* Call setRawValueWithoutLazyInitialization for support with lazy proxy.

* Refactorings

* Revert test change partially and skip with lazy objects.

* Houskeeping: phpcs

* Run with ENABLE_LAZY_PROXY=1 in php 8.4 matrix.

* Fix ci

* Transient properties are not skipping lazy initialization anymore, to expensive and could lead to errors. Adjust lifecycle test that uses transient properittes for assertions.

* Restore behavior preventing property hook use in 8.4 in unsupported coditions

* Add \ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE

Co-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>

* Rename isNativeLazyObjectsEnabled/enableNativeLazyObjects.

* Housekeeping: phpcs

* Update advanced-configuration docs and make proxy config variables not required anymore with native lazy objects.

* Move code around

* Apply suggestions from code review

Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>

* Pick suggestions

---------

Co-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com>
Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
2025-03-29 23:14:13 +01:00
Grégoire Paris
04395f98f9 Merge pull request #11887 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-03-25 16:25:25 +01:00
Grégoire Paris
0c10010f9f Merge pull request #11884 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-03-24 22:34:05 +01:00
Grégoire Paris
be8da83aca Merge pull request #10624 from simPod/deferrable
feat: allow setting foreign key as deferrable
2025-03-23 10:09:10 +01:00
Grégoire Paris
f5ab687226 Merge pull request #11876 from greg0ire/address-reflfield-depr
Address deprecation of ClassMetadata::$reflFields
2025-03-19 15:53:29 +01:00
Grégoire Paris
742eead849 Merge pull request #11878 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-03-18 21:38:05 +01:00
Grégoire Paris
f98e871913 Address deprecation of ClassMetadata::$reflFields
We should use the newly introduced ClassMetadata::$propertyAccessors instead.
See https://github.com/doctrine/orm/pull/11659
2025-03-18 19:39:48 +01:00
Grégoire Paris
4b0c11978e Merge pull request #11875 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-03-18 14:25:17 +01:00
Grégoire Paris
0ef5610a6c Merge pull request #11873 from beberlei/GH-11659-FollowUp1
Bugfix: Missed a spot using getUnderlyingReflector
2025-03-16 23:18:10 +01:00
Benjamin Eberlei
e29d0e977d Bugfix: Missed a spot using getUnderlyingReflector 2025-03-15 17:39:14 +01:00
Grégoire Paris
d540f73778 Merge pull request #11659 from beberlei/PropertyHooks
Necessary refactorings for Property hooks
2025-02-27 20:04:08 +01:00
Benjamin Eberlei
201d751a26 Allow access to underlying reflector for property accessor. 2025-02-26 00:48:30 +01:00
Benjamin Eberlei
6308b2fd86 Update tests/Tests/ORM/Mapping/PropertyAccessors/ReadOnlyAccessorTest.php
Co-authored-by: Claudio Zizza <859964+SenseException@users.noreply.github.com>
2025-02-26 00:38:41 +01:00
Benjamin Eberlei
8f99e84438 Update src/Mapping/PropertyAccessors/EnumPropertyAccessor.php
Co-authored-by: Claudio Zizza <859964+SenseException@users.noreply.github.com>
2025-02-26 00:34:12 +01:00
Benjamin Eberlei
e36b7755e9 Houskeeping: phpcs 2025-02-23 19:43:26 +01:00
Benjamin Eberlei
7b4d869b31 Merge branch '3.4.x' into PropertyHooks 2025-02-23 19:23:20 +01:00
Grégoire Paris
8873109b4f Merge pull request #11840 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-02-18 23:12:40 +01:00
Benjamin Eberlei
5077ae41e5 Housekeeping 2025-02-15 23:25:34 +01:00
Benjamin Eberlei
8e1a27b8cc Explain deprecation in UPGRADE.md 2025-02-15 22:32:16 +01:00
Benjamin Eberlei
e7db1b005f Add ReadOnlyAccessorTest 2025-02-15 22:17:29 +01:00
Benjamin Eberlei
72ce662e45 Tests for ObjectCastPropertyAccessor and RawValuePropertyAccessor. 2025-02-15 22:09:36 +01:00
Benjamin Eberlei
673cf0d4d8 Add test for ObjectCastPropertyAccessor. 2025-02-15 21:45:01 +01:00
Benjamin Eberlei
1cae0534a0 Extract PropertyAccessorFactory, tests for enum and typednodefault accessors. 2025-02-15 21:38:09 +01:00
Benjamin Eberlei
6fb3083f63 Merge remote-tracking branch 'beberlei/PropertyHooks' into PropertyHooks 2025-02-15 00:02:45 +01:00
Benjamin Eberlei
68c17ca1bd Merge remote-tracking branch 'origin/3.4.x' into PropertyHooks 2025-02-15 00:01:50 +01:00
Benjamin Eberlei
82cf29407c Update src/Mapping/PropertyAccessors/PropertyAccessor.php
Co-authored-by: Grégoire Paris <postmaster@greg0ire.fr>
2025-02-15 00:01:22 +01:00
Grégoire Paris
ae74be5e9d Merge pull request #11823 from doctrine/3.3.x-merge-up-into-3.4.x_lzhu6IBq
Merge release 3.3.2 into 3.4.x
2025-02-05 08:08:57 +01:00
Grégoire Paris
4163efd2f2 Merge pull request #11813 from VincentLanglet/queryType
[RFC] Expose QueryBuilder::getType
2025-01-29 11:54:45 +01:00
Vincent Langlet
d7ac6123ad Expose QueryType 2025-01-29 09:27:44 +01:00
Simon Podlipsky
bd260d1be8 feat: allow setting foreign key as deferrable 2025-01-26 13:06:55 +01:00
Grégoire Paris
cd1a52c7e4 Merge pull request #11808 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2025-01-25 11:29:59 +01:00
Alexander M. Turek
0d2cb6acd1 Fix CS (#11782) 2025-01-07 09:53:43 +01:00
Alexander M. Turek
327418a4b7 Merge branch '3.3.x' into 3.4.x
* 3.3.x:
  Update working-with-objects.rst (#7553)
  changed confusing negative wording (#11775)
2025-01-06 20:51:29 +01:00
Grégoire Paris
9f2b367081 Merge pull request #11776 from curry684/issue-9558
Respect referencedColumnName defaults in custom naming strategies
2024-12-25 00:20:07 +01:00
Niels Keurentjes
a9873c86bb Take hardcoded reference column name out of JoinColumn attribute
Previously, when using a custom naming strategy, explicitly declaring a JoinColumn required specifying the referencedColumnName always as it would default to id no matter the naming strategy. This PR changes it to be determines correctly.

Ref #9558
2024-12-23 19:44:56 +01:00
Grégoire Paris
8ebd98ee92 Merge pull request #11773 from doctrine/3.3.x-merge-up-into-3.4.x_xx7XyUCl
Merge release 3.3.1 into 3.4.x
2024-12-19 08:27:07 +01:00
Benjamin Eberlei
5a220078e9 Update PR with PHP Stan by fixing some and baselining other violations. 2024-12-08 21:11:41 +01:00
Grégoire Paris
a15543a2ce Merge pull request #11761 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2024-12-08 13:02:05 +01:00
Benjamin Eberlei
238fb74028 Add RawValuePropertyAccessor to see how it will look in 8.4, pre support for lazy objects. 2024-12-08 13:00:10 +01:00
Benjamin Eberlei
6ff2b130d3 Add comment to PropertyAccessor interface 2024-12-08 13:00:10 +01:00
Benjamin Eberlei
8c9bfca255 Fix wrong type, phpstan failure. 2024-12-08 13:00:10 +01:00
Benjamin Eberlei
c2a2386df9 suppress phpcs that cant be done 2024-12-08 13:00:10 +01:00
Benjamin Eberlei
2f98e11562 Remove last use of reflFields in core. 2024-12-08 13:00:10 +01:00
Benjamin Eberlei
073809cf5c Fixup EnumPropertyAccessor::toEnum 2024-12-08 13:00:10 +01:00
Benjamin Eberlei
e82690d256 More psalm to fix the errors. 2024-12-08 13:00:07 +01:00
Benjamin Eberlei
23c31aec51 Static analysis. 2024-12-08 12:57:19 +01:00
Benjamin Eberlei
622ba2dcc7 Mark all PropertyAccessor classes @internal. 2024-12-08 12:56:39 +01:00
Benjamin Eberlei
0c1cf853fc Address PHPStan issues. 2024-12-08 12:56:38 +01:00
Benjamin Eberlei
79d1f07fa2 Deprecate access to ClassMetadata::$reflFields. 2024-12-08 12:56:38 +01:00
Benjamin Eberlei
eba01f8d0e Style, missing getReflectionProperties()Property() that were renamed. 2024-12-08 12:56:38 +01:00
Benjamin Eberlei
bd292481bd Adjust test. 2024-12-08 12:56:38 +01:00
Benjamin Eberlei
fcc53b260f Use ClassMetadata::$propertyAccessors in all places. 2024-12-08 12:56:34 +01:00
Benjamin Eberlei
7d61a1e73f Fixes in LegacyReflectionFields. 2024-12-08 12:56:08 +01:00
Benjamin Eberlei
b3cffe2d12 Introduce LegacyReflectionFields abstraction, deriving from propertyAccessors at runtime. 2024-12-08 12:56:06 +01:00
Benjamin Eberlei
052c7d7698 Add all necessary accessors, adapting doctrine/persistence and ORM internal reflection properties. no tests. 2024-12-08 12:55:14 +01:00
Benjamin Eberlei
c2713adebc property hooks. 2024-12-08 12:55:10 +01:00
Grégoire Paris
51a984be3d Merge pull request #11758 from doctrine/3.3.x
Merge 3.3.x up into 3.4.x
2024-12-08 12:42:02 +01:00
Grégoire Paris
6007154484 Merge pull request #11746 from greg0ire/3.4.x
Merge 2.21.x up into 3.4.x
2024-12-04 07:52:51 +01:00
Grégoire Paris
22ce0aff37 Merge remote-tracking branch 'origin/2.21.x' into 3.4.x 2024-12-03 23:44:05 +01:00
Grégoire Paris
37051d57ce Merge pull request #11739 from doctrine/2.20.x
Merge 2.20.x up into 2.21.x
2024-11-28 08:23:12 +01:00
Grégoire Paris
4563f2f9a7 Merge pull request #11737 from doctrine/2.20.x
Merge 2.20.x up into 2.21.x
2024-11-27 22:10:21 +01:00
Grégoire Paris
91201c094a Merge pull request #11722 from doctrine/2.20.x
Merge 2.20.x up into 2.21.x
2024-11-23 19:35:45 +01:00
Grégoire Paris
a4a15ad243 Merge pull request #11687 from doctrine/2.20.x
Merge 2.20.x up into 2.21.x
2024-10-16 23:37:08 +02:00
78 changed files with 2767 additions and 323 deletions

View File

@@ -43,17 +43,27 @@ jobs:
- "pdo_sqlite"
deps:
- "highest"
lazy_proxy:
- "0"
include:
- php-version: "8.2"
dbal-version: "4@dev"
extension: "pdo_sqlite"
lazy_proxy: "0"
- php-version: "8.2"
dbal-version: "4@dev"
extension: "sqlite3"
lazy_proxy: "0"
- php-version: "8.1"
dbal-version: "default"
deps: "lowest"
extension: "pdo_sqlite"
lazy_proxy: "0"
- php-version: "8.4"
dbal-version: "default"
deps: "highest"
extension: "pdo_sqlite"
lazy_proxy: "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_LAZY_PROXY: ${{ matrix.lazy_proxy }}
- 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_LAZY_PROXY: ${{ matrix.lazy_proxy }}
- 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.lazy_proxy }}-coverage"
path: "coverage*.xml"

View File

@@ -1,8 +1,21 @@
# 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`

View File

@@ -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:

View File

@@ -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)

View File

@@ -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

View File

@@ -674,6 +674,27 @@ 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'}
In a Dto, if you want add all fields of an entity, you can use ``.*`` :
.. code-block:: php
<?php
$query = $em->createQuery('SELECT NEW NAMED CustomerDTO(c.name, NEW NAMED AddressDTO(a.*) AS address) FROM Customer c JOIN c.address a');
$users = $query->getResult(); // array of CustomerDTO
// CustomerDTO => {name : 'DOE', email: null, city: null, address: {id: 18, city: 'New York', zip: '10011'}}
It's recommended to use named arguments DTOs with the ``.*`` notation because argument order is not guaranteed.
Using INDEX BY
~~~~~~~~~~~~~~
@@ -1697,12 +1718,14 @@ 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]) | AllFieldsExpression
EntityAsDtoArgumentExpression ::= IdentificationVariable
AllFieldsExpression ::= IdentificationVariable ".*"
Conditional Expressions
~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -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
@@ -2811,7 +2811,7 @@ parameters:
-
message: '#^Cannot assign new offset to list\<string\>\|string\.$#'
identifier: offsetAssign.dimType
count: 2
count: 3
path: src/Query/SqlWalker.php
-
@@ -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

View File

@@ -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
*

View File

@@ -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.
*

View File

@@ -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.

View File

@@ -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);

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -684,6 +684,7 @@ class AttributeDriver implements MappingDriver
{
$mapping = [
'name' => $joinColumn->name,
'deferrable' => $joinColumn->deferrable,
'unique' => $joinColumn->unique,
'nullable' => $joinColumn->nullable,
'onDelete' => $joinColumn->onDelete,

View File

@@ -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,

View File

@@ -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;
}

View File

@@ -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,

View 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);
}
}
}

View File

@@ -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(),
);
}
}

View File

@@ -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,
));
}
}

View 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();
}
}

View 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();
}
}

View 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;
}
}

View 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;
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View File

@@ -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;
}
}

View File

@@ -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();
}
}
}

View File

@@ -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,
);

View File

@@ -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[] = [

View File

@@ -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)

View File

@@ -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.
@@ -142,11 +147,11 @@ EOPHP;
private readonly string $proxyNs,
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();
}
@@ -163,8 +168,23 @@ 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);
$proxy = $classMetadata->reflClass->newLazyGhost(static function (object $object) use ($identifier, $entityPersister): void {
$entityPersister->loadById($identifier, $object);
}, 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 +202,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 +256,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 +286,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 +311,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 {

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Doctrine\ORM\Query\AST;
use Doctrine\ORM\Query\SqlWalker;
/**
* AllFieldsExpression ::= u.*
*
* @link www.doctrine-project.org
*/
class AllFieldsExpression extends Node
{
public string $field = '';
public function __construct(
public string|null $identificationVariable,
) {
$this->field = $this->identificationVariable . '.*';
}
public function dispatch(SqlWalker $walker, int|string $parent = '', int|string $argIndex = '', int|null &$aliasGap = null): string
{
return $walker->walkAllEntityFieldsExpression($this, $parent, $argIndex, $aliasGap);
}
}

View 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);
}
}

View File

@@ -20,7 +20,7 @@ class NewObjectExpression extends Node
* @param class-string $className
* @param mixed[] $args
*/
public function __construct(public string $className, public array $args)
public function __construct(public string $className, public array $args, public bool $hasNamedArgs = false)
{
}

View File

@@ -1036,6 +1036,7 @@ final class Parser
assert($this->lexer->token !== null);
if ($this->lexer->isNextToken(TokenType::T_DOT)) {
$this->match(TokenType::T_DOT);
$this->match(TokenType::T_IDENTIFIER);
$field = $this->lexer->token->value;
@@ -1106,6 +1107,64 @@ 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);
}
/**
* AllFieldsExpression ::= IdentificationVariable
*/
public function AllFieldsExpression(): AST\AllFieldsExpression
{
$identVariable = $this->IdentificationVariable();
assert($this->lexer->token !== null);
$this->match(TokenType::T_DOT);
$this->match(TokenType::T_MULTIPLY);
return new AST\AllFieldsExpression($identVariable);
}
/**
* SelectClause ::= "SELECT" ["DISTINCT"] SelectExpression {"," SelectExpression}
*/
@@ -1782,7 +1841,7 @@ final class Parser
$this->match(TokenType::T_CLOSE_PARENTHESIS);
$expression = new AST\NewObjectExpression($className, $args);
$expression = new AST\NewObjectExpression($className, $args, $useNamedArguments);
// Defer NewObjectExpression validation
$this->deferredNewObjectExpressions[] = [
@@ -1829,7 +1888,7 @@ final class Parser
}
/**
* NewObjectArg ::= (ScalarExpression | "(" Subselect ")" | NewObjectExpression) ["AS" AliasResultVariable]
* NewObjectArg ::= ((ScalarExpression | "(" Subselect ")" | NewObjectExpression) ["AS" AliasResultVariable]) | AllFieldsExpression
*/
public function NewObjectArg(string|null &$fieldAlias = null): mixed
{
@@ -1849,6 +1908,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();
}
@@ -1939,10 +2000,14 @@ final class Parser
// it is no function, so it must be a field path
case $lookahead === TokenType::T_IDENTIFIER:
$this->lexer->peek(); // lookahead => '.'
$this->lexer->peek(); // lookahead => token after '.'
$peek = $this->lexer->peek(); // lookahead => token after the token after the '.'
$token = $this->lexer->peek(); // lookahead => token after '.'
$peek = $this->lexer->peek(); // lookahead => token after the token after the '.'
$this->lexer->resetPeek();
if ($token->value === '*') {
return $this->AllFieldsExpression();
}
if ($this->isMathOperator($peek)) {
return $this->SimpleArithmeticExpression();
}

View File

@@ -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.
*

View File

@@ -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
@@ -1499,11 +1518,17 @@ class SqlWalker
{
$sqlSelectExpressions = [];
$objIndex = $newObjectResultAlias ?: $this->newObjectCounter++;
$aliasGap = $newObjectExpression->hasNamedArgs ? null : 0;
foreach ($newObjectExpression->args as $argIndex => $e) {
$resultAlias = $this->scalarResultCounter++;
$columnAlias = $this->getSQLColumnAlias('sclr');
$fieldType = 'string';
if (! $newObjectExpression->hasNamedArgs) {
$argIndex += $aliasGap;
}
$resultAlias = $this->scalarResultCounter++;
$columnAlias = $this->getSQLColumnAlias('sclr');
$fieldType = 'string';
$isScalarResult = true;
switch (true) {
case $e instanceof AST\NewObjectExpression:
@@ -1549,18 +1574,34 @@ 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;
case $e instanceof AST\AllFieldsExpression:
$isScalarResult = false;
$sqlSelectExpressions[] = $e->dispatch($this, $objIndex, $argIndex, $aliasGap);
break;
default:
$sqlSelectExpressions[] = trim($e->dispatch($this)) . ' AS ' . $columnAlias;
break;
}
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
$this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
if ($isScalarResult) {
$this->scalarResultAliasMap[$resultAlias] = $columnAlias;
$this->rsm->addScalarResult($columnAlias, $resultAlias, $fieldType);
$this->rsm->newObjectMappings[$columnAlias] = [
'objIndex' => $objIndex,
'argIndex' => $argIndex,
];
$this->rsm->newObjectMappings[$columnAlias] = [
'className' => $newObjectExpression->className,
'objIndex' => $objIndex,
'argIndex' => $argIndex,
];
}
}
$this->rsm->newObject[$objIndex] = $newObjectExpression->className;
@@ -2265,6 +2306,42 @@ class SqlWalker
return $resultAlias;
}
public function walkAllEntityFieldsExpression(AST\AllFieldsExpression $expression, int|string $objIndex, int|string $argIndex, int|null &$aliasGap): string
{
$dqlAlias = $expression->identificationVariable;
$class = $this->getMetadataForDqlAlias($expression->identificationVariable);
$sqlParts = [];
// Select all fields from the queried class
foreach ($class->fieldMappings as $fieldName => $mapping) {
$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[$objIndex][] = $columnAlias;
$this->rsm->addScalarResult($columnAlias, $objIndex, $mapping->type);
$this->rsm->newObjectMappings[$columnAlias] = [
'objIndex' => $objIndex,
'argIndex' => $aliasGap === null ? $fieldName : (int) $argIndex + $aliasGap++,
];
}
return implode(', ', $sqlParts);
}
/**
* @return string The list in parentheses of valid child discriminators from the given class
*

View File

@@ -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:

View File

@@ -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;

View File

@@ -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.

View File

@@ -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)) {

View File

@@ -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)) {
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;

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
namespace Doctrine\Tests\Models\CMS;
class CmsDumbVariadicDTO
{
private array $values = [];
public function __construct(...$args)
{
foreach ($args as $key => $val) {
$this->values[$key] = $val;
}
}
public function __get(string $key): mixed
{
return $this->values[$key] ?? null;
}
}

View 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);
}
}
}

View 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);
}
}
}

View File

@@ -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);
}

View File

@@ -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();

View 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);
}
}

View File

@@ -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

View File

@@ -12,6 +12,7 @@ use Doctrine\Tests\Models\CMS\CmsAddress;
use Doctrine\Tests\Models\CMS\CmsAddressDTO;
use Doctrine\Tests\Models\CMS\CmsAddressDTONamedArgs;
use Doctrine\Tests\Models\CMS\CmsDumbDTO;
use Doctrine\Tests\Models\CMS\CmsDumbVariadicDTO;
use Doctrine\Tests\Models\CMS\CmsEmail;
use Doctrine\Tests\Models\CMS\CmsPhonenumber;
use Doctrine\Tests\Models\CMS\CmsUser;
@@ -1230,6 +1231,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'
@@ -1407,6 +1572,290 @@ class NewOperatorTest extends OrmFunctionalTestCase
self::assertSame($this->fixtures[2]->username, $result[2]['cmsUserUsername']);
}
public function testShouldSupportNestedNewOperatorsWithAllFieldsForDto(): void
{
$dql = '
SELECT
new CmsDumbDTO(
u.*
)
FROM
Doctrine\Tests\Models\CMS\CmsUser u
JOIN
u.email e
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]->status, $result[0]->val2);
self::assertSame($this->fixtures[1]->status, $result[1]->val2);
self::assertSame($this->fixtures[2]->status, $result[2]->val2);
self::assertSame($this->fixtures[0]->username, $result[0]->val3);
self::assertSame($this->fixtures[1]->username, $result[1]->val3);
self::assertSame($this->fixtures[2]->username, $result[2]->val3);
self::assertSame($this->fixtures[0]->name, $result[0]->val4);
self::assertSame($this->fixtures[1]->name, $result[1]->val4);
self::assertSame($this->fixtures[2]->name, $result[2]->val4);
}
public function testShouldSupportNestedNewOperatorsWithAllFieldsForNamedDto(): void
{
$dql = '
SELECT
new NAMED CmsDumbVariadicDTO(
u.*
)
FROM
Doctrine\Tests\Models\CMS\CmsUser u
JOIN
u.email e
JOIN
u.address a
ORDER BY
u.name';
$query = $this->getEntityManager()->createQuery($dql);
$result = $query->getResult();
self::assertCount(3, $result);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[0]);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[1]);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[2]);
self::assertSame($this->fixtures[0]->status, $result[0]->status);
self::assertSame($this->fixtures[1]->status, $result[1]->status);
self::assertSame($this->fixtures[2]->status, $result[2]->status);
self::assertSame($this->fixtures[0]->username, $result[0]->username);
self::assertSame($this->fixtures[1]->username, $result[1]->username);
self::assertSame($this->fixtures[2]->username, $result[2]->username);
self::assertSame($this->fixtures[0]->name, $result[0]->name);
self::assertSame($this->fixtures[1]->name, $result[1]->name);
self::assertSame($this->fixtures[2]->name, $result[2]->name);
}
public function testShouldSupportNestedNewOperatorsWithMultipleAllFieldsForNamedDto(): void
{
$dql = '
SELECT
new NAMED CmsDumbVariadicDTO(
u.*, a.*
)
FROM
Doctrine\Tests\Models\CMS\CmsUser u
JOIN
u.email e
JOIN
u.address a
ORDER BY
u.name';
$query = $this->getEntityManager()->createQuery($dql);
$result = $query->getResult();
self::assertCount(3, $result);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[0]);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[1]);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[2]);
self::assertSame($this->fixtures[0]->status, $result[0]->status);
self::assertSame($this->fixtures[1]->status, $result[1]->status);
self::assertSame($this->fixtures[2]->status, $result[2]->status);
self::assertSame($this->fixtures[0]->username, $result[0]->username);
self::assertSame($this->fixtures[1]->username, $result[1]->username);
self::assertSame($this->fixtures[2]->username, $result[2]->username);
self::assertSame($this->fixtures[0]->name, $result[0]->name);
self::assertSame($this->fixtures[1]->name, $result[1]->name);
self::assertSame($this->fixtures[2]->name, $result[2]->name);
self::assertSame($this->fixtures[0]->address->city, $result[0]->city);
self::assertSame($this->fixtures[1]->address->city, $result[1]->city);
self::assertSame($this->fixtures[2]->address->city, $result[2]->city);
self::assertSame($this->fixtures[0]->address->zip, $result[0]->zip);
self::assertSame($this->fixtures[1]->address->zip, $result[1]->zip);
self::assertSame($this->fixtures[2]->address->zip, $result[2]->zip);
self::assertSame($this->fixtures[0]->address->country, $result[0]->country);
self::assertSame($this->fixtures[1]->address->country, $result[1]->country);
self::assertSame($this->fixtures[2]->address->country, $result[2]->country);
}
public function testShouldSupportNestedNewOperatorsWithAllFieldsForNamedDtoWithOtherValues(): void
{
$dql = '
SELECT
new NAMED CmsDumbVariadicDTO(
u.*, e.email, a.zip, a.country
)
FROM
Doctrine\Tests\Models\CMS\CmsUser u
JOIN
u.email e
JOIN
u.address a
ORDER BY
u.name';
$query = $this->getEntityManager()->createQuery($dql);
$result = $query->getResult();
self::assertCount(3, $result);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[0]);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[1]);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[2]);
self::assertSame($this->fixtures[0]->status, $result[0]->status);
self::assertSame($this->fixtures[1]->status, $result[1]->status);
self::assertSame($this->fixtures[2]->status, $result[2]->status);
self::assertSame($this->fixtures[0]->username, $result[0]->username);
self::assertSame($this->fixtures[1]->username, $result[1]->username);
self::assertSame($this->fixtures[2]->username, $result[2]->username);
self::assertSame($this->fixtures[0]->name, $result[0]->name);
self::assertSame($this->fixtures[1]->name, $result[1]->name);
self::assertSame($this->fixtures[2]->name, $result[2]->name);
self::assertSame($this->fixtures[0]->email->email, $result[0]->email);
self::assertSame($this->fixtures[1]->email->email, $result[1]->email);
self::assertSame($this->fixtures[2]->email->email, $result[2]->email);
self::assertSame($this->fixtures[0]->address->zip, $result[0]->zip);
self::assertSame($this->fixtures[1]->address->zip, $result[1]->zip);
self::assertSame($this->fixtures[2]->address->zip, $result[2]->zip);
self::assertSame($this->fixtures[0]->address->country, $result[0]->country);
self::assertSame($this->fixtures[1]->address->country, $result[1]->country);
self::assertSame($this->fixtures[2]->address->country, $result[2]->country);
}
public function testShouldSupportNestedNewOperatorsWithAllFieldsForNestedDto(): void
{
$dql = '
SELECT
new CmsDumbDTO(
u.name,
e.email,
new CmsDumbDTO(
a.*
) as address
)
FROM
Doctrine\Tests\Models\CMS\CmsUser u
JOIN
u.email e
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]->val1);
self::assertSame($this->fixtures[1]->name, $result[1]->val1);
self::assertSame($this->fixtures[2]->name, $result[2]->val1);
self::assertSame($this->fixtures[0]->email->email, $result[0]->val2);
self::assertSame($this->fixtures[1]->email->email, $result[1]->val2);
self::assertSame($this->fixtures[2]->email->email, $result[2]->val2);
self::assertInstanceOf(CmsDumbDTO::class, $result[0]->val3);
self::assertInstanceOf(CmsDumbDTO::class, $result[1]->val3);
self::assertInstanceOf(CmsDumbDTO::class, $result[2]->val3);
self::assertSame($this->fixtures[0]->address->country, $result[0]->val3->val2);
self::assertSame($this->fixtures[1]->address->country, $result[1]->val3->val2);
self::assertSame($this->fixtures[2]->address->country, $result[2]->val3->val2);
self::assertSame($this->fixtures[0]->address->zip, $result[0]->val3->val3);
self::assertSame($this->fixtures[1]->address->zip, $result[1]->val3->val3);
self::assertSame($this->fixtures[2]->address->zip, $result[2]->val3->val3);
self::assertSame($this->fixtures[0]->address->city, $result[0]->val3->val4);
self::assertSame($this->fixtures[1]->address->city, $result[1]->val3->val4);
self::assertSame($this->fixtures[2]->address->city, $result[2]->val3->val4);
}
public function testShouldSupportNestedNewOperatorsWithAllFieldsForNestedNamedDto(): void
{
$dql = '
SELECT
new CmsDumbDTO(
u.name,
e.email,
new NAMED CmsDumbVariadicDTO(
a.*
) as address
)
FROM
Doctrine\Tests\Models\CMS\CmsUser u
JOIN
u.email e
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]->val1);
self::assertSame($this->fixtures[1]->name, $result[1]->val1);
self::assertSame($this->fixtures[2]->name, $result[2]->val1);
self::assertSame($this->fixtures[0]->email->email, $result[0]->val2);
self::assertSame($this->fixtures[1]->email->email, $result[1]->val2);
self::assertSame($this->fixtures[2]->email->email, $result[2]->val2);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[0]->val3);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[1]->val3);
self::assertInstanceOf(CmsDumbVariadicDTO::class, $result[2]->val3);
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[2]->address->city, $result[2]->val3->city);
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->zip, $result[2]->val3->zip);
self::assertSame($this->fixtures[0]->address->zip, $result[0]->val3->zip);
self::assertSame($this->fixtures[1]->address->zip, $result[1]->val3->zip);
}
public function testVariadicArgument(): void
{
$dql = <<<'SQL'

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional;
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->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);
}
}

View File

@@ -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,

View File

@@ -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));
}
}

View File

@@ -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;
}

View File

@@ -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());

View File

@@ -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());
}
}

View File

@@ -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');
}

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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();

View File

@@ -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')]

View File

@@ -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

View File

@@ -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

View File

@@ -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);

View File

@@ -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],
],
],
);
}
}

View File

@@ -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());
}
}

View File

@@ -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'],
];
}

View File

@@ -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';
}

View File

@@ -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;
}
}

View File

@@ -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));
}
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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>

View File

@@ -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>

View File

@@ -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();
}
}

View File

@@ -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,12 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
$this->isSecondLevelCacheEnabled = true;
}
$enableNativeLazyObjects = getenv('ENABLE_NATIVE_LAZY_OBJECTS');
if (PHP_VERSION_ID >= 80400 && $enableNativeLazyObjects) {
$config->enableNativeLazyObjects(true);
}
$config->setMetadataDriverImpl(
$mappingDriver ?? new AttributeDriver([
realpath(__DIR__ . '/Models/Cache'),
@@ -1120,4 +1127,9 @@ abstract class OrmFunctionalTestCase extends OrmTestCase
{
return $this->_em->getUnitOfWork()->isUninitializedObject($entity);
}
final protected function initializeObject(object $entity): void
{
$this->_em->getUnitOfWork()->initializeObject($entity);
}
}