ESelector ¶
Reduces annoying need to check if model attributes and relations are set.
Install ¶
Place ESelector in your extensions directory
Attach the ESelectorBehavior to your model or base model
public function behaviors()
{
return array(
'ESelectorBehavior'=>'ext.ESelector.ESelectorBehavior',
);
}
Examples ¶
echo $model->selector('title', 'NO TITLE');
The above is equivalent to:
if( empty($model->title) ) {
echo 'NO TITLE';
} else {
echo $model->title;
}
ESelector also works with relationships ¶
echo $model->selector('contact.fullname', 'No Name');
echo $model->selector('contact.address.city', 'No City');
The above is equivalent to:
if( !is_null($model->contact) && !empty($model->contact->fullname) ) {
echo $model->contact->fullname)
} else {
echo 'No Name';
}
if( !is_null($model->contact) && !is_null($model->contact->address) && !empty($model->contact->address->city) ) {
echo $model->contact->address->city;
} else {
echo 'No City';
}
Use Yii core code instead of this extension
If you use empty(), proper values like 0, "0" and false will be considered false and you will get your default value. Also, empty() already checks if_null().
If you want to do it, just use Yii's build in CHtml::value() instead! http://www.yiiframework.com/doc/api/1.1/CHtml#value-detail
Null checking is a common factor in coding, also, Grid/detail views can already handle it..
@marcovtwout
I don't believe this is valid criticism.
Which would you rather use:
1. CHtml::value($model, 'contact.address.city', 'No City'); --or-- 2. $model->selector('contact.address.city', 'no city);
@evan108108
In your code description, you are saying 'the code above is equivalent to', and using 'empty' in the examples. Again, using php's empty() will omit any isset warnings, so also no need to check those separately.
Yes, only when outputting values, I (but also any Yii widget) would use CHtml::value(). But when handling output, this is pretty much the only case where an alternative default value would be required (your own examples only demonstrate 'echo'). On lower code levels, you would just want the real value, like null.
Bottom line: although your extension allows slightly less typed code in some situations, it feels overengineered to me, and with a non-descriptive function name ('selector') and a weird default value ('false' instead of 'null') I stick to my comment.
@marcovtwout
I think your missing the point. You are telling people not to use this extension and giving it a -1 based on nothing but your preferences and inaccurate information. I find that inappropriate. Perhaps if the extension did not work as advertised or broke the core you would have a case.
That said Let me go through the points you are making.
Yes I did say in my examples 'equivalent to', however if you had looked at the code you would see (as I tried to explain) that we do not use empty but isset and nothing is checked twice.
You say that handling output is the only case where default values would be required. I could not disagree more! Here is an example:
class MyController extends Controller { public function actionMyAction() { ... if($model->selector('contact.address.city', false) !== false) { doSomthing...; } else { doSomethingDiff...; } .... } }
You do not like the name selector. I do. So? Suggest a better name if you like. Keep in mind that the name must be somewhat obscure so as not to conflict with other properties you might have in your model.
Finally you say ESelector is over over engineered. ESelector is essentially single method which gets attached to your model that contains exactly 8 lines of code. Thats right 8! Thats 9 lines less then CHtml.value. If you count the entire behavior that number jumps up to a whopping 15 (that's still 2 lines less then CHtml.value). 15 very easy lines of code to understand. Does that sound over engineered? No, no it doesn't.
@evan108108
At an attempt to prove my point again, using your example, why would anyone attach this behavior and type:
if ($model->selector('contact.address.city', false) !== false) { doSomthing...; } else { doSomethingDiff...; }
..if you can just use PHP native:
if (!empty($model->contact->address->city)) { doSomthing...; } else { doSomethingDiff...; }
By saying it is over-engineered, I am not talking about the amount of code in the extension, I am talking about adding a layer of abstraction to user code that isn't necessary. Despite the shortcomings in the extension and description I mensioned, I am mostly downvoting it just because I think it does not add any significant value. Please look again at your code and presentation, and don't take it too personally.
@evan108108
Again you miss the point. If you don't like the extension don't use it, but down voting it and telling others not to use it is just not expectable when all you can really say is that its not for you. I think it adds a lot of value versus CHtml.value. Thats my opinion and thats the point. Value is assigned by the individual user. State your opinion and let people decide for themselves.
Also again your example is inaccurate as 0 is considered to be empty, so this:
empty($model->contact->address->address_1)
Will not work.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.