3.7.2 Campi "magici"
Se denominate alcuni campi in accordo alle convenzioni, CakePHP potrà effettuare del lavoro per voi.
3.7.2.1 Data Type Associations by Database
Every RDMS defines data types in slightly different ways. Within the datasource class for each database system, CakePHP maps those types to something it recognizes and creates a unified interface, no matter which database system you need to run on.
This breakdown describes how each one is mapped.
3.7.2.1.1 MySQL
| CakePHP Type | Field Properties |
|---|---|
| primary_key | NOT NULL auto_increment |
| string | varchar(255) |
| text | text |
| integer | int(11) |
| float | float |
| datetime | datetime |
| timestamp | datetime |
| time | time |
| date | date |
| binary | blob |
| boolean | tinyint(1) |
3.7.2.1.2 MySQLi
| CakePHP Type | Field Properties |
|---|---|
| primary_key | DEFAULT NULL auto_increment |
| string | varchar(255) |
| text | text |
| integer | int(11) |
| float | float |
| datetime | datetime |
| timestamp | datetime |
| time | time |
| date | date |
| binary | blob |
| boolean | tinyint(1) |
3.7.2.1.3 ADOdb
| CakePHP Type | Field Properties |
|---|---|
| primary_key | R(11) |
| string | C(255) |
| text | X |
| integer | I(11) |
| float | N |
| datetime | T (Y-m-d H:i:s) |
| timestamp | T (Y-m-d H:i:s) |
| time | T (H:i:s) |
| date | T (Y-m-d) |
| binary | B |
| boolean | L(1) |
3.7.2.1.4 DB2
| CakePHP Type | Field Properties |
|---|---|
| primary_key | not null generated by default as identity (start with 1, increment by 1) |
| string | varchar(255) |
| text | clob |
| integer | integer(10) |
| float | double |
| datetime | timestamp (Y-m-d-H.i.s) |
| timestamp | timestamp (Y-m-d-H.i.s) |
| time | time (H.i.s) |
| date | date (Y-m-d) |
| binary | blob |
| boolean | smallint(1) |
3.7.2.1.5 Firebird/Interbase
| CakePHP Type | Field Properties |
|---|---|
| primary_key | IDENTITY (1, 1) NOT NULL |
| string | varchar(255) |
| text | BLOB SUB_TYPE 1 SEGMENT SIZE 100 CHARACTER SET NONE |
| integer | integer |
| float | float |
| datetime | timestamp (d.m.Y H:i:s) |
| timestamp | timestamp (d.m.Y H:i:s) |
| time | time (H:i:s) |
| date | date (d.m.Y) |
| binary | blob |
| boolean | smallint |
3.7.2.1.6 MS SQL
| CakePHP Type | Field Properties |
|---|---|
| primary_key | IDENTITY (1, 1) NOT NULL |
| string | varchar(255) |
| text | text |
| integer | int |
| float | numeric |
| datetime | datetime (Y-m-d H:i:s) |
| timestamp | timestamp (Y-m-d H:i:s) |
| time | datetime (H:i:s) |
| date | datetime (Y-m-d) |
| binary | image |
| boolean | bit |
3.7.2.1.7 Oracle
| CakePHP Type | Field Properties |
|---|---|
| primary_key | number NOT NULL |
| string | varchar2(255) |
| text | varchar2 |
| integer | numeric |
| float | float |
| datetime | date (Y-m-d H:i:s) |
| timestamp | date (Y-m-d H:i:s) |
| time | date (H:i:s) |
| date | date (Y-m-d) |
| binary | bytea |
| boolean | boolean |
| number | numeric |
| inet | inet |
3.7.2.1.8 PostgreSQL
| CakePHP Type | Field Properties |
|---|---|
| primary_key | serial NOT NULL |
| string | varchar(255) |
| text | text |
| integer | integer |
| float | float |
| datetime | timestamp (Y-m-d H:i:s) |
| timestamp | timestamp (Y-m-d H:i:s) |
| time | time (H:i:s) |
| date | date (Y-m-d) |
| binary | bytea |
| boolean | boolean |
| number | numeric |
| inet | inet |
3.7.2.1.9 SQLite
| CakePHP Type | Field Properties |
|---|---|
| primary_key | integer primary key |
| string | varchar(255) |
| text | text |
| integer | integer |
| float | float |
| datetime | datetime (Y-m-d H:i:s) |
| timestamp | timestamp (Y-m-d H:i:s) |
| time | time (H:i:s) |
| date | date (Y-m-d) |
| binary | blob |
| boolean | boolean |
3.7.2.1.10 Sybase
| CakePHP Type | Field Properties |
|---|---|
| primary_key | numeric(9,0) IDENTITY PRIMARY KEY |
| string | varchar(255) |
| text | text |
| integer | int(11) |
| float | float |
| datetime | datetime (Y-m-d H:i:s) |
| timestamp | timestamp (Y-m-d H:i:s) |
| time | datetime (H:i:s) |
| date | datetime (Y-m-d) |
| binary | image |
| boolean | bit |
3.7.2.2 Titles
An object, in the physical sense, often has a name or a title in which to refer to it by. A person has a name like John or Mac or Buddy. A blog post has a title. A category has a name.
By specifying a title or name field, CakePHP will automatically use this label in various circumstances:
- Scaffolding — page titles, fieldset labels
- Lists — normally used for
<select>drop-downs - TreeBehavior — reordering, tree views
If you have a title and name field in your table, the title will be used.
3.7.2.3 created & modified (o updated)
Questi due campi sono gestiti automaticamente dalle chiamate al metodo save() del modello Model di CakePHP. Ogni volta che si creano dei nuovi record, il campo created verrà automaticamente inserito, mentre il campo modified sarà aggiornato ogni qualvolta saranno fatte delle modifiche. Si noti che un campo denominato updated avrà lo stesso comportamente del campo modified.
Sia l'uno che l'altro, di questi campi speciali, dovranno essere del tipo datetime ed avere valore di default impostato a NULL.
3.7.2.4 Uso delle chiave primarie come UUID
Se la chiave primaria per un modello ('id' o altro valore impostato per mezzo della variabile $primaryKey) viene definita come una char(26), CakePHP creerà automaticamente un UUID da utilizzarsi per l'impostazione del campo.
Questo comportamento è utile specialmente per applicazioni che utilizzano più database.
