Basic CRUD

Version 4 (Kien La, 2010-06-19 06:26 PM)

1 1
h2. Basic CRUD
2 1
3 4 Kien La
*(#topic-list) "Create":/projects/main/wiki/Basic_CRUD#create
4 4 Kien La
* "Read":/projects/main/wiki/Basic_CRUD#read
5 4 Kien La
* "Update":/projects/main/wiki/Basic_CRUD#update
6 4 Kien La
* "Delete":/projects/main/wiki/Basic_CRUD#delete
7 4 Kien La
8 3 Kien La
"CRUD":http://en.wikipedia.org/wiki/Create,_read,_update_and_delete as defined by Wikipedia:
9 1
10 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.
11 1
12 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.
13 1
14 4 Kien La
h4(#create). Create
15 1
16 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.
17 1
18 1
<pre class="code"><code class="php">
19 1
$post = new Post();
20 1
$post->title = 'My first blog post!!';
21 1
$post->author_id = 5;
22 1
$post->save();
23 1
# INSERT INTO `posts` (title,author_id) VALUES('My first blog post!!', 5)
24 1
25 1
# the below methods accomplish the same thingg
26 1
27 1
$attributes = array('title' => 'My first blog post!!', 'author_id' => 5);
28 1
$post = new Post($attributes);
29 1
$post->save();
30 1
# same sql as above
31 1
32 1
$post = Post::create($attributes);
33 1
# same sql as above
34 1
</code></pre>
35 1
 
36 4 Kien La
h4(#read). Read
37 1
38 1
These are your basic methods to find and retrieve records from your database. See the [[Finders]] section for more details.
39 1
40 1
<pre class="code"><code class="php">
41 1
$post = Post::find(1);
42 1
echo $post->title; # 'My first blog post!!'
43 1
echo $post->author_id; # 5
44 1
45 1
# also the same since it is the first record in the db
46 1
$post = Post::first();
47 1
 
48 1
# using dynamic finders
49 1
$post = Post::find_by_name('The Decider');
50 1
$post = Post::find_by_name_and_id('The Bridge Builder',100);
51 1
$post = Post::find_by_name_or_id('The Bridge Builder',100);
52 1
 
53 1
# using some conditions
54 1
$posts = Post::find('all',array('conditions' => array('name=?','The Bridge Builder')));
55 1
</code></pre>
56 1
 
57 4 Kien La
h4(#update). Update
58 1
59 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.
60 1
61 1
<pre class="code"><code class="php">
62 1
$post = Post::find(1);
63 1
echo $post->title; # 'My first blog post!!'
64 1
$post->title = 'Some real title';
65 1
$post->save();
66 1
# UPDATE `posts` SET title='Some real title' WHERE id=1
67 1
68 1
$post->title = 'Some other title';
69 1
$post->author_id = 1;
70 1
$post->save();
71 1
# UPDATE `posts` SET title='Some other title', author_id=1 WHERE id=1
72 1
</code></pre>
73 1
 
74 4 Kien La
h4(#delete). Delete
75 1
76 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.
77 1
78 1
<pre class="code"><code class="php">
79 1
$post = Post::find(1);
80 1
$post->delete();
81 1
# DELETE FROM `posts` WHERE id=1
82 1
83 1
echo $post->title; # Some other title
84 1
</code></pre>