From 52591ff34d95a415da2da13ee19736cbd8deb501 Mon Sep 17 00:00:00 2001 From: Justin Busschau Date: Fri, 2 Nov 2012 17:29:16 +0000 Subject: [PATCH] Add the replace() method --- core/MY_Model.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/MY_Model.php b/core/MY_Model.php index 6ac7019..3e416ce 100644 --- a/core/MY_Model.php +++ b/core/MY_Model.php @@ -361,6 +361,28 @@ public function update_all($data) return $result; } + /** + * Replaces a record + * If the record specified by $where can be found, use update, + * Else use Insert to add the record + * + * @param array $where The criteria to find the record we want to replace + * @param array $data The data to update/insert + * @return bool + */ + public function replace($where, $data) + { + $row = $this->get_by($where); + if ($row) + { + return $this->update($row->$this->primary_key, $data); + } + else + { + return ($this->insert($data) > 0); + } + } + /** * Delete a row from the table by the primary value */