5.1.3 Inserting, Updating and Deleting
In order to simplify inserting, updating and deleting database rows, Zend_Db_Adapter provides the functions
insert(), update() and delete() respectively.
Inserting and updating use the same basic premise in that you provide an associative array of data and it
does the rest. Listing 5.3 shows how to add a user to a table called ???users??™.
Listing 5.3: Inserting a row using Zend_Db_Adapter
// insert a user
$data = array( |#1
'date_created' => date('Y-m-d'), |
'date_updated' => date('Y-m-d'), |
'first_name' => 'Ben', |
'surname' => 'Ramsey' |
);
$table = 'users';
$rows_affected = $db->insert($table, $data);
$last_insert_id = $db->lastInsertId(); #2
(annotation) <#1 Associative array of data.>
(annotation) <#2 The id of the row just inserted.>
Note that Zend_Db_Adapter??™s insert() method will automatically ensure that your data is quoted correctly
as it creates an insert statement that uses parameter binding in the same manner as we discussed in section
5.2.2
Updating a record using the update() method works petty much the same way except that you need to pass
in a SQL condition to limit the update to the rows that you are interested in. Typically, you would know the id
of the row you want to update, however update() is flexible enough to allow for updating multiple rows as
shown in Listing 5.
Pages:
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155