In my workplace I use Symfony2, for my hobby projects Yii . And in Symfony2 I found a very useful function. I can call on every column name the findBynameOfColumn function implicitly! I can automaticly call from the Symfony, and this is great, because I don’t have to explicit create this methods.
For example:
Table’s columns:
-id
-name
-price
-created
And I can call the findByName(), findByPrice too (of course the Name and Price are really the entity’s field names). I don’t have to write these functions. For me this is very helpful.
Will it be avalaible in Yii 2.0?
I tried found in the framework’s classes. But I haven’t discovred this functionality.
My personal opinion is that this is absolutely useless feature, and I have no idea why other frameworks keep implementing it.
Model::find(['columnName' => $value])
is already short enough. It’s the equivalent of Model::find()->where([‘columnName’ => $value])->one();
Anyway, you can implement this feature by yourself. Use magic method __callStatic() for it.
public static function __callStatic($name, $arguments) {
if (...$name looks like findBySomething...) {
// make sure that the field exist in schema
// create AQ instance, add andWhere([$field_name => $arguments[0]),
// return AQ or call one() and return AR
} else {
// call parent implementation (that will raise exception)
}
}
See BaseActiveRecord::find() for examples how it can be done.