spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

fetchAll with IN operator #275

Closed dertin closed 5 years ago

dertin commented 5 years ago

Hi,

Only one Row is returned for the first Id value but 3 Rows are expected:

return $this->connection()->fetchAll('SELECT foo.*
FROM foo
WHERE foo.FooId IN (:FooId)', [':FooId' => '10,23,30']);

In the following way, if it works as expected:

return $this->connection()->fetchAll('SELECT foo.*
FROM foo
WHERE foo.Id IN (10,23,30)');

How should this query be done?

FlipEverything commented 5 years ago

You should do the request like this:

return $spot->mapper('foo')->all()->where(['FooId' => [19, 23, 30]]);

Raw SQL is not needed in this case.

dertin commented 5 years ago

@FlipEverything

I use Raw SQL in cases where I do not need mapper relationships I have found that the response is significantly faster.

From what I see, I would not be working with the preparation of the query with the parameters.

FlipEverything commented 5 years ago

Ofc it's slower than raw queries, because there are a whole lot happening in the background. I think that you shouldn't use spot if you want to bypass the whole library with raw queries, but I won't argue with you about this topic, feel free to use it as you like.