Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45214f7

Browse files
authored
fix: revert adding PersistManager::findBy() (#996)
1 parent b36b9b3 commit 45214f7

9 files changed

Lines changed: 110 additions & 114 deletions

File tree

src/Mongo/MongoPersistenceStrategy.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -95,32 +95,4 @@ public function isScheduledForInsert(object $object): bool
9595

9696
return $uow->isScheduledForInsert($object) || $uow->isScheduledForUpsert($object);
9797
}
98-
99-
public function findBy(string $class, array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
100-
{
101-
$qb = $this->objectManagerFor($class)
102-
->getRepository($class)
103-
->createQueryBuilder()
104-
->refresh();
105-
106-
foreach ($criteria as $field => $value) {
107-
$qb->field($field)->equals($value);
108-
}
109-
110-
if ($orderBy) {
111-
foreach ($orderBy as $field => $direction) {
112-
$qb->sort($field, $direction);
113-
}
114-
}
115-
116-
if ($limit) {
117-
$qb->limit($limit);
118-
}
119-
120-
if ($offset) {
121-
$qb->skip($offset);
122-
}
123-
124-
return $qb->getQuery()->execute()->toArray(); // @phpstan-ignore method.nonObject
125-
}
12698
}

src/ORM/AbstractORMPersistenceStrategy.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,4 @@ final public function managedNamespaces(): array
9797

9898
return \array_values(\array_merge(...$namespaces));
9999
}
100-
101-
final public function findBy(string $class, array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
102-
{
103-
$qb = $this->objectManagerFor($class)->getRepository($class)->createQueryBuilder('o');
104-
105-
foreach ($criteria as $field => $value) {
106-
$paramName = \str_replace('.', '_', $field);
107-
if (\is_array($value)) {
108-
$qb->andWhere("o.{$field} IN(:{$paramName})");
109-
} else {
110-
$qb->andWhere("o.{$field} = :{$paramName}");
111-
}
112-
$qb->setParameter($paramName, $value);
113-
}
114-
115-
if ($orderBy) {
116-
foreach ($orderBy as $field => $direction) {
117-
$qb->addOrderBy('o.'.$field, $direction);
118-
}
119-
}
120-
121-
if ($limit) {
122-
$qb->setMaxResults($limit);
123-
}
124-
125-
if ($offset) {
126-
$qb->setFirstResult($offset);
127-
}
128-
129-
return $qb->getQuery()
130-
->setHint(Query::HINT_REFRESH, true)
131-
->getResult();
132-
}
133100
}

src/Persistence/PersistenceManager.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -419,23 +419,6 @@ public static function isOrmOnly(): bool
419419
})();
420420
}
421421

