CakePHP Application Development

"Models
In CakePHP, a model represents a particular database table. Each database table should have a model representing it. So, in CakePHP, every database table has its own model. All PHP code related to accessing, adding, modifying or deleting records from the table are situated in the model. The model also contains code that defines its relationship with other models. Other than that, the model also defines the data validation rules when adding or updating data for that model. Model can be thought of as the data layer of the application." (Chapter 1)

I have a table called products. The model that represents that table is defined in app/models/product.php. This file begins with the definition of the class:

<?php
class Product extends AppModel
{
..........
}
?>


In the "products" table I have these two fields:

  1. short_description
  2. sales_projection

When adding or modifying products, I wanted to make it mandatory to provide values for both fields. In the database I used "NOT NULL" for both fields:

ALTER TABLE `products` ADD `sales_projection` DOUBLE(10,2) NOT NULL AFTER `city_id`;
ALTER TABLE `products` ADD `short_description` VARCHAR(250) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL AFTER `sales_projection`;


Other than using NOT NULL in the database, in the CakePHP model I defined the data validation rules when adding or updating data for that model.

<?php
class Product extends AppModel
{
var $name = 'Product';
..........
function __construct($id = false, $table = null, $ds = null)
{
parent::__construct($id, $table, $ds);
$this->validate = array(
..........
'short_description' => array(
'rule' => 'notempty',
'allowEmpty' => false,
'message' => __l('Required')
) ,
'sales_projection' => array(
'rule' => 'notempty',
'allowEmpty' => false,
'message' => __l('Required')
) ,
..........
);
..........
}
}


Created: 2019-11-29 18:53:36 Last updated: 2019-11-29 18:53:36

"It is a convention in Cake to have plural words for table names." (Chapter 3)

All of my tables have plural words in the database that I use for a CakePHP e-commerce website.

Created: 2019-12-03 15:37:58 Last updated: 2019-12-03 15:37:58

"All tables in Cake applications' database must have id as the primary key for the table." (Chapter 3)

All of my tables in a CakePHP e-commerce website have the id field that works as the primary key of the table.

Created: 2019-12-03 15:59:34 Last updated: 2019-12-03 15:59:34

"Note
Conventions in CakePHP
Database tables used with CakePHP should have plural names.
All database tables should have a field named id as the primary key of the table." (Chapter 3)

I am following conventions in CakePHP for an e-commerce website: the names of my tables are plural words in the database and all of my tables have the id field that works as the primary key of the table.

Created: 2019-12-03 16:14:28 Last updated: 2019-12-03 16:14:28

Top