Tag Archives: CakePHP 2.5

Install CakePHP2 on Codio

I only just discovered Codio yesterday! How come nobody ever told me about this? If you haven’t tried it yet and you are a keen programmer you should try it out. Chances are though you here because you are trying to get CakePHP2 working.

Well I’ve put in the hard work for you so just follow the steps below to get it working.

Step 1 – Create Project

  1. Create a new project.
  2. Give it a name. I named mine CakePHP2 but you can call it what you want.
  3. Select the “Template” tab.
  4. Choose “Empty project” from drop down list.
  5. Click Create

Sit back while your virtual machine and programming area is created for you. Step one is done.

Step 2 – Install Apache2, MySQL and PHP with PHP Modules

Your new project should launch with a tab called “Backend”, this is just a terminal you can use to run commands. If for some reason it’s not showing just go to Tools > Terminal. Then run the following command to install Apache2, MySQL and PHP with PHP Modules.

You will notice a few important things are installed

  1. Apache2 – Webserver
  2. MySQL – Database Server
  3. PHP5 – Version 5.5.15 (at the time of writing)
  4. Composer – PHP Package Manager
  5. PDO – Required for CakePHP database connection

After installation you are given some important information about where to find things later such as the php.ini and httpd.conf files etc.

You might also be wondering what “parts” is? It’s the codio command for doing a variety of things such as package installation and starting or stopping services such as Apache2. For example “parts search php” lets you search PHP packages that you can install. Lets do that now by installing PHP My Admin, we will need it later.

for good measure let’s restart Apache

Now you should be able to go to the last Menu option on the right and select “Box URL”. Then type /phpmyadmin on the end of the URL. You should now see the PHP My Admin login page for MySQL.

Before we can login to PHP My Admin we need to add a password to the default “root” user of MySQL. Let’s do that with the following command. Change “newpassword” to the password you want. You may need to start MySQL first.

Now that you have added a password to your “root” MySQL account you can go to the “Box URL” and then access /phpmyadmin. Then login with the following

  1. User: root
  2. Password: newpassword

Now that you are logged in to PHP MyAdmin go ahead and create a database for our CakePHP installation. I created a database called: cakephp but you can create and name as many as you like. You can also create database users however remember to GRANT access to these newly created users.

Step 3 – Install CakePHP2 with Composer

We now can install CakePHP. The environment is ready and we have a database ready to use with root/newpassword. First make sure you are outside of the “workspace” folder. In the terminal you can type the “pwd” (present working directory) and it will show you where you are. If you have followed the guide so far then you should be by default be inside the workspace directory however we want to move up a directory to the /home/codio/ directory.  To move back directory you can type “cd ..”. So once you sure you’re inside the “codio” directory type the following.

You may get an error saying that composer needs an update. That’s easy to fix with the following command line. Even with the error CakePHP should have successfully downloaded. You can check by using “ls” to list the items in the folder. You will need to first “cd workspace/” to see the downloaded CakePHP files.

After all this you should now be able to select “Box URL” and see a working copy of CakePHP. You will however notice a few errors. Lets fix those errors now.

Step 4 – Fix CakePHP Errors and Tidy Up

Since we are getting good with composer lets add the official CakePHP Debug Kit so that error goes away.

Next open app/Config/bootstrap.php and add the following

Lets now fix the WARNING: strtotime() error. This is an easy one. Open app/Config/core.php and uncomment the following line.

While we have the core.php file open lets change the Security.salt and Security.cipherSeed values. That gets rid of two more errors.

Last but not least let’s now connect our database. First you will need to create a new file app/Config/database.php. You can copy and paste the code from app/Config/database.php.default. Once you have done that edit the following code with your database user and password.

Make sure you edit the $default datasource and NOT the $test otherwise you will not see any changes.

Installation Finished

Give yourself a pat on the back, that wasn’t to hard was it. I hope that this guide gives you an idea of the process used for installing various packages. With the power of composer the world is your limit. You should be able to set up any PHP package available to you. Once CakePHP3 goes stable then you’ll be ready to install that as well!

Happy app building.


Encrypt & Decrypt CakePHP2 Database Fields

In a current project I’ve been working on I needed to encrypt and decrypt database fields such as user phone numbers and email address. While this is not the only security measure you should have in place it’s certainly worth while for sensitive data that you want to provide a little extra obscurity for. Remember that this trick just encrypts the data so that means that it can be decrypted, in other words a determined hacker could still decode this information if he also gain access to the Security Salt.

You need to add two callback methods to your AppModel.php file. The first is a beforeSave which will encrypt the data before it’s saved and the second is an afterFind function that will decrypt the data after it’s retrieved from the database.

Include the following functions between this code in your AppModel.php file.

beforeSave Function

afterFind Function

The above examples show email, mobile_number, home_number etc being encrypted/decrypted however you could change this for any field you wish. I have also wrapped the encrypt/decrypt code in an if statement to check if there is any data or not as I was finding that even if the field was empty it would still encrypt a “blank” into the database. So for the sake of performance I figured it was best to only encrypt or decrypt when there was real data and not empty fields.

Another thing to be aware of is to change your database fields to VARCHAR(255) otherwise if your field is shorter the encryption hash will get trimmed and will not decrypt properly. I’m also using the CakePHP Security.salt found in the Config/core.php file however you could and probably should use a different generated Security Salt.

Another easy CakePHP tip that works well. Enjoy.

Limit CakePHP2 Results by YEAR, MONTH or DAY

You can never have to many tricks up your sleeves when it comes to pulling the data you need from your database. Sometimes you only want to return a limited set of results based on the year, month or day. So how do you do this?

The solution is quite simple. The following code can be used as part of a find(‘all’), find(‘list’) query or as part of a containable condition.

You could remove the PHP date() function and pass in variables rather than fixing it to the current year, month or day. The YEAR(). MONTH() and DAY() functions are special MySQL functions that wrap around your Model.date field (or any date field in your DB) field and pulls only the relevant results.

Another good simple CakePHP trick!

CakePHP2 Find Neighbors Method with UUID Primary Keys

I recently decided to switch over an application I have been developing from using INT primary keys to use CakePHP’s ability to generate UUID primary keys. It wasn’t difficult to do, you simply set the primary key to BINARY(36) and Cake will do the rest. (the CakePHP manual recommends VARCHAR(36) or CHAR(36) however do a bit of reading and you’ll find that these are not the quickest)

That said there are always problems that you don’t foresee  as a result of a small change like this. The problem I faced was that records could no longer be sorted by id. As it was I generally don’t sort by id but by a unique field with a simple ASC or DESC sort. This works most of the time except when you want to find the previous or next record…

In one instance I was using the find by Neighbors method to create some simple NEXT and PREV links to move between records. The task is simple with the follow code when using INT as a primary key.

That works well with ID’s but not with UUID’s. So what can be done. Well you need another field in your model that you can sort your data by. This may mean that you have to create a “sort” field or perhaps like me you have a simple “date” field in your model. In my application this works well because I have only one date for each week of the year so I never have a record with the same date. Here’s the solution I eventually got working.

It requires an extra line of code. The magic findById method is used and then we can set the value of “value” to “$order[‘Model’][‘date’]” and set the “field” value to “date”.

This returns a nice array for you to use in your view.

A simple tidy solution to what at first seemed complicated.