active-record icon indicating copy to clipboard operation
active-record copied to clipboard

Suggestion: ActiveRecord::findAll() without $condition

Open egorio opened this issue 10 years ago • 16 comments

Sometimes you want to get all records from table (for example if it is a dictionary).

Each model has findAll($condition) method, but you can call it without condition.

I suggest modify Model::findAll($condition = null) to call it without $condition if you want. Just Model::findAll();

For example:

$records = Dictionaty::findAll(['field' => 'value']);

// find all records the same way
$records = Dictionary::findAll();

For now it looks like:

$records = Dictionaty::findAll(['field' => 'value']);

// for now it is a different way
$records = Dictionary::find()->all();

egorio avatar Jan 07 '15 21:01 egorio

What do you do if you have 10000 records in your database table? Fetching all records blindly is not a good idea when all get instantiated as active records...

cebe avatar Jan 07 '15 22:01 cebe

If you have 10000 records and call findAll() you are crazy :) You also can call find()->all() for this table. It is not a problem of code, it is problem of design :)

I told about small tables... for example $countries = Country::findAll();

egorio avatar Jan 07 '15 22:01 egorio

I thought of the "design" that prevents crazyness by letting you think before you do findAll() because it does not work ;) But you have a good point too.

cebe avatar Jan 07 '15 22:01 cebe

@cebe Than crazyness prevention should go through whole system. You can still do for example MyModel::find()->all(). I'm with OP. Condition should be removed. For example it just make pain to use findAll() inside unit tests. When you really need find all records and compare with original data set.

creocoder avatar Jan 07 '15 23:01 creocoder

@creocoder I was already convinced ;)

cebe avatar Jan 09 '15 15:01 cebe

findAll and findOne are very closely related. Should findOne behavior change to work without $condition too? and return ActiveRecord::find()->one() ?

Faryshta avatar Feb 08 '15 20:02 Faryshta

Hello! Are there any advances in this issue? Faced with the same problem and was surprised that AQ can not do query without conditions

vkarelin avatar Mar 24 '15 05:03 vkarelin

active query can, but not the findAll() shortcut. You can speed things up by sending a PR.

cebe avatar Mar 24 '15 14:03 cebe

If findAll() shortcut for find()->all() they need to be similar or this illogically

hellvesper avatar Jun 29 '15 10:06 hellvesper

I NEED THIS findAll() IN ANY CASE. HOW CAN I DO THIS?

shahidkarimi avatar Feb 26 '17 04:02 shahidkarimi

There is an active pull request that will be merged in 2.1 branch.

SilverFire avatar Feb 26 '17 10:02 SilverFire

@shahidkarimi , not sure if checked #8200 yet but you can do ::find()->all() for the same result until the PR is merged.

longrunningprocess avatar Mar 17 '17 12:03 longrunningprocess

Hello, ::findAll( ) should be implemented. Hope it is already done by now. However, I see on 2.0.15 it is not available yet. But yes For tables with large data, the programmer has to use conditions. I am sure anyone working on large data would run proper queries to filter data.

eisdevelopers avatar Apr 03 '18 09:04 eisdevelopers

As you guys said we should use find()->all() as an alternative of findAll(), is there any reason you do not check null param in findAll and use find()->all() for us? Since findAll have nearly 100% same name with find()->all(), it's hard to understand why they do not have nearly 100% same feature. If it is a problem of design, could you please explain this design? If it's a design, it should let us know that we should use find()->all() than findAll(), not just throw an inspection, without any hint comment in the original method (the original method comment tell us how to use with $condition, but not tell us that we should use find()->all() as an alternative).

+1 for findAll() without $condition

LazyKnightX avatar Apr 26 '18 09:04 LazyKnightX

It's ready for adoption in 2.1. Feel free to implement it and send a pull request.

samdark avatar Apr 26 '18 18:04 samdark

Is something better than?

namespace app\components\ar;

class ExtendedAr extends \yii\db\ActiveRecord
{
    public static function findAll($condition = null)
    {
		return $condition ? parent::findAll($condition) : parent::find()->all();
    }
}

YanAlex avatar Feb 22 '23 09:02 YanAlex