Yii 2 For Beginners Update

Happy New Year Everyone!  I hope 2015 brings you joy and happiness.  I’ve been hard at work during the holidays to take care of bugs and typos in my book.  Even though Yii 2 For Beginners is only 64 days old, there were some minor changes to Gii and some other minor points that prompted me to do a fresh install and build the entire application from scratch.  Plus, as some readers pointed out, there were a bunch of typos.  No more.  The significant typos are gone and the bugs have been squashed.

Because I went through the entire application, I was able to clarify little things that will end up saving you time.  Most importantly, I confirmed that the code is accurate.  Everything is 100% working as described in the current version of the book.  I’ll do my best to stay on top of Yii 2 changes that will affect the book as they occur in the future.

I’m also happy to announce a major enhancement to the book.  I’ve incorporated Gists, which are code snippets hosted by Github, directly inline and in context in the book.  Anywhere in the book where there are more than 3 lines of code that need to be copied, you can now launch a gist in a new window and copy it from there. This makes working with the book a lot easier. And you don’t have to worry about bookmarking anything, it’s all attached to the relevant parts of the book, so it’s all organized for you.

And the even better news is that all of this is free.  All updates to the book are free for the life of the book.  All you have to do is go to your http://www.leanpub.com account and download the latest version.

I’m also pushing hard to get bonus material written for the book by the end of January 2015.  I would have waited with the current release until then, but I know how important your time is, and I wanted to get the improvements out to you as quickly as possible.

Let me conclude this post with saying how much I appreciate the positive comments, links and reviews.  It really drives me to do more.  I absolutely love the Yii 2 framework and knowing that I can help people learn it, as others have helped me, gives great purpose to my writing.  So once again, happy 2015.  I’m looking forward to sharing a great year with you.

17 thoughts on “Yii 2 For Beginners Update”

  1. Hi, great book.

    I had to change PermissionHelpers model because when you use PermissionHelpers::requireMinimumRole(‘SuperUser’) and there is no such role name in your database table, you will still have permissions to do all. Why? Because value of role_id is always greater then false.

    What I did:
    public static function requireMinimumStatus($status_name)
    if ( ValueHelpers::getStatusValue($status_name) && Yii::$app->user->identity->status_id >= ValueHelpers::getStatusValue($status_name)) {

    return true;

    } else {

    return false;



    public static function requireMinimumRole($role_name)
    if ( ValueHelpers::getRoleValue($role_name) && Yii::$app->user->identity->role_id >= ValueHelpers::getRoleValue($role_name)) {

    return true;

    } else {

    return false;



    It doesn’t mean what I did is the right thing, but it works.

    And…….Thank you for writing this book.

    I learned some things I did not know.

    1. Thanks for your kind words on the book, I really appreciate it. On the helpers method, that’s an excellent point. While not necessary if the method is called properly, it does close a hole from programmer oversight if the programmer makes an error in calling the method. You handled it perfectly. I will consider that modification for the next update. Nicely done!


      Thanks for pointing this out, but your solution did not work. I came up with a robust solution to close this potential hole in the bonus chapter, so please refer to that if you have not already done so. Thank you again for pointing this out.

  2. Hi Bill,

    Though I have experience in development, I am new to web-development. There’s something missing out there in the Internet. I found several blogs and websites dealing with Yii2 (including yours), however I didn’t find any step by step guide to set up an development environment (composer etc). I am running Ubuntu. Is that included in your book?

    1. Hi Stephane, thanks for your question. You can use whichever development environment you are comfortable with. In the book, I go through setup on xampp for windows. I link to all the setup instructions and downloads for the tools, but I don’t cover those instructions as part of the book.

      You do not need to follow that environment, you are not locked into that at all, and you can use mamp, vagrant, mac, linux etc., it’s up to you. One of the big pluses about xampp, mamp, and vagrant is that they provide apache, PHP, MySql, and PHP MyAdmin for you out of the box, so those are very convenient setups.

      Setting up a development environment can be a challenge for newer programmers and it’s good way to know if you are ready to study framework development.

      You should have an understanding PHP, Mysql, and be familiar with using and IDE (like Netbeans) and Php MyAdmin. Mysql Workbench is a plus. You don’t necessarily need to be an expert with the tools, but you should at least have basic knowledge to work with.

      The good news is all of these tools are free and work with multiple development environments. You can get composer at getcomposer.org, they have installation and download instructions for multiple environments.

      You just need to get it installed, we cover the commands after that in the book.

      There are plenty of tutorials and videos for all of these tools, so there is plenty of help out there.

      Personally, I would spend a week or two playing with the tools, making sure they are working, before diving into framework development. It’s the least fun part of the journey, but we all have to walk down that road sooner or later, so you might as well do it now.

      That said, once you are setup in a development environment, I take you step by step into setup of Yii 2, including apache config. I cover each step in detail, I will get you up and running.

      I hope this is helpful information for you.

      1. Hi Bill,

        thank you for your reply. I guess I did not clearly state my question. I do have a LAMP environment running including PHPMyAdmin etc.
        Ok, so next will be Composer. But what about Codeception. I found this to be the preferred testing environment for Yii-based applications, and I love test-driven development. And are there other necessary helpers? I think of integration with SASS or LESS-based front-end libraries such as Foundation. Due to the heterogeneous nature of the web this can be a very challenging setup for newbies like me. What’s missing is a tutorial or some piece of document explaining this. Do you have any recommendation on this?

      2. TDD and codeception is not something I cover in the book, and not a strength for me personally, so I think it would be best to refer to the Yii 2 forum for your questions on TDD, and of course the Yii 2 guide.

        I may come back to that topic in the future, but for now, there’s too much else I’m working on.

        I’m releasing over 100 pages of bonus material within the next day. You get that for free by logging into your leanpub.com account. I will send out an email when it’s ready and announce it on the blog as well.

  3. Hello Bill,
    I bought your book and it was worth it!!!

    Only I have a questing regarding the profile, How and where do I have to make changes not to return only 1 profile, but 4?
    Only viewing/updating own profiles and creating max 4 or after an upgrade max 8?

    Thanks in advance,

    I didn’t know where to post my question, but hope you can help me.

    1. Hi Marcel, thanks for the kind words on the book, glad you are finding it useful.

      To make the changes to support multiple profiles, you would need to adjust the profile controller and the associated view pages.

      Also, note the userHas method is used for returning a single record, not multiple.

      You could make changes to the helper files to account for the number of profiles you want to support as well.

      When you are having multiple records, the code generated by Gii is closer to what you are looking for, so you can reference that as well.

      I hope that points you in the right direction.

      I’ll be adding over 100 pages of bonus material within the next day, so you will get the benefit of a lot of new things that aren’t covered in the base book.

      You get free updates for the life of the book, and you will be notified by email when the update is released. I plan to continue updating the book for the foreseeable future.

      Thanks again for the kind words on the book.

  4. Bill, I’ve read the Bonus chapters and it gets me further and further 🙂

    Now I wonder if it is easy to show/hide the FAQ’s Answers beneath the question when clicking the questions…

    Also in an other scenario when clicking an question, a modal will popup with the answer…

    1. Thanks for the nice words about the book. For show/hide/ you probably need a jquery implementation for that. I will be working on those types of things in future updates, but I haven’t worked on it yet, so I don’t have a ready-made implementation. I will be working on this in future updates. In the meantime you might want to check out this popover plugin from Kartik at Krajee.com, I think you could get the effect you are looking for. Thanks again for the kind words.

  5. Hi Bill,
    I installed the base template in order to use it for a litle website and to learn Yii 2. But doing that on my own the progress was very slow. And I soon recognized I should have chosen the Advanced template ;-).

    I am really glad about finding your book. You explained very well many things I didn’t understand or didn’t know, mostly line by line. I worked through it and built your modification of the advanced template. Everything works fine :-).

    I am looking forward to extend it for my own application.
    Thank you very much for writing!

    Best regards

    1. Thanks Hauke for nice comments. I’m glad you found the book helpful. I also appreciate your comment about learning things line by line. I felt that was important. Very often we just skip over things because they work, without understanding why they work, and I wanted to to give us a strong foundation for future development. I’m happy this is working for you.

      Good luck with your application and thanks again for comment.

  6. Hello Bill, I got an error on chapter 14 when trying to execute the MySql SQL code, it wouldn’t create the constraint because my user id was not unsigned. After altering the user id to unsigned it executed fine.

    (Do you have a FAQ per Chapter where I can add comments or is it fine to add them here)

    1. It’s better to email me for longer questions, but this is fine for a typo. Just put the comment near the post you think is most relevant. I checked the book, but it does include unsigned as data type for user_id in both the book and the Gist, so I don’t think it’s a problem with the book. Please double-check the book and let me know if you see something different. Anyway, thanks for the diligence, everyone will appreciate it if we do find typos.

      Matching datatypes and constraints are required to setup foreign keys. This can be very tricky to diagnose, because often it will simply tell you that it failed without giving you the reason. So this comment is a great heads up for anyone who comes across this kind of problem. Thanks again for your comment.

  7. Hi Bill,

    I am working through the latest version of your book, just downloaded, and I am not able to get the User.php model up and running. I have checked the git against your book and did not find any differences, but when I try to create a new user I get many errors (this is just the start of the list). I tried making the two modifications recommended in the github comments on User.php, but this doesn’t change anything. I also rechecked my db against your recommended db several times, and I confirmed that password and username are correct in the config. Have you heard about this from other users? Any recommended troubleshooting? ( I realize you are only looking for errata/typos and that this is probably my fault, but I’ve spent a lot of time double checking everything so I wanted to reach out).


    1. in /var/www/html/yii2build/vendor/yiisoft/yii2/base/Component.php at line 143
    2. in /var/www/html/yii2build/vendor/yiisoft/yii2/db/BaseActiveRecord.php – yii\base\Component::__get(‘user_type_id’) at line 246
    3. in /var/www/html/yii2build/vendor/yiisoft/yii2/validators/DefaultValueValidator.php – yii\db\BaseActiveRecord::__get(‘user_type_id’) at line 46
    4. in /var/www/html/yii2build/vendor/yiisoft/yii2/validators/Validator.php – yii\validators\DefaultValueValidator::validateAttribute(common\models\User, ‘user_type_id’) at line 238
    5. in /var/www/html/yii2build/vendor/yiisoft/yii2/base/Model.php – yii\validators\Validator::validateAttributes(common\models\User, [‘status_id’, ‘role_id’, ‘user_type_id’, ‘username’, …]) at line 333
    6. in /var/www/html/yii2build/vendor/yiisoft/yii2/db/ActiveRecord.php – yii\base\Model::validate(null) at line 416
    7. in /var/www/html/yii2build/vendor/yiisoft/yii2/db/BaseActiveRecord.php – yii\db\ActiveRecord::insert(true, null) at line 582
    8. in /var/www/html/yii2build/frontend/models/SignupForm.php – yii\db\BaseActiveRecord::save() at line 51
    9. in /var/www/html/yii2build/frontend/controllers/SiteController.php – frontend\models\SignupForm::signup() at line 123
    10. frontend\controllers\SiteController::actionSignup()

    1. The code in the book is fully tested against the latest version of yii 2.0.3, so it should work perfectly. It’s probably an error in setup.

      When you install the advanced template, and you run the init, you should be able to register users. This is before making modifications. If that is not working, then there is a problem, most likely with your DB.

      From what I can tell, you seem to be having validation errors, which could point to DB formatting issues.

      It’s really hard for me to diagnose this from here, please follow up via email through the leanpub contact author link if you need further assistance.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s