Next Chapter: Creating a Sitemap for SEO

CakePHP 4: Adding User Data

2019-05-07 17:43:11+00

I as a end user want to have my knowledge into the application.

Acceptance Criteria
  • For each entry I must enter a title
  • for each entry I can enter a url and or a text
  • for each entry I can enter multiple tags

Implementation

CREATE TABLE entry (
    id character(36) NOT NULL,
    user_id character(36) NOT NULL,
    created timestamp with time zone DEFAULT ('now'::text)::timestamp with time zone,
    modified timestamp with time zone DEFAULT ('now'::text)::timestamp with time zone,
    etyp character(1) NOT NULL,
    etitle character varying(80) NOT NULL,
    etags character varying(255),
    eurl character varying(255),
    etext character varying(12000),
    etag jsonb,
    CONSTRAINT entry_etyp_check 
         CHECK ((etyp = ANY (ARRAY['l'::bpchar, 'i'::bpchar, 'v'::bpchar, 't'::bpchar, 'd'::bpchar])))
);

CREATE TABLE user (
    id character(36) NOT NULL,
    nick character varying(24) NOT NULL,
    email character varying(255) NOT NULL,
    lang character(2) DEFAULT 'en'::bpchar,
    last_login timestamp with time zone,
    created timestamp with time zone DEFAULT ('now'::text)::timestamp with time zone,
    modified timestamp with time zone DEFAULT ('now'::text)::timestamp with time zone
);

ALTER TABLE ONLY user
    ADD CONSTRAINT user_pkey PRIMARY KEY (id);

CREATE UNIQUE INDEX entry_eurl ON entry 
     USING btree (user_id, eurl, etags) WHERE (eurl IS NOT NULL);

CREATE INDEX entry_tags_index ON entry USING gin (etag);

CREATE INDEX entry_user ON entry USING btree (user_id);

CREATE UNIQUE INDEX user_email ON user USING btree (email);

CREATE UNIQUE INDEX user_nick ON user USING btree (nick);

ALTER TABLE ONLY entry
    ADD CONSTRAINT entry_user_fk FOREIGN KEY (user_id) REFERENCES user(id);

Baking models, Controller and Templates

As we have precisely defined our database structure we can generate models, controllers and templates.

<pre> bin/cake bake model user </pre>
One moment while associations are detected.

Baking table class for User...

Wrote `/usr/.../src/Model/Table/UserTable.php`

Baking entity class for User...

Wrote `/usr/.../src/Model/Entity/User.php`

Baking test fixture for User...

Wrote `/usr/.../tests/Fixture/UserFixture.php`
Bake is detecting possible fixtures...

Baking test case for App\Model\Table\UserTable ...

Wrote `/usr/.../tests/TestCase/Model/Table/UserTableTest.php`
Done

Now we generate the template for Entry.

<pre> bin/cake bake template entry </pre>

Baking `index` view template file...

Creating file /usr/.../src/../templates/Entry/index.php
Wrote `/usr/.../src/../templates/Entry/index.php`

Baking `view` view template file...

Creating file /usr/.../src/../templates/Entry/view.php
Wrote `/usr/.../src/../templates/Entry/view.php`

Baking `add` view template file...

Creating file /usr/.../src/../templates/Entry/add.php
Wrote `/usr/.../src/../templates/Entry/add.php`

Baking `edit` view template file...

Creating file /usr/.../src/../templates/Entry/edit.php
Wrote `/usr/.../src/../templates/Entry/edit.php`

Tag: cakephp4

Previous Chapter: Configuring Nginx for CakePHP 4 with Subfolders

Next Chapter: Creating a Sitemap for SEO