westdc-core/Westdc/Db/Record.php

44 lines
1.2 KiB
PHP
Raw Normal View History

<?php
/**
* Created by PhpStorm.
* User: liujin834
* Date: 15-2-16
* Time: 上午11:31
*/
namespace Westdc\Db;
use Zend\Db\Sql\TableIdentifier;
use Westdc\EventModel\AbstractEventManager;
class Record extends AbstractEventManager {
public function insert($table,$data,callable $callback = null){
if(!is_string($table) && !$table instanceof 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();
$dbh = new Dbh;
$id = $dbh->insert($table,$data,true);
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;
}
}