Nico
Nico

Reputation: 589

CakePHP 3 - saving entities id/created/modified not filled in using sqlite

I've worked with cake 1 & 2 in the past, first time I'm using cakephp 3.

I tried to do something fairly simple, connect to an sqlite database and create a record.

I wanted the id, created and modified fields to be automatically filled in. It might be because I'm using sqlite and the table definition is wrong, normally I use mysql or postgres.

My table:

CREATE TABLE account (
    id       CHAR (36) PRIMARY KEY,
    username TEXT      UNIQUE,
    password TEXT      NOT NULL,
    created  INTEGER,
    modified INTEGER
);

And what I'm doing to save data in it:

$account = new Account([
    'username' => $username,
    'password' => $password
]);
TableRegistry::get('Account')->save($account);

I've been reading the docs and for id a char(36) should have done the job like it is mentioned here: http://book.cakephp.org/3.0/en/intro/conventions.html#model-and-database-conventions

Upvotes: 3

Views: 1374

Answers (1)

ndm
ndm

Reputation: 60503

The created and modified fields are only being populated when using the Timestamp behavior, so ensure that you are using it.

Also for CakePHP to be able to properly handle the time fields, you should use a more specific Date/Time type, like DATETIME. SQLite will internally treat these as numerical columns nonetheless, but CakePHP needs these types in order to be able to map the values to the proper database type classes.

As for your id column, remove the whitespace between CHAR and (36), SQLite will return the type as defined, ie with the whitespace, CHAR (36), and CakePHP is looking for CHAR(36), without whitespace.

https://github.com/cakephp/.../blob/3.1.5/src/Database/Schema/SqliteSchema.php#L47

See also

Upvotes: 2

Related Questions