From 03192a087965e6927a4842e49f5fd825dedd350f Mon Sep 17 00:00:00 2001 From: saintnicster Date: Wed, 14 Sep 2011 16:30:21 -0500 Subject: Copied into GitHub from @kenjis 's bitbucket repo.https://bitbucket.org/kenjis/ci-user-guide/changeset/3d579dd14afe --- user_guide/database/active_record.html | 39 ++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'user_guide/database') diff --git a/user_guide/database/active_record.html b/user_guide/database/active_record.html index bd3c07d88..17c58c9f1 100644 --- a/user_guide/database/active_record.html +++ b/user_guide/database/active_record.html @@ -546,7 +546,7 @@ $data = array(
   )
);

-$this->db->update_batch('mytable', $data); +$this->db->insert_batch('mytable', $data);

// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date') @@ -669,6 +669,41 @@ You can optionally pass this information directly into the update function as a

You may also use the $this->db->set() function described above when performing updates.

+

$this->db->update_batch();

+

Generates an update string based on the data you supply, and runs the query. You can either pass an +array or an object to the function. Here is an example using an array:

+ + +$data = array(
+   array(
+      'title' => 'My title' ,
+      'name' => 'My Name 2' ,
+      'date' => 'My date 2'
+   ),
+   array(
+      'title' => 'Another title' ,
+      'name' => 'Another Name 2' ,
+      'date' => 'Another date 2'
+   )
+);
+
+$this->db->update_batch('mytable', $data, 'title'); +

+// Produces:
+// UPDATE `mytable` SET `name` = CASE
+// WHEN `title` = 'My title' THEN 'My Name 2'
+// WHEN `title` = 'Another title' THEN 'Another Name 2'
+// ELSE `name` END,
+// `date` = CASE
+// WHEN `title` = 'My title' THEN 'My date 2'
+// WHEN `title` = 'Another title' THEN 'Another date 2'
+// ELSE `date` END
+// WHERE `title` IN ('My title','Another title')
+ +

The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.

+ +

Note: All values are escaped automatically producing safer queries.

+  

Deleting Data

@@ -786,4 +821,4 @@ Next Topic:  Transactions - \ No newline at end of file + -- cgit v1.2.3-24-g4f1b