Skip to content

Instantly share code, notes, and snippets.

@ericvanjohnson
Last active June 19, 2017 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericvanjohnson/c77417182cdb396ad26da29bb5b510bf to your computer and use it in GitHub Desktop.
Save ericvanjohnson/c77417182cdb396ad26da29bb5b510bf to your computer and use it in GitHub Desktop.
Notes from CakePHP Lightning Talk at SDPHP June 14, 2017

Create New Cake Project

composer create-project --prefer-dist cakephp/app bookmarker

Start CakePHP Internal Server

bin/cake server

Show the landing page

http://localhost:8765/

Connect to MySQL

mysql -u root -p

Show databases

show databases;

Create cakephp application database

create database cakephp_bookmarks;

Show databases again

show databases;

Use the Bookmark database

use cakephp_bookmarks;

Show tables;

show tables;

Create some tables

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    email VARCHAR(255) NOT NULL,
    password VARCHAR(255) NOT NULL,
    created DATETIME,
    modified DATETIME
);

CREATE TABLE bookmarks (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    title VARCHAR(50),
    description TEXT,
    url TEXT,
    created DATETIME,
    modified DATETIME,
    FOREIGN KEY user_key (user_id) REFERENCES users(id)
);

CREATE TABLE tags (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255),
    created DATETIME,
    modified DATETIME,
    UNIQUE KEY (title)
);

CREATE TABLE bookmarks_tags (
    bookmark_id INT NOT NULL,
    tag_id INT NOT NULL,
    PRIMARY KEY (bookmark_id, tag_id),
    FOREIGN KEY tag_key(tag_id) REFERENCES tags(id),
    FOREIGN KEY bookmark_key(bookmark_id) REFERENCES bookmarks(id)
);

Show tables were created

show tables;

Explain a couple tables

explain bookmarks;
explain tags;

Exit MySQL

Show one more time the file system

Now it's time to do some baking

First edit the Database configuration

vi config/app.php

Search for Datasource and update the connection information

Bake stuff

bin/cake bake all users
bin/cake bake all bookmarks
bin/cake bake all tags

Demo using the CRUD system

Show the weird step of needing to add hashing to the user model

vi src/Model/Entity/User.php

Add

use Cake\Auth\DefaultPasswordHasher;

/* Code from the bake, then also add */
protected function _setPassword($value)
    {
        $hasher = new DefaultPasswordHasher();
        return $hasher->hash($value);
    }

Turn of Foreign key checking and truncate data

SET FOREIGN_KEY_CHECKS = 0;
truncate bookmarks;
truncate users;
SET FOREIGN_KEY_CHECKS = 1;

Create a new Post Migration

bin/cake bake migration CreatePosts title:string story:text created modified
bin/cake migrations migrate

Log into database and EXPLAIN new table

show tables;

New table called phinxlog which captures the migrations as well as a posts table

explain posts;

Take a second to create migrations for the other tables. We will exclude the Posts table by adding the --require-table switch to Initial. Since we haven't created a model for Posts yet this should exclude the creation of the migration.

bin/cake bake migration_snapshot Initial --require-table

Since we don't want to run the migration, we will mark the migration as migrated

bin/cake migrations mark_migrated

Modify the Posts table to add User ID.

bin/cake bake migration AddUserIdToPosts user_id:integer

Bake the Post Table

bin/cake bake all posts

Let's show some database access without code. Duplicate the posts table including the data

CREATE TABLE articles AS SELECT * FROM posts;

Drop into the console

bin/cake console
use Cake\ORM\TableRegistry;
$articles = TableRegistry::get('Articles');
$articles->find()->all();

Show example of the Controller to Model Auto Binding

Demonstrate the Inflector

use Cake\Utility\Inflector;
echo Inflector::pluralize('Apple');
echo Inflector::humanize('big_apple');
echo Inflector::underscore('BigApple');
echo Inflector::slug('Big Apple');
echo Inflector::singularize('Apples');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment