yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

DB: ->limit(1) in ->one()

Open ldkafka opened this issue 2 weeks ago • 3 comments

Proposed new feature or change

Hi,

Is there a use-case where the ActiveQuery ->one() cannot/should not have an implicit ->limit(1) in it?

I vote for adding ->limit(1) to ->one().

Thoughts?

ldkafka avatar Dec 10 '25 17:12 ldkafka

This has been discussed many times already. https://github.com/yiisoft/yii2/issues/14236 https://github.com/yiisoft/yii2/issues/13875

mspirkov avatar Dec 10 '25 17:12 mspirkov

Yes, but it still seems not implemented, despite any solid reasons against it... Limit (1) is meant to stop the search when the condition is met (either in the where clause, or order by, etc). I briefly read through the existing threads and was not convinced of a valid use-case against adding it. Maybe I missed something?

ldkafka avatar Dec 10 '25 18:12 ldkafka

I haven't delved into these discussions very deeply either. But I know for sure that this has been discussed a lot, and it's actually quite a complex topic. Perhaps you should take a look at this: https://github.com/yiisoft/yii2/pull/20266

Maybe you have something to say about this.

mspirkov avatar Dec 10 '25 20:12 mspirkov

As @mspirkov said, that was already discussed, and the threads linked have the answer to the question.

samdark avatar Dec 11 '25 16:12 samdark