CRUD Operations
With the database connection from the previous page in place, you're ready for the four operations almost every app needs: Create, Read, Update, and Delete — collectively known as CRUD. CakePHP's ORM gives each one a small, consistent set of method calls.
Create: Building and Saving a New Record
Creating a record is a three-step pattern: get an empty entity, fill it with data, then save it.
$article = $this->Articles->newEmptyEntity();
$article = $this->Articles->patchEntity($article, $data);
$this->Articles->save($article);
newEmptyEntity() gives you a blank Article entity (the row-level object
introduced in Models & the ORM), patchEntity()
copies an array of submitted data onto it (applying validation rules along the way), and
save() writes it to the database.
Read: Fetching Records
Reading has two common shapes depending on whether you want many rows or a single one by its primary key.
// Many records
$articles = $this->Articles->find()->all();
// One record by primary key
$article = $this->Articles->get($id);
find()->all() returns a collection of every matching row (optionally filtered with
where(), as shown on the previous page), while get($id) fetches exactly
one record by its ID and throws a "not found" exception if it doesn't exist.
Update: Same save(), Different Starting Point
Updating looks almost identical to creating — the difference is you start from an entity you fetched from the database instead of a brand new empty one.
$article = $this->Articles->get($id);
$article = $this->Articles->patchEntity($article, $data);
$this->Articles->save($article);
The same save() method handles both inserts and updates — CakePHP checks whether the
entity is "new" (never saved before) or already has a primary key from the database, and
automatically runs an INSERT or an UPDATE accordingly. You never call a
separate "update" method.
Delete: Removing a Record
Deleting follows the same fetch-first pattern — you get the entity, then hand it to
delete().
$article = $this->Articles->get($id);
$this->Articles->delete($article);
Worked Example: A Full add() Action
In a real controller, one action typically handles both showing an empty form (a GET
request) and saving the submitted data (a POST request). CakePHP's request object
tells you which case you're in.
<?php
namespace App\Controller;
class ArticlesController extends AppController
{
public function add()
{
$article = $this->Articles->newEmptyEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity(
$article,
$this->request->getData()
);
if ($this->Articles->save($article)) {
$this->Flash->success('The article has been saved.');
return $this->redirect(['action' => 'index']);
}
$this->Flash->error('Could not save the article.');
}
$this->set('article', $article);
}
}
On a plain GET request, the if block is skipped entirely — an empty
$article entity is set for the view (see
Views & Layouts), which renders a blank form. On POST,
patchEntity() merges the submitted form data and save() attempts to write
it, redirecting on success or re-showing the form with the entered data (and validation errors) on
failure.