collections
collections copied to clipboard
Update ClosureExpressionVisitor.php
Searching for elements within ArrayCollection using element's \DateTime attributes fails, since until now the comparison uses === instead of ==.
I would like to change the cases ::EQ and ::NEQ – is this ok?
(The matching functionality in PersistentCollection works fine, if I use EXTRA_LAZY...)
Here is an example for the problem:
$collection = new ArrayCollection();
$collection->add(['date' => new \DateTime('2015-01-01 00:00:00.000'), 'id' => 1]);
$collection->add(['date' => new \DateTime('2015-01-02 00:00:00.000'), 'id' => 2]);
var_dump($collection->matching(new Criteria(Criteria::expr()->eq('date', new \DateTime('2015-01-01 00:00:00.000'))))->toArray());
This is done by design and is documented in the Doctrine doc's, you'll need to scroll down but it says:
DateTime and Object types are compared by reference, not by value. Doctrine updates this values if the reference changes and therefore behaves as if these objects are immutable value objects.
@baileylo this is OK for updates, but not really for querying (DQL uses the datetime value when building queries)
@stof If I understand you well, this PR should be merged right ?
This is not a good idea, comparing entities by reference can be a huge performance drain, because it happens recursively.