From 715ba4df3faccf9839f7e194bf41078cae91027b Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Tue, 10 Aug 2010 19:46:10 +0200 Subject: add CommonTable->get() and CommonTable->find() methods --- class/CommonTable.php | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) (limited to 'class') diff --git a/class/CommonTable.php b/class/CommonTable.php index a7d029d..7e1dd8e 100644 --- a/class/CommonTable.php +++ b/class/CommonTable.php @@ -30,7 +30,7 @@ * @license http://www.gnu.org/licenses/agpl-3.0-standalone.html AGPL License 3.0 or (at your option) any later version * @link http://github.com/remicollet/rpmphp/ * @since The begining of times. -*/ + */ abstract class CommonTable { @@ -116,6 +116,38 @@ abstract class CommonTable return $id; } + /** + * Find a row from the table (the first matching criteria) + * + * @param hastable $crit array of field name => value + * + * @return hashtable (the first row) + */ + function find(array $crit) + { + if (count($crit)) { + foreach ($this->request($crit) as $row) { + return $row; + } + } + return false; + } + + /** + * Read a row from the table + * + * @param integer $id of the row + * + * @return hashtable + */ + function get($id) + { + if (intval($id)>0) { + return $this->find(array('id'=>$id)); + } + return false; + } + /** * Delete a row in the table * @@ -160,6 +192,11 @@ abstract class CommonTable $link = 'SET'; foreach ($fields as $key => $value) { + if ($key=='id') { + // Don't update id + continue; + } + $sql .= "$link `$key`="; if (is_null($value)) { $sql .= 'NULL'; -- cgit