Cake3 … baby steps (step 2 — setting up)

(Get the app code on github.)

Now that we have cake installed, and our fresh application is up and running, it’s time to setup some things and prepare for development.

I am going to use good ol’ MySQL for my DB, so first of all I need to create a database called “todo” and provide the right login credentials in
todo/config/app.php
I am going to setup both the “default” and “test” connections, kind of like so:

'Datasources' => [
    'default' => [
      'className' => 'Cake\Database\Connection',
      'driver' => 'Cake\Database\Driver\Mysql',
      'persistent' => false,
      'host' => 'localhost',
      'username' => 'root',
      'password' => 'password',
      'database' => 'todo',
      'encoding' => 'utf8',
      'timezone' => 'UTC',
      'cacheMetadata' => true,

Next, I hear that cake migrations got even more awesome in the new version. Since they are leveraging Phinx, a very solid migrations tool. So I am definitely going to use cakephp migrations to keep my database changes organized.

The installation is now done via composer.
Just add the following line to your todo/composer.json

"cakephp/migrations": "dev-master"

You can place this in the require{} section.

Now, back to the terminal and your main app directory and simply run:

composer update

This should update any dependencies and install cakephp/migrations plugin.
Don’t forget to add Plugin::load(‘Migrations’); to your config/bootstrap.php

You can verify that the plugin is available by running bin/cake from your main application directory.

Let’s get our first migration setup, by running:

bin/cake migrations create Initial

Basically this will create a file for our first migration. My plan is to have a whole of one table for the application.

We can easily define the table and add it to the database using the migrations plugin.
Open up the migrations file in config/Migrations/[something]_initial.php and let’s take a look at the change() method.

This is where the definition of the table should go. Generally speaking change() method replaced up() and down() for the most part, you can still make use of all if you have to get very granular, but when it comes to creating and dropping (when reverting) a table… change() works perfectly well.

Let’s see how to define a table inside the change() method:

public function change()
    {
        // create the table
        $table = $this->table('todos');
        $table->addColumn('todo', 'string', ['limit' => 200])
              ->addColumn('created', 'datetime')
              ->addColumn('updated', 'datetime')
              ->addColumn('is_done', 'boolean')
              ->create();
    }

Notice, that I don’t even have to supply any information about a Primary Key column.
By default the migration will create and “id” column which is a PK and AUTO_INCREMENT’ed.
Sweet.

Give it a try:

bin/cake migrations migrate

If all goes well, you should now have a new table ready in your database.

Part 3.

Related Posts

%d bloggers like this: