active-record
active-record copied to clipboard
Suggestion: ActiveRecord::findAll() without $condition
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();
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...
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();
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 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 I was already convinced ;)
findAll
and findOne
are very closely related. Should findOne
behavior change to work without $condition
too? and return ActiveRecord::find()->one()
?
Hello! Are there any advances in this issue? Faced with the same problem and was surprised that AQ can not do query without conditions
active query can, but not the findAll() shortcut. You can speed things up by sending a PR.
If findAll() shortcut for find()->all() they need to be similar or this illogically
I NEED THIS findAll() IN ANY CASE. HOW CAN I DO THIS?
There is an active pull request that will be merged in 2.1 branch.
@shahidkarimi , not sure if checked #8200 yet but you can do ::find()->all()
for the same result until the PR is merged.
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.
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
It's ready for adoption in 2.1. Feel free to implement it and send a pull request.
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();
}
}