Persisting Model state with insert , update , save , delete
Prev Chapter 12. Models Next

Persisting Model state with insert , update , save , delete

Persisting changes is as simple as calling save () for INSERT s or UPDATE s and delete () for DELETE s. Let's take a look:

Example 12.3.  Persisting changes with save and delete

<?php
$post = new Post();
$post->title = 'Hello World';
$post->body = 'Welcome to Models in Recess!';
$post->save(); // internally calls $post->insert()
$post->title = 'Hello World! With a Bang!';
$post->save(); // internally calls $post->update()
echo 'New Post ID: ', $post->id;

$oldPost = new Post(10); // Post with ID 10
if($oldPost->exists()) {
    $oldPost->delete();
}

$postsWithRuby = new Post();
$postsWithRuby->like('title', '%Ruby%')->delete();
?>

In lines 1-7 a new Post is created saved and updated with some filler values. Lines 9-12 delete a Post with an ID of 10. Finally lines 14-15 delete all posts containing 'Ruby' in the title. Recess has support for cascading deletes across relationships which is discussed in the following chapter, Model Relationships.


Prev Up Next
Querying Models Home Chapter 13. Relationships Between Models