ThinkPHP+Oracle执行存储过程。
一、首先修改ThinkPHP3.0的Oracle驱动(ThinkPHP\Lib\Driver\Db\DbOracle.class.php),在DbOracle类中增加以下方法:
/**
+----------------------------------------------------------
* 执行存储过程
+----------------------------------------------------------
* @author hongping <hongping626@qq.com>
+----------------------------------------------------------
* @param string $pName 存储过程名称
* @param array $pValue 传入参数值
+----------------------------------------------------------
* @return array 返回执行结果
+----------------------------------------------------------
*/
public function execProcedure($pName,$pValue) {
$pValue = array_change_key_case($pValue, CASE_LOWER);//强制下标为小写
$this->initConnect(true);
if ( !$this->_linkID ) return false;
//更改事务模式
$this->mode = OCI_COMMIT_ON_SUCCESS;
//释放前次的查询结果
if ( $this->queryID ) $this->free();
N('db_write',1);
// 记录开始执行时间
G('queryStartTime');
$argSql = "SELECT ARGUMENT_NAME,IN_OUT FROM USER_ARGUMENTS WHERE OBJECT_NAME = '".strtoupper($pName)."' ORDER BY SEQUENCE";
$argRow = $this->query($argSql);
$ParStr = '';
$x = 0;
foreach($argRow as $key=>$value){
if($x==0){
$ParStr .= ":".$value['argument_name'];
}else{
$ParStr .= ",:".$value['argument_name'];
}
$x++;
}
$this->queryID = oci_parse($this->_linkID, 'BEGIN '.$pName.'('.$ParStr.');END;');
foreach($argRow as $key=>$value){
$parmName = strtolower($value['argument_name']);
$parmType = strtolower($value['in_out']);
if($parmType==='out'){
oci_bind_by_name($this->queryID, ":".$parmName, $OutData[$parmName],2048);
}else{
$OutData[$parmName] = $pValue[$parmName];
oci_bind_by_name($this->queryID, ":".$parmName, $OutData[$parmName],2048);
}
}
oci_execute($this->queryID);
$this->debug();
if (!$this->queryID) {
$this->error();
return $this->queryID;
}else {
return $OutData;
}
}
二、然后在ThinkPHP\Lib\Core\Model.class.php加入以下(1185行左右):
/**
+----------------------------------------------------------
* 执行oracle存儲過程
+----------------------------------------------------------
* @access public
+----------------------------------------------------------
* @param string $sql SQL指令
* @param array $parr 传入传出参数值
* @param boolean $parse 是否需要解析SQL
+----------------------------------------------------------
* @return false | integer
+----------------------------------------------------------
*/
public function execProcedure($sql,$parr,$parse=false) {
$sql = $this->parseSql($sql,$parse);
return $this->db->execProcedure($sql,$parr);
}
使用示例:
public function testPub(){
$data['name'] = $_POST['name']; //存储过程传入参数
$data['value'] = $_POST['value']; //存储过程传入参数
$pdName = "PUB_TEST_KT";/*存储过程名*/
$result =M() ->execProcedure($pubName,$pubParm);//执行
$this->success($result['res']);//返回结果