From 4fa342c3ce36bc102e5dbe0ba76c7fd5a1351260 Mon Sep 17 00:00:00 2001 From: Jack Freeman Date: Thu, 26 Feb 2015 00:03:43 +0800 Subject: [PATCH] perfect the Westdc\Db\Record --- Westdc/Db/PreDeclare.php | 4 +++ Westdc/Db/Record.php | 77 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 74 insertions(+), 7 deletions(-) diff --git a/Westdc/Db/PreDeclare.php b/Westdc/Db/PreDeclare.php index 615245a..5aa1417 100644 --- a/Westdc/Db/PreDeclare.php +++ b/Westdc/Db/PreDeclare.php @@ -12,4 +12,8 @@ final class PreDeclare { const EVENT_INSERT_PRE = "insert.pre"; const EVENT_INSERT_PROCESS_DATA = "insert.processData"; const EVENT_INSERT_POST = "insert.post"; + + const EVENT_UPDATE_PRE = "update.pre"; + const EVENT_UPDATE_PROCESS_DATA = "update.processData"; + const EVENT_UPDATE_POST = "update.post"; } \ No newline at end of file diff --git a/Westdc/Db/Record.php b/Westdc/Db/Record.php index cc79fd1..7463783 100644 --- a/Westdc/Db/Record.php +++ b/Westdc/Db/Record.php @@ -8,14 +8,23 @@ namespace Westdc\Db; -use Zend\Db\Sql\TableIdentifier; +use Fselab\Db\RecordException; +use Zend\Db\Sql; use Westdc\EventModel\AbstractEventManager; class Record extends AbstractEventManager { + /** + * 向数据库表中写入新数据 + * @param $table + * @param $data + * @param callable $callback + * @return bool + * @throws RecordException + */ public function insert($table,$data,callable $callback = null){ - if(!is_string($table) && !$table instanceof TableIdentifier) + if(!is_string($table) && !$table instanceof Sql\TableIdentifier) throw new RecordException("table must is string or TableIdentifier"); $params = compact('data'); @@ -27,13 +36,67 @@ class Record extends AbstractEventManager { if(!$results->isEmpty()) $data = $results->bottom(); - $dbh = new Dbh; + try{ + $dbh = new Dbh; + $id = $dbh->insert($table,$data,true); + }catch(\Exception $e){ + throw new RecordException($e->getMessage()); + } - $id = $dbh->insert($table,$data,true); + if(!empty($callback) && is_callable($callback)) + call_user_func($callback,$id,$data); - if(!empty($callback)) - if(is_callable($callback)) - call_user_func($callback,$id,$data); + $params = compact('data','id'); + $this->getEventManager()->trigger(PreDeclare::EVENT_INSERT_POST, $this, $params); + + return true; + } + + /** + * 更新数据库记录 + * @param $table + * @param array $data + * @param string $where + * @param callable $callback + * @return bool + * @throws RecordException + */ + public function update($table,array $data,$where = '',callable $callback = null){ + + if(!is_string($table) && !$table instanceof Sql\TableIdentifier) + throw new RecordException("table must is string or TableIdentifier"); + + $params = compact('data'); + $this->getEventManager()->trigger(PreDeclare::EVENT_UPDATE_PRE, $this, $params); + + /** @var \Zend\EventManager\ResponseCollection $results */ + $results = $this->getEventManager()->trigger(PreDeclare::EVENT_UPDATE_PROCESS_DATA, $this, $params); + if(!$results->isEmpty()) + $data = $results->bottom(); + + if($table instanceof Sql\TableIdentifier) + $table = $table->getSchema().".".$table->getTable(); + + if(is_string($where) || empty($where)){ + $dbh = new Dbh; + $dbh->update($table,$data,$where); + } + + elseif($where instanceof Sql\Where) + { + + } + + elseif(is_array($where)) + { + + } + + else + throw new RecordException("Condition must is one of string,Zend\\Db\\Sql\\Where object,array"); + + if(!empty($callback) && is_callable($callback)) + call_user_func($callback,$data); $params = compact('data','id'); $this->getEventManager()->trigger(PreDeclare::EVENT_INSERT_POST, $this, $params);