Basic CRUD

Version 15 (Bill Zhao, 2014-10-20 04:21 AM)

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 11 Héctor Ramón Jiménez
* "Massive":/projects/main/wiki/Basic_CRUD#massive
8 4 Kien La
9 3 Kien La
"CRUD":http://en.wikipedia.org/wiki/Create,_read,_update_and_delete as defined by Wikipedia:
10 1
11 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.
12 1
13 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.
14 1
15 4 Kien La
h4(#create). Create
16 1
17 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.
18 1
19 1
<pre class="code"><code class="php">
20 1
$post = new Post();
21 1
$post->title = 'My first blog post!!';
22 1
$post->author_id = 5;
23 1
$post->save();
24 1
# INSERT INTO `posts` (title,author_id) VALUES('My first blog post!!', 5)
25 1
26 15 Bill Zhao
# the below methods accomplish the same thing
27 1
28 1
$attributes = array('title' => 'My first blog post!!', 'author_id' => 5);
29 1
$post = new Post($attributes);
30 1
$post->save();
31 1
# same sql as above
32 1
33 1
$post = Post::create($attributes);
34 1
# same sql as above
35 1
</code></pre>
36 1
 
37 4 Kien La
h4(#read). Read
38 1
39 1
These are your basic methods to find and retrieve records from your database. See the [[Finders]] section for more details.
40 1
41 1
<pre class="code"><code class="php">
42 1
$post = Post::find(1);
43 1
echo $post->title; # 'My first blog post!!'
44 1
echo $post->author_id; # 5
45 1
46 1
# also the same since it is the first record in the db
47 1
$post = Post::first();
48 1
 
49 1
# using dynamic finders
50 1
$post = Post::find_by_name('The Decider');
51 1
$post = Post::find_by_name_and_id('The Bridge Builder',100);
52 1
$post = Post::find_by_name_or_id('The Bridge Builder',100);
53 1
 
54 1
# using some conditions
55 1
$posts = Post::find('all',array('conditions' => array('name=?','The Bridge Builder')));
56 1
</code></pre>
57 1
 
58 4 Kien La
h4(#update). Update
59 1
60 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.
61 1
62 1
<pre class="code"><code class="php">
63 1
$post = Post::find(1);
64 1
echo $post->title; # 'My first blog post!!'
65 1
$post->title = 'Some real title';
66 1
$post->save();
67 1
# UPDATE `posts` SET title='Some real title' WHERE id=1
68 1
69 14 Benjamin P
$post->update_attributes(array('title' => 'Some other title', 'author_id' => 1));
70 1
# UPDATE `posts` SET title='Some other title', author_id=1 WHERE id=1
71 1
</code></pre>
72 1
 
73 4 Kien La
h4(#delete). Delete
74 1
75 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.
76 1
77 1
<pre class="code"><code class="php">
78 1
$post = Post::find(1);
79 1
$post->delete();
80 1
# DELETE FROM `posts` WHERE id=1
81 1
82 1
echo $post->title; # Some other title
83 1
</code></pre>
84 5 Héctor Ramón Jiménez
85 8 Héctor Ramón Jiménez
h4(#massive). Massive Update or Delete
86 9 Héctor Ramón Jiménez
87 5 Héctor Ramón Jiménez
You can do a massive update or massive delete easily. Look at this example:
88 1
89 10 Héctor Ramón Jiménez
<pre class="code"><code class="php">
90 5 Héctor Ramón Jiménez
# MASSIVE UPDATE
91 5 Héctor Ramón Jiménez
# Model::table()->update(AttributesToUpdate, WhereToUpdate);
92 5 Héctor Ramón Jiménez
Post::table()->update(array('title' => 'Massive title!', /* Other attributes... */, array('id' => array(1, 3, 7));
93 5 Héctor Ramón Jiménez
# UPDATE `posts` SET title = `Massive title!` WHERE id IN (1, 3, 7)
94 5 Héctor Ramón Jiménez
95 5 Héctor Ramón Jiménez
# MASSIVE DELETE
96 5 Héctor Ramón Jiménez
# Model::table()->delete(WhereToDelete);
97 13 Héctor Ramón Jiménez
Post::table()->delete(array('id' => array(5, 9, 26, 30));
98 5 Héctor Ramón Jiménez
# DELETE FROM `posts` WHERE id IN (5, 9, 26, 30)
99 5 Héctor Ramón Jiménez
</code></pre>