Yii 2 Dropdown List with Boolean Values

Sometimes it’s the simplest tasks that cause us the most grief. For example, a couple of days ago, I conceived of and wrote a tutorial for Super Simple RBAC in Yii 2 in about 20 minutes. What got me excited about that tutorial is that I reduced down the complexity of implementing RBAC, in other words granting special privileges and access to users with a role value of Admin, to six simple steps.

Of course that would not be possible if not for Yii 2. I know of no other framework that hands you so much power out-of-the-box that can help you move along so quickly. I’ve already gotten some great feedback on it and it seems like a nice solution for those who need admin users, but don’t want it to be too complex.

I cover this subject in my book, Yii 2 For Beginners in great detail, with a somewhat more robust solution with graphical UI for assigning roles. Also, a quick word of thanks to everyone who has purchased a copy, I appreciate your support.

Anyway, writing the RBAC tutorial had me feeling like a jr. Ninja and I was pretty happy about it. Unfortunately, I got splashed down to Earth on something really simple that took me a couple of hours to get right.

Have you ever had that frustrating feeling of knowing that what you want to do is so simple, and yet you just don’t know the right syntax to do it? So off you go, into experimentation mode to try to figure it out.

Now it would be nice if you could just look up everything in one awesome book, and the official guide is a great work in progress, but as of this writing, it is still incomplete. And even when it is complete, it is not going to cover every scenario.

It’s also not always clear what the best path to take is. For example, I’ve gotten quick at creating the little methods on the model that define relationships, and these are very useful for relationships. You can create dropdown lists with these methods in the views that really make your forms and view pages very intuitive for users.

But the ones I’m used to doing are based on relationships, which means they are built a certain way, involving other models and data tables. Now that sounds more complex, and it probably is, but like I said, I’ve done enough of them to be fairly comfortable with them at this point. I cover them extensively in my book.

But what if you just need to return a boolean value in a dropdown list that you want represented as a yes or a no? Does it make sense to put that in a DB table and create a relationship around it?

This is where the conversation gets interesting. You could do it that way, and if you were following a strict convention, doing it the same way as your other relationships, there’s some effeciency in that. You are always doing things the same way, therefore your code is easier to maintain and extend.

But here’s the catch. A boolean as a data structure is highly efficient, especially with MySql. That means if you get into complex querries that utilize the boolean value, it’s better to have the data type enforced as a boolean, MySql will perform better. It may only be a small difference, but small differences scaled over a large project can really add up.

2 thoughts on “Yii 2 Dropdown List with Boolean Values”

  1. Is it possible to create a dropdownlist with a boolean!?
    I’m suffering!

    field($model, ‘bln_reprogramacao’)->DropDownList([1 => ‘Yes’,0 => ‘no’],[‘prompt’=>”])?>

    How can I make it work??

Leave a reply to Bill Keck Cancel reply