Tag commands

Read about the posts of tag commands

This article is dedicated to Alex, that asks 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 accesses the URL and execute the code, a huge process begins asking for the database and constructing the CRUD from zero.

Exist a lot of frameworks with Scaffolding in PHP, but I prefer CakePHP by its 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 doesn’t support Scaffolding, they remove it in the 2.5 version (actually the last version of CakePHP is 3.3).

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

  • CRUD: means CReate Update Delete. Actually when developers talk about CRUD refer to the pages needed to list records from a database, form to create new records and form to update records.
  • Scaffolding: Technique that creates CRUD list and forms on the fly and shows 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 generates the PHP files needed to access a database. Scaffolding does something similar, explores database and create CRUD on 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 the 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 the articles and others from categories of articles. Something simple.

I followed the model convention name of CakePHP:

https://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 suppose that you can figure out how to create your own database and tables and connect to a server. Anyway, here the structure:

 

Install Cake

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

Git

I create a 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 it directly from GitHub. Follow the next link and click the green button called Clone Or Download and then click Download ZIP. Decompress the file in a directory and continue.

Github Cake PHP

 

Try It

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

NGINX

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

Book Cake PHP Installation

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 is telling us that all is correct but a database isn’t we need to write database config.

Configure Cake

First, we need to connect to a 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 it with your data (is important that uncomment utf-8 if you use accents):

A configured database, the next step, begins 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 the execution of an application and ask 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 the other for Categories. Create file:

And now the most complex and difficult to read code:

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

Now for categories:

and code:

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

Scaffolding

Scaffolding List:

Scaffolding List

 

Scaffolding Edit:

Scaffolding Edit

You can browse for categories in the domain.tld/categories

Improvements

If you browse a little bit, you notice that category isn’t shown in Articles Scaffolding, only show ids. This is because the models need to know 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 important the existence of category_id, with another name the relation will be more complex. You can see relations here:

Book CakePHP Models

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

Git is a powerful tool that can help to manage code. But sometimes we don’t know or we are scared about the features aren’t know. At least is our code and don’t want to lose any line of them.

Following the previous git post: Basic Git commands, we talk about branches.

The branch is a powerful feature of git. If like we copy all code to a new directory and begin work there. But the main difference is that git knows how to join or merge these two directories without broke or lose any line of code and all in the same directory ;)

Then, we want to show how to manage branches and easiest that is.

First of all, we need to make a dir and init a new repository:

Now it’s time to create some code:

It’s time to see branches and create new ones:

Now we have two branches. Made some changes to hello.php, and test what happened:

Time to see differences:

We are in master and time to merge branches, now we say: merge second into master:

Merge two branches we don’t need the second branch, time to remove:

This kind of work with git gives us the opportunity to work in different code branch with different features, it’s like work in different directories without worry about mix code later.

 

EBAVS/ Try to work hard with the latest technologies. We work with git for several years and we learned a lot.

This is a simple reference guide with commands that we use day to day from the beginning of a project.

 

Initialising Repo with Git

First of all, we create a project (actually we download skeletons from a composer, but this is another history or post):

Then we initialise the git repository. This is important because git makes their local repository for local working:

Working with Repository

When you initialized we can begin work, then create a sample file for testing:

then add simple code:

now add this file to a repository:

or

Now we have a repository with some of the code. It’s time to move this code, we have code added but isn’t in the repo yet. Write:

The files are committed to a repo, are stored in this and assigned a short guide identifier 6f30a7f for us.

Go to change index.php and see what happens in the next commit:

And change to:

Look what we add -a parameter. This means that we change a file. If we only add files don’t need -a parameter.

We have our application ready to store on the remote server. We could make a remote in GitHub or in Bitbucket. I Have a preference for bitbucket because I can make private repos for free. But GitHub is good too.

When we make a repository, in GitHub for example, they make a new URL for the repository, the URL construction is:

https://github.com/user/repo.git

A user is your username and Repo are the repository name.  For this example a real URL could be:

Example Git EBAVS/

Then is time to add this remote repository to our local repository:

We have connected now our local repo with our remote repo, time to move code to remote:

And we have our repo working.

Summary

commands that we used:

 

Update:

If you like it you can visit our next post talking about Git: