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

Not valid filtering by DateTime for ArrayCollection #93

Open
vvasilenok opened this issue Sep 6, 2016 · 5 comments
Open

Not valid filtering by DateTime for ArrayCollection #93

vvasilenok opened this issue Sep 6, 2016 · 5 comments

Comments

@vvasilenok
Copy link

Hi!

I found not valid behavior for ArrayCollection matching function.

$criteria = Criteria::create()
                ->where(Criteria::expr()->eq("date", $date));

$result = $this->meta->matching($criteria);

For ArrayCollection matching use code from

# Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php (lines 112 - 116)
switch ($comparison->getOperator()) {
   case Comparison::EQ:
      return function ($object) use ($field, $value) {
            return ClosureExpressionVisitor::getObjectFieldValue($object, $field) === $value;
}

But

$date1 = new DateTime('2016-09-05');
$date2 = new DateTime('2016-09-05');
$result = $date1 === $date2; ///false

$result = $date1 !== $date2; ///true

Need fix this.

@Ocramius
Copy link
Member

Ocramius commented Sep 6, 2016

This is normal behavior: comparing object fields will never work, since we
can't assume that an object field is a value object on our own.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

On Tue, Sep 6, 2016 at 9:55 AM, vvasilenok notifications@github.com wrote:

Hi!

I found not valid behavior for ArrayCollection matching function.

$criteria = Criteria::create()
->where(Criteria::expr()->eq("date", $date));

$result = $this->meta->matching($criteria);

For ArrayCollection matching use code from

Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php (lines 112 - 116)

switch ($comparison->getOperator()) {
case Comparison::EQ:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) === $value;
}

But

$date1 = new DateTime('2016-09-05');
$date2 = new DateTime('2016-09-05');
$result = $date1 === $date2; ///false

$result = $date1 !== $date2; ///true

Need fix this.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#93, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakJ2VIK5iah3l8gF_nZzaFul3VEbnks5qnRx0gaJpZM4J1jyC
.

@vvasilenok
Copy link
Author

vvasilenok commented Sep 6, 2016

Possibly add a not strict comparison? or replace current comparison to not strict? Or for DateTime use toString method?

@Ocramius
Copy link
Member

Ocramius commented Sep 7, 2016

Possibly add a not strict comparison?

non-strict comparison in PHP crashes with segfaults when there are infinite cycles/recursive data structures :-(

@vvasilenok
Copy link
Author

But with strict comparison need write this code for correct work

        $reports = $project->getReports();
        if ($reports instanceof PersistentCollection && $reports->isInitialized()) {
            $criteria = Criteria::create()->where(Criteria::expr()->in('reportDate', [$date]));
        } else {
            $criteria = Criteria::create()->where(Criteria::expr()->eq('reportDate', $date));
        }


        return $reports->matching($criteria)->first();

I think this behavior is not expected when you use criteria

@elpoutro
Copy link

elpoutro commented Feb 4, 2020

I really don't like this kind of freak but this solution work for me:

$criteria = Criteria::create()
    ->where(Criteria::expr()->gte("date", $date))
    ->andWhere(Criteria::expr()->lte("date", $date));

$result = $this->meta->matching($criteria);

I needed something that work if $this->meta is a Repository or an ArrayCollection. So i confirm this solution work for both.

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

3 participants