-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix matching lazy collections with enums criteria #11516
Open
kira0269
wants to merge
6
commits into
doctrine:3.2.x
Choose a base branch
from
kira0269:fix_issue_11481
base: 3.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3e3de05
Fix issue #11481
e9fa426
Apply review's suggestions
7d9904a
Update ResolveValuesHelper.php
kira0269 6ec521b
Update ManyToManyPersister.php
kira0269 480b362
Fix pipeline errors
adf4a53
Update psalm-baseline.xml
kira0269 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\ORM\Persisters\Traits; | ||
|
||
use BackedEnum; | ||
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver; | ||
|
||
use function array_merge; | ||
use function is_array; | ||
use function is_object; | ||
|
||
/** @internal */ | ||
trait ResolveValuesHelper | ||
kira0269 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* Retrieves the parameters that identifies a value. | ||
* | ||
* @psalm-return list<mixed> | ||
*/ | ||
private function getValues(mixed $value): array | ||
{ | ||
if (is_array($value)) { | ||
$newValues = []; | ||
|
||
foreach ($value as $itemValue) { | ||
$newValues[] = $this->getValues($itemValue); | ||
} | ||
|
||
return [array_merge(...$newValues)]; | ||
} | ||
|
||
return $this->getIndividualValue($value); | ||
} | ||
|
||
/** | ||
* Retrieves an individual parameter value. | ||
* | ||
* @psalm-return list<mixed> | ||
*/ | ||
private function getIndividualValue(mixed $value): array | ||
{ | ||
if (! is_object($value)) { | ||
return [$value]; | ||
} | ||
|
||
if ($value instanceof BackedEnum) { | ||
return [$value->value]; | ||
} | ||
|
||
$valueClass = DefaultProxyClassNameResolver::getClass($value); | ||
|
||
if ($this->em->getMetadataFactory()->isTransient($valueClass)) { | ||
return [$value]; | ||
} | ||
|
||
$class = $this->em->getClassMetadata($valueClass); | ||
|
||
if ($class->isIdentifierComposite) { | ||
$newValues = []; | ||
|
||
foreach ($class->getIdentifierValues($value) as $innerValue) { | ||
$newValues[] = $this->getValues($innerValue); | ||
} | ||
|
||
return array_merge(...$newValues); | ||
} | ||
|
||
return [$this->em->getUnitOfWork()->getSingleIdentifierValue($value)]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Enums; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\JoinColumn; | ||
use Doctrine\ORM\Mapping\ManyToMany; | ||
use Doctrine\ORM\Mapping\ManyToOne; | ||
use Doctrine\ORM\Mapping\Table; | ||
|
||
#[Entity] | ||
#[Table(name: 'books')] | ||
class Book | ||
{ | ||
#[Id] | ||
#[GeneratedValue] | ||
#[Column] | ||
public int $id; | ||
|
||
#[ManyToOne(targetEntity: Library::class, inversedBy: 'books')] | ||
#[JoinColumn(name: 'library_id', referencedColumnName: 'id')] | ||
public Library $library; | ||
|
||
#[Column(enumType: BookColor::class)] | ||
public BookColor $bookColor; | ||
|
||
#[ManyToMany(targetEntity: BookCategory::class, mappedBy: 'books')] | ||
public Collection $categories; | ||
|
||
public function __construct() | ||
{ | ||
$this->categories = new ArrayCollection(); | ||
} | ||
|
||
public function setLibrary(Library $library): void | ||
{ | ||
$this->library = $library; | ||
} | ||
|
||
public function addCategory(BookCategory $bookCategory): void | ||
{ | ||
$this->categories->add($bookCategory); | ||
$bookCategory->addBook($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Enums; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\Common\Collections\Criteria; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\GeneratedValue; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\ManyToMany; | ||
|
||
#[Entity] | ||
class BookCategory | ||
{ | ||
#[Id] | ||
#[Column] | ||
#[GeneratedValue] | ||
public int $id; | ||
|
||
#[Column] | ||
public string $name; | ||
|
||
#[ManyToMany(targetEntity: Book::class, inversedBy: 'categories')] | ||
public Collection $books; | ||
|
||
public function __construct() | ||
{ | ||
$this->books = new ArrayCollection(); | ||
} | ||
|
||
public function addBook(Book $book): void | ||
{ | ||
$this->books->add($book); | ||
} | ||
|
||
public function getBooks(): Collection | ||
{ | ||
return $this->books; | ||
} | ||
|
||
public function getBooksWithColor(BookColor $bookColor): Collection | ||
{ | ||
$criteria = Criteria::create() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer if you move this code into the test, because reading the test code now its not easy to grasp how test name matches to code. Same for the other Criteria code in Library. |
||
->andWhere(Criteria::expr()->eq('bookColor', $bookColor)); | ||
|
||
return $this->books->matching($criteria); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\Models\Enums; | ||
|
||
enum BookColor: string | ||
{ | ||
case RED = 'red'; | ||
case BLUE = 'blue'; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove this
unset
, its not needed.