Reduces annoying need to check if model attributes and relations are empty/null/undefined.


Reduces annoying need to check if model attributes and relations are set.


  1. Place ESelector in your extensions directory

  2. Attach the ESelectorBehavior to your model or base model

public function behaviors()
    return array(


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('', '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';

Wow ESelector is so much easier!


Total 6 comments

#13302 report it
evan108108 at 2013/05/21 10:21am

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:


Will not work.

#13301 report it
marcovtwout at 2013/05/21 09:59am

At an attempt to prove my point again, using your example, why would anyone attach this behavior and type:

if ($model->selector('', false) !== false) {
 } else {

..if you can just use PHP native:

if (!empty($model->contact->address->city)) {
} else {

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.

#13300 report it
evan108108 at 2013/05/21 09:30am

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.

  1. 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.

  2. 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('', false) !== false) {
      } else {
  1. 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.

  2. 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.

#13294 report it
marcovtwout at 2013/05/21 04:13am
  1. 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.

  2. 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.

#13248 report it
evan108108 at 2013/05/17 12:08pm

I don't believe this is valid criticism.

  1. What the extension checks is that the property isset not empty as you suggest.
  2. CHtml::value() is not a super great replacement in my opinion as it does not feel like the right use case to use it outside of the View context?

Which would you rather use:

1. CHtml::value($model, '', 'No City');


2. $model->selector('', 'no city);
  1. Even if you prefer CHtml::value() that does not warrant a -1. The extension works as advertised. I would ask you to remove that and revise your comment please...
#13229 report it
marcovtwout at 2013/05/16 10:53am
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!

Null checking is a common factor in coding, also, Grid/detail views can already handle it..

Leave a comment

Please to leave your comment.

Create extension