Cake PHP

Scaffolding with CakePHP

This article is dedicated to Alex, that ask to me how to create a basic CRUD from a database without development, this is called Scaffolding.

For me, Scaffolding is one of the most amazing techniques that people don’t want to use. Exist a major reason, that reason is every time someone access to the url and execute code, a huge process begins asking to database and constructing the CRUD from zero.

Exist a lot of frameworks with Scaffolding in PHP, but I prefer CakePHP by their simplicity. Other languages like Python with Django and Flask or Ruby On Rails have their own mini scaffolding.

A little Terminology

Actually, the last version of CakePHP don’t support Scaffolding, they remove in 2.5 version (actually last version of CakePHP is 3.3).

Before to begin I want to explain some of the vocabulary here:

  • CRUD: means CReate Update Delete. Actually when developers talks about CRUD refer to the pages needed to list records from database, form for create new records and form to update record.
  • Scaffolding: Technique that create CRUD list and forms at the fly and show it.

Ok, continue explaining the difference between the two main techniques regarding this. The best of both is CRUD generation. CRUD Generation executes one code that explores the database and generate the PHP files needed to access database. Scaffolding do something similar, explores database and create CRUD at the fly. This is the reason that needs a lot of resources.

If you have a consistent database and don’t need to change anything I recommend that you use some of CRUD generators. Two of the best for me:

We need Data

Now I want to begin our amazing scaffolding project. First of all I need to create a database. I will begin to create 2 tables. One of articles and other from categories of articles. Something simple.

I followed model convention name of CakePHP:

http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html#model-and-database-conventions

I don’t want to enter to explain databases, I supose that you can figure out how to create your own database and tables and connect to server. Anyway, here the structure:

 

Install Cake

It’s time to install CakePHP. You can choose download or use git. I prefer git but maybe you want download:

Git

I create directory and enter it and clone:

With -b ‘2.4.9’ in clone we are telling that only want this branch or tag. If you look VERSION.txt of CakePHP you can see version:

Download

You also can download directly from GitHub. Follow next link and click green button called Clone Or Download and then click Download ZIP. Decompress file in directory and continue.

https://github.com/cakephp/cakephp/tree/2.4.9

 

Try It

If you are using apache in your environment, all works fine and you halt next step.

NGINX

In other hand, if you work with nginx you need to configure your server to make good environtment for CakePHP. Follow instructions in CakePHP Cookbook:

http://book.cakephp.org/2.0/en/installation/url-rewriting.html#pretty-urls-on-nginx

Trying

Then, if you load now your CakePHP url you will see your first big screen 🙂

screen-shot-2016-11-15-at-11-25-17

 

This is correct, CakePHP are telling us that all is correct but database isn’t we need to write database config.

Configure Cake

Firs we need to connect to database. This means that we need to create a configuration file. We have one sample file in:

Rename it to

And open it, search for the next code and fill with your data (is important that uncomment utf-8 if you use accents):

Configured database, next step, is begin the big and complex scaffolding code.

Creating Models

Now it’s time to create the models files to tell CakePHP what tables are involved. Create a file:

And write (or paste) the next code:

Save it and do the same with Articles:

And code:

It’s done, we have models.

 

Creating Controllers

It’s time to create Controllers. Controllers are files that control execution of application and ask to models for data and send to views.  In CakePHP also are mapped to urls. If you want to access to domain.tld/products you need to create a ProductsController. In our case I want two urls, one for Articles and other for Categories. Create file:

And now the most complex and difficult to read code:

The property $scaffold is the most important, tell to CakePHP that this controller are Scaffolding controller.

Now for categories:

and code:

It’s done. Scaffolding it’s working. You need to access to domain.tld/articles and CakePHP do the work for you, scaffolding in action.

Scaffolding

Scaffolding List:

Scaffolding List

 

Scaffolding Edit:

Scaffolding Edit

You can bowse to for categories in domain.tld/categories

Improvements

If you browse a little bit, you notice that category aren’t showed in Articles Scaffolding, only show id’s. This is because the models needs to now the relations between tables. Only change models to:

With $belongsTo and $hasMany properties we tell to Cake the relation with tables. Remember that if you don’t follow conventions this relation won’t work. In Articles is importante the existence of category_id, with other name the relation will be more complex. You can see relations here:

http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

Finally, I show you how can use Scaffolding with CakePHP. But remember that this is a hard work for servers if you use with a lot of tables and users, and is better to use CRUD instead.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *