collections icon indicating copy to clipboard operation
collections copied to clipboard

Update ClosureExpressionVisitor.php

Open DrSchimke opened this issue 9 years ago • 5 comments

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...)

DrSchimke avatar Oct 28 '15 09:10 DrSchimke

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());

DrSchimke avatar Oct 28 '15 09:10 DrSchimke

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 avatar Dec 01 '15 17:12 baileylo

@baileylo this is OK for updates, but not really for querying (DQL uses the datetime value when building queries)

stof avatar Dec 02 '15 08:12 stof

@stof If I understand you well, this PR should be merged right ?

mikeSimonson avatar Mar 25 '16 08:03 mikeSimonson

This is not a good idea, comparing entities by reference can be a huge performance drain, because it happens recursively.

beberlei avatar Mar 27 '16 20:03 beberlei