Basic CRUD

Version 2 (Kien La, 2010-06-19 01:41 PM)

1 1
h2. Basic CRUD
2 1
3 2 Kien La
"CRUD":http://en.wikipedia.org/wiki/Create,_read,_update_and_delete is defined by Wikipedia:
4 1
5 1
> Create, read, update and delete (CRUD) are the four basic functions of persistent storage, a major part of nearly all computer software. Sometimes CRUD is expanded with the words retrieve instead of read or destroy instead of delete. It is also sometimes used to describe user interface conventions that facilitate viewing, searching, and changing information; often using computer-based forms and reports.
6 1
7 1
In other words, CRUD is the day-to-day tedium of saving and reading data. ActiveRecord removes the remedial and encumbering task of hand-writing SQL queries. Instead, you will only need to write the relevant parts to work with your data.
8 1
9 1
h4. Create
10 1
11 1
This is where you save records to your database. Here we create a new post by instantiating a new object and then invoking the save() method.
12 1
13 1
<pre class="code"><code class="php">
14 1
$post = new Post();
15 1
$post->title = 'My first blog post!!';
16 1
$post->author_id = 5;
17 1
$post->save();
18 1
# INSERT INTO `posts` (title,author_id) VALUES('My first blog post!!', 5)
19 1
20 1
# the below methods accomplish the same thingg
21 1
22 1
$attributes = array('title' => 'My first blog post!!', 'author_id' => 5);
23 1
$post = new Post($attributes);
24 1
$post->save();
25 1
# same sql as above
26 1
27 1
$post = Post::create($attributes);
28 1
# same sql as above
29 1
</code></pre>
30 1
 
31 1
h4. Read
32 1
33 1
These are your basic methods to find and retrieve records from your database. See the [[Finders]] section for more details.
34 1
35 1
<pre class="code"><code class="php">
36 1
$post = Post::find(1);
37 1
echo $post->title; # 'My first blog post!!'
38 1
echo $post->author_id; # 5
39 1
40 1
# also the same since it is the first record in the db
41 1
$post = Post::first();
42 1
 
43 1
# using dynamic finders
44 1
$post = Post::find_by_name('The Decider');
45 1
$post = Post::find_by_name_and_id('The Bridge Builder',100);
46 1
$post = Post::find_by_name_or_id('The Bridge Builder',100);
47 1
 
48 1
# using some conditions
49 1
$posts = Post::find('all',array('conditions' => array('name=?','The Bridge Builder')));
50 1
</code></pre>
51 1
 
52 1
h4. Update
53 1
54 1
To update you would just need to find a record first and then change one of its attributes. It keeps an array of attributes that are "dirty" (that have been modified) and so our sql will only update the fields modified.
55 1
56 1
<pre class="code"><code class="php">
57 1
$post = Post::find(1);
58 1
echo $post->title; # 'My first blog post!!'
59 1
$post->title = 'Some real title';
60 1
$post->save();
61 1
# UPDATE `posts` SET title='Some real title' WHERE id=1
62 1
63 1
$post->title = 'Some other title';
64 1
$post->author_id = 1;
65 1
$post->save();
66 1
# UPDATE `posts` SET title='Some other title', author_id=1 WHERE id=1
67 1
</code></pre>
68 1
 
69 1
h4. Delete
70 1
71 1
Deleting a record will not destroy the object. This means that it will call sql to delete the record in your database, however, you can still use the object.
72 1
73 1
<pre class="code"><code class="php">
74 1
$post = Post::find(1);
75 1
$post->delete();
76 1
# DELETE FROM `posts` WHERE id=1
77 1
78 1
echo $post->title; # Some other title
79 1
</code></pre>