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

Query does not works with array of search criterias #104

Closed
EthraZa opened this issue Feb 12, 2021 · 3 comments
Closed

Query does not works with array of search criterias #104

EthraZa opened this issue Feb 12, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@EthraZa
Copy link

EthraZa commented Feb 12, 2021

Array of multiple search criteria is not working, just the first one works.

// Does not works, just the subject criteria is respected -(
$folder->query()->where([['subject' => 'foo'], ['UNSEEN']]);

// Works, all criterias are respected -)
$folder->query()->where(['subject' => 'foo'])->where(['UNSEEN']);

Looking the docs at Custom search criteria, since it uses an array of arrays, I came to the conclusion that multiple search criterias shoud work but, it may be not true. Yet, it would be cool if it works.

@EthraZa
Copy link
Author

EthraZa commented Feb 12, 2021

Loos like the where method just return the first instead to push it to the array.

So, as a quick solution I did my own function to increment wheres:

public function addWhere(object $query, array $search): object
    {
        foreach ($search as $k => $v) {
            if (is_array($v)) {
                $query = $this->addWhere($query, $v);
            } else if (is_numeric($k)) {
                $query->where($v);
            } else {
                $query->where([$k => $v]);
            }
            
        }

        return $query;
}

// Usage
$query = $this->addWhere($folder->query(), [['subject' => 'foo'], ['UNSEEN']]);

Maybe this logic can be imported to the where method itself in the future. ;)

@EthraZa EthraZa closed this as completed Feb 12, 2021
@Webklex Webklex reopened this Feb 14, 2021
@Webklex Webklex added the enhancement New feature or request label Feb 14, 2021
@Webklex
Copy link
Owner

Webklex commented Nov 3, 2021

Hi @EthraZa ,
what do you think of replacing

public function where($criteria, $value = null) {
if (is_array($criteria)) {
foreach ($criteria as $key => $value) {
if (is_numeric($key)) {
return $this->where($value);
}
return $this->where($key, $value);
}
} else {
$criteria = $this->validate_criteria($criteria);
$value = $this->parse_value($value);
if ($value === null || $value === '') {
$this->query->push([$criteria]);
} else {
$this->query->push([$criteria, $value]);
}
}
return $this;
}
with this:

public function where($criteria, $value = null) {
    if (is_array($criteria)) {
        foreach ($criteria as $key => $value) {
            if (is_numeric($key)) {
                $this->where($value);
            }
            $this->where($key, $value);
        }
    } else {
        $criteria = $this->validate_criteria($criteria);
        $value = $this->parse_value($value);

        if ($value === null || $value === '') {
            $this->query->push([$criteria]);
        } else {
            $this->query->push([$criteria, $value]);
        }
    }

    return $this;
}

I believe this would work as well and would allow multiple search criteria as long as the provider accepts them:

$query = $folder->query()->where([['subject' => 'foo'], ['UNSEEN']]);

Best regards,

Update 2021-11-04:
I just pushed ae26ff5 which solved a stupid little problem in my suggestion from above and also works with all old formats. So everybody wins :) Thanks for your suggestion!

@Webklex
Copy link
Owner

Webklex commented Nov 4, 2021

Hi @EthraZa ,
this change is now available via the v3.0.0-alpha release.

Best regards,

@Webklex Webklex closed this as completed Nov 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants