Skip to content
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

Merge 3.3.x up into 3.4.x #11724

Merged
merged 29 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0a49274
Update aggregate-fields.rst
d-ph Mar 8, 2024
129553d
Allow overriding association's cascade
malarzm Jul 11, 2024
e7efded
Update README.md
beberlei Oct 18, 2024
039b032
Inherit issue templates
greg0ire Oct 20, 2024
85fc950
Merge pull request #11691 from greg0ire/inherit-issue-templates
greg0ire Oct 20, 2024
4d821cb
Remove config.yml
greg0ire Oct 20, 2024
af54a16
Merge pull request #11692 from greg0ire/inherit-issue-templates
greg0ire Oct 20, 2024
021a9cc
UPGRADE: mention `SqlWalker` deprecations in 3.3 (#11693)
janedbal Oct 22, 2024
05f5486
Merge pull request #11549 from doctrine/feature/allow-overriding-cascade
greg0ire Oct 22, 2024
439b4da
Is not correctly generated sql when changed/switched sqlFilter parame…
dbannik Oct 22, 2024
3ca9529
Merge pull request #11694 from dbannik/Bug-join-sql-when-change-sqlFi…
greg0ire Oct 23, 2024
44fa8bb
Merge pull request #11344 from d-ph/patch-2
greg0ire Oct 24, 2024
9e2bfa8
Run tests against PostgreSQL 17 (#11697)
IndraGunawan Oct 25, 2024
4b03ec7
Refine Explanations of Doctrine orm Package Structure (#11710)
Speelwolf Nov 11, 2024
ff3ccff
Add `isEmpty()` method to the Extra Lazy Associations tutorial
acasademont Nov 13, 2024
d9aa6ef
Merge pull request #11716 from acasademont/patch-1
greg0ire Nov 14, 2024
43ce0be
lazy and eager collection refresh
goetas Sep 26, 2022
7d1b24f
attempt a fix
goetas Sep 26, 2022
486e406
Merge pull request #10065 from goetas/lazy-eager-collection-refresh
greg0ire Nov 14, 2024
4fbce94
Bump codecov/codecov-action from 4 to 5
dependabot[bot] Nov 18, 2024
ba11851
Merge pull request #11718 from doctrine/dependabot/github_actions/2.2…
greg0ire Nov 18, 2024
bb5b2a3
Ignore deprecation about StaticReflectionService
greg0ire Nov 18, 2024
f140651
Merge pull request #11719 from greg0ire/ignore-deprecation
greg0ire Nov 18, 2024
346c498
Fix `Events::onFlush` and `PostFlush()` documentation: events are alw…
lyrixx Nov 7, 2024
58ad1d9
Merge pull request #11709 from lyrixx/fix-event-doc
greg0ire Nov 18, 2024
8422a41
unitofwork.rst: php => PHP
k00ni Jul 22, 2024
82e2c98
Merge pull request #11556 from k00ni/patch-1
greg0ire Nov 19, 2024
2ff998d
Merge remote-tracking branch 'origin/2.20.x' into 3.3.x
greg0ire Nov 23, 2024
71e038c
Merge pull request #11723 from greg0ire/3.3.x
greg0ire Nov 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Is not correctly generated sql when changed/switched sqlFilter parame…
…ters

CachedPersisterContext::$selectJoinSql should be clear or regenerated when sqlFilter changed
The problem reproduce when in use fetch=EAGER and use additional sql filter on this property
  • Loading branch information
dbannik committed Oct 23, 2024
commit 439b4dacf415b743b0d40d65c490a4123759c520
6 changes: 5 additions & 1 deletion src/Persisters/Entity/BasicEntityPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ class BasicEntityPersister implements EntityPersister
/** @var CachedPersisterContext */
private $noLimitsContext;

/** @var ?string */
private $filterHash = null;

/**
* Initializes a new <tt>BasicEntityPersister</tt> that uses the given EntityManager
* and persists instances of the class described by the given ClassMetadata descriptor.
Expand Down Expand Up @@ -1271,7 +1274,7 @@ final protected function getOrderBySQL(array $orderBy, string $baseTableAlias):
*/
protected function getSelectColumnsSQL()
{
if ($this->currentPersisterContext->selectColumnListSql !== null) {
if ($this->currentPersisterContext->selectColumnListSql !== null && $this->filterHash === $this->em->getFilters()->getHash()) {
return $this->currentPersisterContext->selectColumnListSql;
}

Expand Down Expand Up @@ -1378,6 +1381,7 @@ protected function getSelectColumnsSQL()
}

$this->currentPersisterContext->selectColumnListSql = implode(', ', $columnList);
$this->filterHash = $this->em->getFilters()->getHash();

return $this->currentPersisterContext->selectColumnListSql;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilter;

use Doctrine\Tests\OrmFunctionalTestCase;

use function sprintf;
use function str_replace;

final class ChangeFiltersTest extends OrmFunctionalTestCase
{
private const COMPANY_A = 'A';
private const COMPANY_B = 'B';

public function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([
Order::class,
User::class,
]);
}

/**
* @return non-empty-array<"companyA"|"companyB", array{orderId: int, userId: int}>
*/
private function prepareData(): array
{
$user1 = new User(self::COMPANY_A);
$order1 = new Order($user1);
$user2 = new User(self::COMPANY_B);
$order2 = new Order($user2);

$this->_em->persist($user1);
$this->_em->persist($order1);
$this->_em->persist($user2);
$this->_em->persist($order2);
$this->_em->flush();
$this->_em->clear();

return [
'companyA' => ['orderId' => $order1->id, 'userId' => $user1->id],
'companyB' => ['orderId' => $order2->id, 'userId' => $user2->id],
];
}

public function testUseEnableDisableFilter(): void
{
$this->_em->getConfiguration()->addFilter(CompanySQLFilter::class, CompanySQLFilter::class);
$this->_em->getFilters()->enable(CompanySQLFilter::class)->setParameter('company', self::COMPANY_A);

['companyA' => $companyA, 'companyB' => $companyB] = $this->prepareData();

$order1 = $this->_em->find(Order::class, $companyA['orderId']);

self::assertNotNull($order1->user, $this->generateMessage('Order1->User1 not found'));
self::assertEquals($companyA['userId'], $order1->user->id, $this->generateMessage('Order1->User1 != User1'));

$this->_em->getFilters()->disable(CompanySQLFilter::class);
$this->_em->getFilters()->enable(CompanySQLFilter::class)->setParameter('company', self::COMPANY_B);

$order2 = $this->_em->find(Order::class, $companyB['orderId']);

self::assertNotNull($order2->user, $this->generateMessage('Order2->User2 not found'));
self::assertEquals($companyB['userId'], $order2->user->id, $this->generateMessage('Order2->User2 != User2'));
}

public function testUseChangeFilterParameters(): void
{
$this->_em->getConfiguration()->addFilter(CompanySQLFilter::class, CompanySQLFilter::class);
$filter = $this->_em->getFilters()->enable(CompanySQLFilter::class);

['companyA' => $companyA, 'companyB' => $companyB] = $this->prepareData();

$filter->setParameter('company', self::COMPANY_A);

$order1 = $this->_em->find(Order::class, $companyA['orderId']);

self::assertNotNull($order1->user, $this->generateMessage('Order1->User1 not found'));
self::assertEquals($companyA['userId'], $order1->user->id, $this->generateMessage('Order1->User1 != User1'));

$filter->setParameter('company', self::COMPANY_B);

$order2 = $this->_em->find(Order::class, $companyB['orderId']);

self::assertNotNull($order2->user, $this->generateMessage('Order2->User2 not found'));
self::assertEquals($companyB['userId'], $order2->user->id, $this->generateMessage('Order2->User2 != User2'));
}

public function testUseQueryBuilder(): void
{
$this->_em->getConfiguration()->addFilter(CompanySQLFilter::class, CompanySQLFilter::class);
$filter = $this->_em->getFilters()->enable(CompanySQLFilter::class);

['companyA' => $companyA, 'companyB' => $companyB] = $this->prepareData();

$getOrderByIdCache = function (int $orderId): ?Order {
return $this->_em->createQueryBuilder()
->select('orderMaster, user')
->from(Order::class, 'orderMaster')
->innerJoin('orderMaster.user', 'user')
->where('orderMaster.id = :orderId')
->setParameter('orderId', $orderId)
->setCacheable(true)
->getQuery()
->setQueryCacheLifetime(10)
->getOneOrNullResult();
};

$filter->setParameter('company', self::COMPANY_A);

$order = $getOrderByIdCache($companyB['orderId']);
self::assertNull($order);

$order = $getOrderByIdCache($companyA['orderId']);

self::assertInstanceOf(Order::class, $order);
self::assertInstanceOf(User::class, $order->user);
self::assertEquals($companyA['userId'], $order->user->id);

$filter->setParameter('company', self::COMPANY_B);

$order = $getOrderByIdCache($companyA['orderId']);
self::assertNull($order);

$order = $getOrderByIdCache($companyB['orderId']);

self::assertInstanceOf(Order::class, $order);
self::assertInstanceOf(User::class, $order->user);
self::assertEquals($companyB['userId'], $order->user->id);
}

private function generateMessage(string $message): string
{
$log = $this->getLastLoggedQuery();

return sprintf("%s\nSQL: %s", $message, str_replace(['?'], (array) $log['params'], $log['sql']));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilter;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;

use function sprintf;

class CompanySQLFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string
{
if ($targetEntity->getName() === User::class) {
return sprintf('%s.%s = %s', $targetTableAlias, $targetEntity->fieldMappings['company']['fieldName'], $this->getParameter('company'));
}

if ($targetEntity->getName() === Order::class) {
return sprintf('%s.%s = %s', $targetTableAlias, $targetEntity->fieldMappings['company']['fieldName'], $this->getParameter('company'));
}

return '';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilter;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="Order_Master")
*/
class Order
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var int
*/
public $id;

/**
* @ORM\Column(type="string")
*
* @var string
*/
public $company;

/**
* @ORM\ManyToOne(targetEntity="User", fetch="EAGER")
*
* @var User
*/
public $user;

public function __construct(User $user)
{
$this->user = $user;
$this->company = $user->company;
}
}
35 changes: 35 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilter/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\SwitchContextWithFilter;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
* @ORM\Table(name="User_Master")
*/
class User
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var int
*/
public $id;

/**
* @ORM\Column(type="string")
*
* @var string
*/
public $company;

public function __construct(string $company)
{
$this->company = $company;
}
}