CakePHP
Database naming conventions
- Table name:
projects
,project_statuses
,customers
,addresses
- If not sure, try
Inflector::pluralize('project_status')
- If not sure, try
- Primary key:
id
- Foreign keys:
customer_id
,project_status_id
- Self reference:
parent_id
- Self reference:
- Display field: Call it
name
, to automatically use it when baking - Timestamp: The fields
created
andmodified
will be automatically updated
Steps to add a field manually to a CakePHP model
- Add the field in the database
- Add the property to the
Model/Entity
header as@property
- Add the property to the
$_accessible
list - Clear the
_cake_model
cache
Add a record with a field filled in from request parameters
In the Controllers function add()
if ($this->request->is('post')) {
// [...]
} else { // add this
$entityName->fieldName = $this->request->getQuery("fieldName");
}
Do not redirect to list after saving a form
In the Controllers function edit($id = null)
if ($this->request->is(['patch', 'post', 'put'])) {
$entityName = $this->Entity->patchEntity($entityName, $this->request->getData());
if ($this->Entity->save($entityName)) {
$this->Flash->success(__('The entity has been saved.'));
// return $this->redirect(['action' => 'index']); // remove this
} else { // add else
$this->Flash->error(__('The entity could not be saved. Please, try again.'));
}
}