Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Noter using Cakephp Mysql and Docker

Setup of Noter in CakePHP, MySQL and Docker (and other tools) - notes, commands, snippets go here.

Direct branch-URL - https://github.com/konnector-dev/noter/tree/cakephp-mysql

Repo URL - https://github.com/konnector-dev/noter

Branch - cakephp-mysql

Missed commands in the first version:

  1. git clone git@github.com:konnector-dev/noter.git
  2. cd noter
  3. git checkout cakephp-mysql
  4. docker-compose run noter composer install -n --prefer-dist
  5. docker-compose run noter bin/cake migrations migrate
  6. docker-compose run noter bin/cake migrations seed
  7. docker-compose up --build
@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

Dockerfile

FROM jdecode/kode:2.1

COPY php/ini/conf.d/memory_limit.ini /usr/local/etc/php/conf.d/memory_limit.ini

RUN a2enmod rewrite
#change uid and gid of apache to docker user uid/gid
RUN usermod -u 1000 www-data && groupmod -g 1000 www-data

#change ownership of our applications
RUN chown -R www-data:www-data /var/www/html/tmp /var/www/html/logs

ARG BUILD
ENV BUILD=${BUILD}

## Disabled following when running locally (keep it enabled for GCP Cloud Run)
RUN if [ "$BUILD" = "local" ] ; then ls -al ; else sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf ; fi

docker-compose.yml

version: "3"
services:
  noter:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        BUILD: 'local'
    image: noter:1.1
    ports:
      - 8080:80
    volumes:
      - .:/var/www/html
    links:
      - mysql
    depends_on:
      - mysql
    environment:
      DEBUG: 'true'
      SECURITY_SALT: 49e558df8e733a6daedbe054a9ccd7add9d9a6efa8a96cf3248815c364b47391
      DATABASE_URL: mysql://devuser:devpass@183.83.1.2/noter
    networks:
      testing_net:
        ipv4_address: 183.83.1.1

  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: noter
      MYSQL_USER: devuser
      MYSQL_PASSWORD: devpass
    ports:
      - 9906:3306
    networks:
      testing_net:
        ipv4_address: 183.83.1.2
volumes:
  db_data: {}
networks:
  testing_net:
    ipam:
      driver: default
      config:
        - subnet: 183.83.0.0/16
@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

Run docker-compose up --build to run docker-compose/Dockerfile

The CakePHP site should be running now at 183.83.1.1

Screenshot from 2020-08-31 11-05-54

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

Original at - https://gist.github.com/jdecode/26dd814527b6159a989f0ea1652533aa#gistcomment-3435777

Copied below for quick reference - when in doubt, check the original at the above link.

DB structure for users table

  • id INT (11) Primary Key Auto-Increment Indexed
  • uuid UUID Indexed
  • email VARCHAR (200) Indexed
  • password VARCHAR (100) : the length would depend on the hashing approach
  • name VARCHAR(100)
  • active TINYINT(1)
  • email_verified TINYINT(1)
  • email_verified_at DATE/TIME or INT(11)
  • remember_me_token VARCHAR(40) Indexed : would contain SHA1 hashed random string
  • created DATE/TIME or INT(11)
  • modified DATE/TIME or INT(11)
@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

CakePHP bake command to create migration (to be used to create the DB table) based on the table columns specifications:

bin/cake bake migration CreateUsers uuid:uuid[64]:index:UUID_INDEX email:string?:index:EMAIL_INDEX password:string? name:string? active:boolean? email_verified:boolean? email_verified_at:datetime? remember_me_token:string?:index:REMEMBER_ME_TOKEN_INDEX created:datetime? modified:datetime?

Docker tip: If you are using docker-compose/Docker, then prefix the above command with docker-compose run noter (noter is the name of the "service" in docker-compose, it could be different based on your docker-compose.yml file), so that the full command now is :

docker-compose run noter bin/cake bake migration CreateUsers uuid:uuid[64]:index:UUID_INDEX email:string?:index:EMAIL_INDEX password:string? name:string? active:boolean? email_verified:boolean? email_verified_at:datetime? remember_me_token:string?:index:REMEMBER_ME_TOKEN_INDEX created:datetime? modified:datetime?

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

The above command will create a migrations file (at config/Migrations) - you can check it to confirm if the columns have the specifications as expected, or you can modify this file as per the changes needed.

Next, run the migration to make desired changes to the DB by using following command:

bin/cake migrations migrate

Docker tip: If you are using docker/docker-compose, then prefix the above command with docker-compose run noter (where noter is the service name in docker-compose.yml file)

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

As of here, the table is created, now we run "bake" command to generate Controller, Model, Entity, View(templates) for users:

bin/cake bake all users

If "users" is omitted, then a prompt would show up asking for selecting the table/entity to be baked.

Docker tip (again): Prefix the command with docker-compose run noter, and remember - omitting "users" would not prompt anything, the command will not be executed (need to find the right flag to wait for prompt, and for now use the full command, including "users" at the end, specifying the table, hence not having the need for any prompt)

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

Technically, no PHP code written so far.

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

The bin/cake bake all users generates scaffolding code for Model, View and Controller.

Now /users URL (in this case http://183.83.1.1/users) will take you to a "listing" page created by "bake" command and it will look like the following screenshot (as of CakePHP 4.1.3 Strawberry):

Screenshot from 2020-08-31 17-58-25

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

Adding screenshots for CRUD pages

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Aug 31, 2020

3eb26330-ae34-43ae-a14d-b9ca5252139b

Key things to notice:

  • UUID is "not-so-famous" data type, so the input field for that is "non-stylized"
  • Email field is having "type=email" (HTML source code not in screenshot)
  • Password field is having "type=password"
  • The TINYINT(1) columns are handled as "boolean" by default, and hence the "checkbox", depicting a TRUE/FALSE or 1/0 state
  • Date/time column (email_verified_at) has a date/time picker and also supports keyboard-navigation as well (pretty awesome)
    Screenshot from 2020-08-31 18-54-35
@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Sep 1, 2020

Adding a couple of users (plain text entries, no file uploads or any other kind of logic, yet), makes the user listing page like this:

Screenshot from 2020-09-01 12-59-09

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Sep 1, 2020

"View" user (details page)

a03665c4-0807-4328-b89f-1316e4dfa082

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Sep 1, 2020

Deleting a user prompts the javascript "confirm" popup box

Screenshot from 2020-09-01 14-14-03

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Sep 1, 2020

So far - not a single hand-written code in PHP!

@jdecode

This comment has been minimized.

Copy link
Owner Author

@jdecode jdecode commented Sep 5, 2020

admins

  • id
  • uuid
  • login
  • password
  • name
  • active
  • remember_me_token
  • last_logged_in
  • created
  • modified

bin/cake bake migration CreateAdmins uuid:uuid[64]:index:UUID_INDEX login:string?:index:LOGIN_INDEX password:string? name:string? active:boolean? remember_me_token:string?:index:REMEMBER_ME_TOKEN_INDEX last_logged_in:datetime? created:datetime? modified:datetime?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment