Skip to content

Commit 6982c8a

Browse files
authored
Merge pull request #12284 from doctrine/3.5.x
Merge 3.5.x up into 3.6.x
2 parents f0562f4 + 3df11d5 commit 6982c8a

File tree

9 files changed

+92
-31
lines changed

9 files changed

+92
-31
lines changed

docs/en/reference/basic-mapping.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,21 @@ Here is a complete list of ``Column``s attributes (all optional):
182182
- ``options``: Key-value pairs of options that get passed
183183
to the underlying database platform when generating DDL statements.
184184

185+
Specifying default values
186+
~~~~~~~~~~~~~~~~~~~~~~~~~
187+
188+
While it is possible to specify default values for properties in your
189+
PHP class, Doctrine also allows you to specify default values for
190+
database columns using the ``default`` key in the ``options`` array of
191+
the ``Column`` attribute.
192+
193+
.. configuration-block::
194+
.. literalinclude:: basic-mapping/DefaultValues.php
195+
:language: attribute
196+
197+
.. literalinclude:: basic-mapping/default-values.xml
198+
:language: xml
199+
185200
.. _reference-php-mapping-types:
186201

187202
PHP Types Mapping
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Entity;
6+
7+
use Doctrine\ORM\Mapping\Column;
8+
use Doctrine\ORM\Mapping\Entity;
9+
10+
#[Entity]
11+
class Message
12+
{
13+
#[Column(options: ['default' => 'Hello World!'])]
14+
private string $text;
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<doctrine-mapping>
2+
<entity name="Message">
3+
<field name="text">
4+
<options>
5+
<option name="default">Hello World!</option>
6+
</options>
7+
</field>
8+
</entity>
9+
</doctrine-mapping>

docs/en/reference/faq.rst

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,6 @@ In your mapping configuration, the column definition (for example, the
1818
the ``charset`` and ``collation``. The default values are ``utf8`` and
1919
``utf8_unicode_ci``, respectively.
2020

21-
Entity Classes
22-
--------------
23-
24-
How can I add default values to a column?
25-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26-
27-
Doctrine does not support to set the default values in columns through the "DEFAULT" keyword in SQL.
28-
This is not necessary however, you can just use your class properties as default values. These are then used
29-
upon insert:
30-
31-
.. code-block:: php
32-
33-
class User
34-
{
35-
private const STATUS_DISABLED = 0;
36-
private const STATUS_ENABLED = 1;
37-
38-
private string $algorithm = "sha1";
39-
/** @var self::STATUS_* */
40-
private int $status = self::STATUS_DISABLED;
41-
}
42-
43-
.
44-
4521
Mapping
4622
-------
4723

phpstan-baseline.neon

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3345,6 +3345,12 @@ parameters:
33453345
count: 1
33463346
path: src/UnitOfWork.php
33473347

3348+
-
3349+
message: '#^Access to an undefined property Doctrine\\ORM\\Mapping\\AssociationMapping\:\:\$joinColumns\.$#'
3350+
identifier: property.notFound
3351+
count: 1
3352+
path: src/UnitOfWork.php
3353+
33483354
-
33493355
message: '#^Access to an undefined property Doctrine\\ORM\\Mapping\\ManyToManyInverseSideMapping\|Doctrine\\ORM\\Mapping\\ManyToManyOwningSideMapping\|Doctrine\\ORM\\Mapping\\ManyToOneAssociationMapping\|Doctrine\\ORM\\Mapping\\OneToManyAssociationMapping\|Doctrine\\ORM\\Mapping\\OneToOneInverseSideMapping\|Doctrine\\ORM\\Mapping\\OneToOneOwningSideMapping\:\:\$inversedBy\.$#'
33503356
identifier: property.notFound
@@ -3360,7 +3366,7 @@ parameters:
33603366
-
33613367
message: '#^Access to an undefined property Doctrine\\ORM\\Mapping\\ManyToManyInverseSideMapping\|Doctrine\\ORM\\Mapping\\ManyToManyOwningSideMapping\|Doctrine\\ORM\\Mapping\\ManyToOneAssociationMapping\|Doctrine\\ORM\\Mapping\\OneToManyAssociationMapping\|Doctrine\\ORM\\Mapping\\OneToOneInverseSideMapping\|Doctrine\\ORM\\Mapping\\OneToOneOwningSideMapping\:\:\$mappedBy\.$#'
33623368
identifier: property.notFound
3363-
count: 1
3369+
count: 3
33643370
path: src/UnitOfWork.php
33653371

33663372
-

src/UnitOfWork.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
use function array_sum;
6363
use function array_values;
6464
use function assert;
65+
use function count;
6566
use function current;
6667
use function get_debug_type;
6768
use function implode;
@@ -2594,8 +2595,10 @@ public function createEntity(string $className, array $data, array &$hints = [])
25942595
$reflField->setValue($entity, $pColl);
25952596

25962597
if ($hints['fetchMode'][$class->name][$field] === ClassMetadata::FETCH_EAGER) {
2597-
$isIteration = isset($hints[Query::HINT_INTERNAL_ITERATION]) && $hints[Query::HINT_INTERNAL_ITERATION];
2598-
if (! $isIteration && $assoc->isOneToMany() && ! $targetClass->isIdentifierComposite && ! $assoc->isIndexed()) {
2598+
$isIteration = isset($hints[Query::HINT_INTERNAL_ITERATION]) && $hints[Query::HINT_INTERNAL_ITERATION];
2599+
$isForeignKeyComposite = $assoc->isOneToMany() && $targetClass->hasAssociation($assoc->mappedBy) && count($targetClass->getAssociationMapping($assoc->mappedBy)->joinColumns) > 1;
2600+
2601+
if (! $isIteration && $assoc->isOneToMany() && ! $isForeignKeyComposite && ! $assoc->isIndexed()) {
25992602
$this->scheduleCollectionForBatchLoading($pColl, $class);
26002603
} else {
26012604
$this->loadCollection($pColl);

tests/Tests/Models/EagerFetchedCompositeOneToMany/RootEntity.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ class RootEntity
2424
#[ORM\OneToMany(mappedBy: 'root', targetEntity: SecondLevel::class, fetch: 'EAGER')]
2525
private Collection $secondLevel;
2626

27+
/** @var Collection<int, SecondLevelWithoutCompositePrimaryKey> */
28+
#[ORM\OneToMany(mappedBy: 'root', targetEntity: SecondLevelWithoutCompositePrimaryKey::class, fetch: 'EAGER')]
29+
private Collection $anotherSecondLevel;
30+
2731
public function __construct(int $id, string $other)
2832
{
29-
$this->otherKey = $other;
30-
$this->secondLevel = new ArrayCollection();
31-
$this->id = $id;
33+
$this->otherKey = $other;
34+
$this->secondLevel = new ArrayCollection();
35+
$this->anotherSecondLevel = new ArrayCollection();
36+
$this->id = $id;
3237
}
3338

3439
public function getId(): int|null
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Tests\Models\EagerFetchedCompositeOneToMany;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
9+
#[ORM\Entity]
10+
class SecondLevelWithoutCompositePrimaryKey
11+
{
12+
#[ORM\Id]
13+
#[ORM\GeneratedValue]
14+
#[ORM\Column(type: 'integer', nullable: false)]
15+
private int|null $id;
16+
17+
#[ORM\ManyToOne(targetEntity: RootEntity::class, inversedBy: 'anotherSecondLevel')]
18+
#[ORM\JoinColumn(name: 'root_id', referencedColumnName: 'id')]
19+
#[ORM\JoinColumn(name: 'root_other_key', referencedColumnName: 'other_key')]
20+
private RootEntity $root;
21+
22+
public function __construct(RootEntity $upper)
23+
{
24+
$this->root = $upper;
25+
}
26+
27+
public function getId(): int|null
28+
{
29+
return $this->id;
30+
}
31+
}

tests/Tests/ORM/Functional/EagerFetchOneToManyWithCompositeKeyTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Doctrine\Tests\Models\EagerFetchedCompositeOneToMany\RootEntity;
88
use Doctrine\Tests\Models\EagerFetchedCompositeOneToMany\SecondLevel;
9+
use Doctrine\Tests\Models\EagerFetchedCompositeOneToMany\SecondLevelWithoutCompositePrimaryKey;
910
use Doctrine\Tests\OrmFunctionalTestCase;
1011
use PHPUnit\Framework\Attributes\Group;
1112

@@ -14,7 +15,7 @@ final class EagerFetchOneToManyWithCompositeKeyTest extends OrmFunctionalTestCas
1415
#[Group('GH11154')]
1516
public function testItDoesNotThrowAnExceptionWhenTriggeringALoad(): void
1617
{
17-
$this->setUpEntitySchema([RootEntity::class, SecondLevel::class]);
18+
$this->setUpEntitySchema([RootEntity::class, SecondLevel::class, SecondLevelWithoutCompositePrimaryKey::class]);
1819

1920
$a1 = new RootEntity(1, 'A');
2021

0 commit comments

Comments
 (0)