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

Collection index field ignored when selecting things from a not-initialized extra lazy collection #384

Open
janklan opened this issue Dec 4, 2023 · 0 comments

Comments

@janklan
Copy link

janklan commented Dec 4, 2023

BC break: no
Version: 2.1.4


Summary:

When attempting to ->match() a slice of an extra lazy collection using strings as indexes, the produced result is not an associative array with the expected indexes, but a regular array.


Sample code

#[ORM\OneToMany(mappedBy: 'assignedTo', targetEntity: Task::class, fetch: 'EXTRA_LAZY', indexBy: 'id')]
private Collection $assignedTasks;

public function getAssignedTaskOn(\DateTimeImmutable $date): ReadOnlyCollection
{
    $startDate = $date->setTime(0, 0);
    $endDate = $startDate->setTime(23, 59, 59, 999999);

    /** @var ExpressionBuilder $expressionBuilder */
    $expressionBuilder = Criteria::expr();

    $expression = $expressionBuilder->andX(
        $expressionBuilder->gte('startDate', $startDate),
        $expressionBuilder->lte('endDate', $endDate)
    );

    $criteria = new Criteria($expression);
    $criteria->orderBy(['position' => Criteria::ASC]);

//        $this->assignedTasks->toArray();
    $results = $this->assignedTasks->matching($criteria);
    dd(
        $results->toArray(),
        $results->getKeys(),
        array_slice($this->assignedTasks->toArray(),0, 2) // sanity check to see the collection is correctly indexes when not using the expressions to select parts of the collection
    );
}

The $this->assignedTasks->toArray(); causes the collection to initialise. With the line commented, the ->matching() is applied to an un-initialised collection, runs a SQL query and returns the expected result, however indexes are wrong:

1 in Employee.php on line 731:
array:1 [▼
  0 => App\Entity\Task {#1375 ▶}
]
2 in Employee.php on line 731:
array:1 [▼
  0 => 0
]
3 in Employee.php on line 733:
array:2 [▼
  "1edb9734-48cc-6eee-9efa-654d8f81754f" => App\Entity\Task {#1788 ▶}
  "1edb9731-73e7-6dfc-a3f3-a1b2352c64a1" => App\Entity\Task {#1825 ▶}
]

With the collection initialised, the result is 100% correct:

1 in Employee.php on line 731:
array:1 [▼
  "1ee8984d-846e-678c-b32f-0d1cce90af40" => App\Entity\Task {#1375 ▶}
]
2 in Employee.php on line 731:
array:1 [▼
  0 => "1ee8984d-846e-678c-b32f-0d1cce90af40"
]
3 in Employee.php on line 731:
array:2 [▼
  "1edb9734-48cc-6eee-9efa-654d8f81754f" => App\Entity\Task {#1602 ▶}
  "1edb9731-73e7-6dfc-a3f3-a1b2352c64a1" => App\Entity\Task {#1639 ▶}
]

Initialising the entire collection defeats the purpose of EXTRA_LAZY, so I assume this is a bug: I expected the indexes to be present in both test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant