The Database First Approach of Yii

The Database First Approach of Yii

Each framework has a different selling point so to speak. For example, Laravel has arguably the most clean and intuitive syntax available. Symfony has built a reputation as a standard of sorts. Codeigniter is known for simplicity and speed. The next generation PHP Framework called Phalcon is known for breaking new ground with blazing fast performance via a direct extension to PHP written in C. Yii’s claim to fame is the database first approach. It requires that you focus on the database, database seeding, and its structure first, then build the models according to that structure. As we mentioned in a prior post, Gii does this for you, making initial model bootstrapping very quick and easy.

Database First

To test this theory out, let’s say we have a WordPress database. With this database in place, we can configure Yii to use this database via the configuration files. Once that is all configured, we can use Gii to create a model for every single table in one shot. Let’s see how to do that. First off, we’ll assume you have a local WordPress install running. If not, that’s ok, you can still follow along here. Next, install a copy of Yii. We will use the advanced template that we had installed in a prior episode. Finally, we’ll configure this install to use the WordPress database then run the Gii tool like so:

Create The Models

yii wordpress models

Create CRUD for Posts Table

yii wordpress crud

Small Controller Edit

The actionIndex method by default queries the database and returns *everything* from that table, with no conditions. WordPress stores many types of posts in the wp_posts table. We only want to see the posts, so we add the where clause to the query as seen here.

Complete

By taking the database first approach and using Gii, we were able to create models for every single table in a WordPress database, as well as create CRUD for the posts table. The example database for this example had about 100 posts in it, and when we visit http://yiiadvanced.com/index.php?r=posts we get all of them in the familiar GridView that Yii provides allowing us to view, update, delete, or create via buttons associated with each post. There is also a nice paginated layout. This took about 10 minutes with the Gii tool and we have a functioning application. It’s not something we’d put on the internet, but we have a strong starting point from where to begin.

Benefits

The benefits are easy to see here. Gii reads the entire database schema and creates everything for you in your models. You can then create CRUD for any model you want. All of this gets completed in minutes. This is really incredible, however this does not result in a user friendly and functional application. It does give you the entire blueprint to create this, but it will require *heavy* modification of the generated code, in addition to potential reorganization of files, namespaces, routes, and so on.

Drawbacks

Ask yourself, is it easier to add to something or take away from it? It really all depends, don’t you think? Sometimes, it seems that taking away from something is actually a little trickier. This is what you run into with all of the generated code. Yes, it is wonderful that the code was created for you, however this does not mean it is not required that you understand what the code does and how it is implemented. In fact if anything, it is the reverse. You need to understand precisely what each line of code is doing, because you need to modify it appropriately for your applications needs. Doing this without breaking things is not as easy as it might seem 🙂 If you are approaching building your application from the other angle, that is to say building out your models and migrations as you go so to speak, you are forced to make things work one step at a time. It might be a little easier to digest that way. All in all, studying both approaches will probably help to understand how to better build your application, regardless of which approach you choose.

Conclusion

In this episode about Yii, we took a look at the database first philosophy of working with Yii to create applications. By using a defined database to begin with, in this case an actual WordPress database, we found that we can use the Gii tool to give us a fully functioning base application within a very short time for which to build upon.