422-
/**
423-
* @template T of object
424-
*
425-
* @param class-string<T> $class
426-
* @param array<string, mixed> $criteria
427-
* @param array<string, string>|null $orderBy
428-
* @phpstan-param array<string, 'asc'|'desc'|'ASC'|'DESC'>|null $orderBy
429-
*
430-
* @return list<T>
431-
*/
432-
public function findBy(string $class, array $criteria = [], ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array
433-
{
434-
$class = ProxyGenerator::unwrap($class);
435-
436-
return $this->strategyFor($class)->findBy($class, $criteria, $orderBy, $limit, $offset);
437-
}
438-
439422
private function flushAllStrategies(): void
440423
{
441424
foreach ($this->strategies as $strategy) {

src/Persistence/PersistenceStrategy.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,6 @@ public function getIdentifierValues(object $object): array
9292
*/
9393
abstract public function managedNamespaces(): array;
9494

95-
/**
96-
* Uses a query builder to be able to pass hints to UoW and to force Doctrine to return fresh objects.
97-
*
98-
* @template T of object
99-
*
100-
* @param class-string<T> $class
101-
* @param array<string, mixed> $criteria
102-
* @param array<string, string>|null $orderBy
103-
* @phpstan-param array<string, 'asc'|'desc'|'ASC'|'DESC'>|null $orderBy
104-
*
105-
* @return list<T>
106-
*/
107-
abstract public function findBy(string $class, array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null): array;
108-
10995
/**
11096
* @param class-string $owner
11197
*

src/Persistence/Proxy/PersistedObjectsTracker.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function add(object ...$objects): void
3939
{
4040
foreach ($objects as $object) {
4141
if (self::$buffer->offsetExists($object) && self::$buffer[$object]) {
42+
self::proxifyObject($object, self::$buffer[$object]);
43+
4244
continue;
4345
}
4446

@@ -78,16 +80,21 @@ private static function proxifyObjects(): void
7880
continue;
7981
}
8082

81-
$reflector = new \ReflectionClass($object);
83+
self::proxifyObject($object, $id);
84+
}
85+
}
8286

83-
if ($reflector->isUninitializedLazyObject($object)) {
84-
continue;
85-
}
87+
private static function proxifyObject(object $object, mixed $id): void
88+
{
89+
$reflector = new \ReflectionClass($object);
8690

87-
$clone = clone $object;
88-
$reflector->resetAsLazyGhost($object, function($object) use ($clone, $id) {
89-
Configuration::instance()->persistence()->autorefresh($object, $id, $clone);
90-
});
91+
if ($reflector->isUninitializedLazyObject($object)) {
92+
return;
9193
}
94+
95+
$clone = clone $object;
96+
$reflector->resetAsLazyGhost($object, function($object) use ($clone, $id) {
97+
Configuration::instance()->persistence()->autorefresh($object, $id, $clone);
98+
});
9299
}
93100
}

src/Persistence/RepositoryDecorator.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,7 @@ public function findAll(): array
133133
*/
134134
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
135135
{
136-
if ($this->inMemory) {
137-
$results = $this->inner()->findBy($this->normalize($criteria), $orderBy, $limit, $offset);
138-
} else {
139-
try {
140-
$results = Configuration::instance()->persistence()->findBy($this->class, $this->normalize($criteria), $orderBy, $limit, $offset);
141-
} catch (\LogicException|\Error) {
142-
// prevent entities/documents with readonly properties to create an error
143-
// LogicException is for ORM / Error is for ODM
144-
// @see https://github.com/doctrine/orm/issues/9505
145-
$results = $this->inner()->findBy($this->normalize($criteria), $orderBy, $limit, $offset);
146-
}
147-
}
148-
149-
$objects = \array_values($results);
136+
$objects = \array_values($this->inner()->findBy($this->normalize($criteria), $orderBy, $limit, $offset));
150137

151138
if (!$this instanceof ProxyRepositoryDecorator) {
152139
Configuration::instance()->persistedObjectsTracker?->add(...$objects);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\ManyToOneWithAutoGeneratedUlid;
15+
16+
use Doctrine\Common\Collections\ArrayCollection;
17+
use Doctrine\Common\Collections\Collection;
18+
use Doctrine\ORM\Mapping as ORM;
19+
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
20+
use Symfony\Bridge\Doctrine\Types\UlidType;
21+
use Symfony\Component\Uid\Ulid;
22+
23+
/**
24+
* @author Nicolas PHILIPPE <[email protected]>
25+
*/
26+
#[ORM\Entity]
27+
#[ORM\Table('many_to_one_with_auto_generated_ulid_inverse')]
28+
class InverseSide
29+
{
30+
#[ORM\Id]
31+
#[ORM\Column(type: UlidType::NAME, unique: true)]
32+
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
33+
#[ORM\CustomIdGenerator(class: UlidGenerator::class)]
34+
public ?Ulid $id = null;
35+
36+
/** @var Collection<int,OwningSide> */
37+
#[ORM\OneToMany(targetEntity: OwningSide::class, mappedBy: 'inverseSide', cascade: ['persist'])]
38+
public Collection $owningSides;
39+
40+
public function __construct() {
41+
$this->owningSides = new ArrayCollection();
42+
}
43+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the zenstruck/foundry package.
7+
*
8+
* (c) Kevin Bond <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\ManyToOneWithAutoGeneratedUlid;
15+
16+
use Doctrine\ORM\Mapping as ORM;
17+
use Symfony\Bridge\Doctrine\IdGenerator\UlidGenerator;
18+
use Symfony\Bridge\Doctrine\Types\UlidType;
19+
use Symfony\Component\Uid\Ulid;
20+
21+
/**
22+
* @author Nicolas PHILIPPE <[email protected]>
23+
*/
24+
#[ORM\Entity]
25+
#[ORM\Table('many_to_one_with_auto_generated_ulid_owning')]
26+
class OwningSide
27+
{
28+
#[ORM\Id]
29+
#[ORM\Column(type: UlidType::NAME, unique: true)]
30+
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
31+
#[ORM\CustomIdGenerator(class: UlidGenerator::class)]
32+
public ?Ulid $id = null;
33+
34+
#[ORM\ManyToOne(cascade: ['persist'], inversedBy: 'owningSides')]
35+
public ?InverseSide $inverseSide = null;
36+
}

tests/Integration/ORM/EdgeCasesRelationshipTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithoutNullable;
3232
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\InversedOneToOneWithSetter;
3333
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\ManyToOneToSelfReferencing;
34+
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\ManyToOneWithAutoGeneratedUlid;
3435
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\OneToManyWithUnionType;
3536
use Zenstruck\Foundry\Tests\Fixture\Entity\EdgeCases\RichDomainMandatoryRelationship;
3637
use Zenstruck\Foundry\Tests\Fixture\Factories\Entity\EdgeCases\MultipleMandatoryRelationshipToSameEntity;
@@ -304,6 +305,20 @@ static function(InversedOneToOneWithNonNullableOwning\OwningSide $o) use ($inver
304305
self::assertSame($owningSide, $owningSide->inverseSide->getOwningSide());
305306
}
306307

308+
/**
309+
* @test
310+
*/
311+
#[Test]
312+
public function it_can_find_and_object_with_ulid_as_id(): void
313+
{
314+
$this->expectNotToPerformAssertions();
315+
316+
$inverseSide = persistent_factory(ManyToOneWithAutoGeneratedUlid\InverseSide::class)->create();
317+
318+
persistent_factory(ManyToOneWithAutoGeneratedUlid\OwningSide::class)->many(2)->create(['inverseSide' => $inverseSide]);
319+
persistent_factory(ManyToOneWithAutoGeneratedUlid\OwningSide::class)::random(['inverseSide' => $inverseSide]);
320+
}
321+
307322
/**
308323
* @test
309324
*/

0 commit comments

Comments
 (0)