148 lines
4.2 KiB
PHP
148 lines
4.2 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: liujin834
|
|
* Date: 15-2-16
|
|
* Time: 上午11:31
|
|
*/
|
|
|
|
namespace Westdc\Db\Record;
|
|
|
|
use Zend\Db\Sql;
|
|
use Westdc\EventModel\AbstractEventManager;
|
|
use Westdc\Db\Db;
|
|
use Westdc\Db\Dbh;
|
|
|
|
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 Sql\TableIdentifier)
|
|
throw new RecordException("table must is string or TableIdentifier");
|
|
|
|
$params = compact('data');
|
|
|
|
$this->getEventManager()->trigger(PreDeclare::EVENT_INSERT_PRE, $this, $params);
|
|
|
|
/** @var \Zend\EventManager\ResponseCollection $results */
|
|
$results = $this->getEventManager()->trigger(PreDeclare::EVENT_INSERT_PROCESS_DATA, $this, $params);
|
|
if(!$results->isEmpty())
|
|
$data = $results->bottom();
|
|
|
|
try{
|
|
$dbh = new Dbh;
|
|
$id = $dbh->insert($table,$data,true);
|
|
}catch(\Exception $e){
|
|
throw new RecordException($e->getMessage());
|
|
}
|
|
|
|
if(!empty($callback) && is_callable($callback))
|
|
call_user_func($callback,$id,$data);
|
|
|
|
$params = compact('data','id');
|
|
$this->getEventManager()->trigger(PreDeclare::EVENT_INSERT_POST, $this, $params);
|
|
|
|
return true;
|
|
}//insert
|
|
|
|
/**
|
|
* 更新数据库记录
|
|
* @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;
|
|
$id = $dbh->update($table,$data,$where,true);
|
|
}
|
|
|
|
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_UPDATE_POST, $this, $params);
|
|
|
|
return true;
|
|
}//update
|
|
|
|
/**
|
|
* @param $table
|
|
* @param $where
|
|
* @param callable $callback
|
|
* @return bool
|
|
* @throws RecordException
|
|
*/
|
|
public function delete($table,$where,callable $callback = NULL){
|
|
|
|
if(!is_string($table) && !$table instanceof Sql\TableIdentifier)
|
|
throw new RecordException("table must is string or TableIdentifier");
|
|
|
|
if($table instanceof Sql\TableIdentifier)
|
|
$table = $table->getSchema().".".$table->getTable();
|
|
|
|
$params = compact('where','table');
|
|
|
|
$this->getEventManager()->trigger(PreDeclare::EVENT_DELETE_PRE, $this, $params);
|
|
|
|
$dbAdapter = Db::getInstance();
|
|
|
|
$sql = new Sql\Sql($dbAdapter);
|
|
$delete = $sql->delete($table);
|
|
|
|
$delete->where($where);
|
|
|
|
$queryString = $sql->getSqlStringForSqlObject($delete);
|
|
$status = $dbAdapter->query($queryString);
|
|
$result = $status->execute();
|
|
|
|
if($result->getAffectedRows() > 0){
|
|
if(!empty($callback) && is_callable($callback)){
|
|
call_user_func($callback,$result->getAffectedRows());
|
|
}
|
|
return true;
|
|
}else{
|
|
return false;
|
|
}
|
|
}//delete
|
|
|
|
} |