9854-888-021 New York, NY
Posted on / by admin / in Uncategorized

CakePHP: Model

o hi..I was too busy form past few days.. didn’t had tm 2 update it so lets start..
BAsically cake php is a php framework, based on MVC (Model – View – Controller)architecture. Model belongs to data. it directly associated with DB. We can definecertain DB properties, primary keys, table relationships in to model. so overall model can be think as a Database Proxy.hmm I know what question is in your mind.. Why I need Model?? hmm rite?.. ok let me explainBAsically model is abstract view of database. all the complex operation on databbase cas bedone with ease by using model functions. suppose you hav 40 columns in your DB, for inserting data you need to write a long query (near abt 1000 words), but using model just use Model::save() function.. Cool..!! no…..

need more ok..Now another cool feature is data validation. suppose you want that password must be 6-15 digit longand username must not be blank. In traditional design you need to write a long code which may take 15 minuts.But here just define it in model in few seconds and u r done!! .. A lots of more features are there.. I will tell you in next posts.

Now lets create a simple model, User

<?php
/** * Constructing model for the application. This model maps ‘addresses’ * table in database. It receives command from controller and applies * it to database */

class User extends AppModel {
var $name = ‘User’; // Name of Model, We will use this name in our controller and view to refer ‘tbl_user’ table.

var $useTable = ‘tbl_user’; // Name of table
var $primaryKey = ‘user_id’; // Primary key

// Validation criteria
var $validate = array( ‘username’ => array(‘rule’ => array(‘between’, 6, 15), ‘message’ => ‘Username must 6 – 15 character long.’), ‘password’ => array(‘rule’ => array(‘between’, 6, 15), ‘message’ => ‘Password must 6 – 15 character long.’), ’email’ => array(‘rule’ => array(’email’), ‘message’ => ‘Please enter valid email’), ‘first_name’ => array(‘rule’ => array(‘between’, 1, 1000), ‘message’ => ‘Please enter first name.’), ‘last_name’ => array(‘rule’ => array(‘between’, 1, 1000), ‘message’ => ‘Please enter last name.’), ‘address_line_1’ => array(‘rule’ => array(‘between’, 1, 1000), ‘message’ => ‘Please enter address’), ‘city’ => array(‘rule’ => array(‘between’, 1, 1000), ‘message’ => ‘Please enter city.’), ‘country’ => array(‘rule’ => array(‘between’, 1, 1000), ‘message’ => ‘Please select country’), ‘day_phone’ => array(‘rule’ => array(‘between’, 1, 20), ‘message’ => ‘Please enter valid phone number.’), ‘zip’ => array(‘rule’ => array(‘between’, 1, 7), ‘message’ => ‘Please enter valid zip code.’) );

}

?>

Leave a Reply