4. Models

Created Saturday 30 August 2014

Models

class Item extends AppModel {
}

Instant Cake (console)

Baking Time!

create table if not exists `categories` (
`id` int(10) not null auto_increment,
`name` varchar(50) not null,
`length_type` varchar(50) not null,
primary key (`id`)
);
./cake bake
What would you like to Bake?
m
Use Database Config: (default/test)
[default] > Press ENTER for default
Possible Models based on your current database:
  1. Category
  2. Item
[q] > 1
Would you like to supply validation criteria for the fields in your model? (y/n)
[y] > n (not a typical decision..)
Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)? (y/n)
[y] > n
Look okay? (y/n)
[y] > y
PHPUnit is not installed. Do you want to bake unit test files anyway? (y/n)
[y] > n
.....
What would you like to Bake?
c
Use Database Config: (default/test)
[default] > Press ENTER for default
Possible Controllers based on your current database:
  1. Categories
  2. Items
[q] > 1
Would you like to build your controller interactively? (y/n)
[y] > y
Would you like to use dynamic scaffolding? (y/n)
[n] > n
Would you like to create some basic class methods (index(), add(), view(), edit())? (y/n)
[n] > y
Would you like to create the basic class methods for admin routing? (y/n)
[n] > n
Would you like this controller to use other helpers besides HtmlHelper and FormHelper (y/n)
[n] > y
Please provide a comma separated list of the other helper names you'd like to use.
Example: 'Text, Js, Time'
> Session
Would you like this controller to use any components? (y/n)
[n] > n
Would you like to use Session flash messages? (y/n)
[y] > y
Look okay? (y/n)
[y] > y
.....
What would you like to Bake?
v
Use Database Config: (default/test)
[default] > Press ENTER for default
Possible Controllers based on your current database:
  1. Categories
  2. Items
[q] > 1
Would you like to build your views interactively? (y/n)
[n] > y
Would you like to create some CRUD views (index, add, view, edit) for this controller? (y/n)
[y] > y
Would you like to create the views for admin routing? (y/n)
[n] > n
> q

Linking Models together

One to One Association

One to Many Association

Many to One Association

Detailed Variables

class Cake extends AppModel {
public $hasOne = array (
'CakeDetail' => array (
'className' => 'CakeDetail',
'dependent' => true
)
);
}

class User extends AppModel {
public $hasMany = array (
'Recipe' => array (
'className' => 'Recipe',
'foreignKey' => 'user_id',
'order' => 'Recipe.added DESC',
'limit' => '5',
'dependent' => true
)
);
}

class CakeDetail extends AppModel {
public $belongsTo = array (
'Cake' => array (
'className' => 'Cake',
'foreignKey' => 'cake_id'
)
);
}

Has One

Has Many

Belongs To

Model Attributes

A Few Model Attributes

class Item extends AppModel {
public $useDbConfig = 'live';
// default
public $useTable = 'products';
// items
public $primaryKey = 'itemID';
// id
public $displayField = 'productName';
// name, title

Data Validation

class User extends AppModel {
public $validate = array (
'login' => array (
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Alphabets and numbers only'
),
'password' => array (
'rule' => array ('minLength', '8'),
'message' => 'Minimum 8 characters long'
),
'email' => 'email',
'born' => array (
'rule' => 'date',
'message' => 'Enter a valid date',
'allowEmpty' => true
)
);
}

Validation Array



Backlinks: