采用json方法处理postgresql数组

This commit is contained in:
wlx 2015-01-09 09:44:43 +00:00
parent 34616beca3
commit b930363955
1 changed files with 24 additions and 46 deletions

View File

@ -25,9 +25,9 @@ class Doi extends Zend_Controller_Plugin_Abstract
}
if(empty($uid))
{
$sql = "SELECT * FROM ".$this->tbl_doi." d where ".$wheresql."ORDER BY d.ts_published desc,d.ts_submitted desc,d.ts_created DESC";
$sql = "SELECT *,array_to_json(organization_en) as orgen_json FROM ".$this->tbl_doi." d where ".$wheresql."ORDER BY d.ts_published desc,d.ts_submitted desc,d.ts_created DESC";
}else{
$sql = "SELECT d.* FROM ".$this->tbl_doi." d
$sql = "SELECT d.*,array_to_json(d.organization_en) as orgen_json FROM ".$this->tbl_doi." d
LEFT JOIN mdauthor a ON d.uuid=a.uuid
WHERE a.userid=".$uid." AND a.status>0 and ".$wheresql."
ORDER BY d.ts_published desc,d.ts_submitted desc,d.ts_created DESC
@ -39,12 +39,11 @@ class Doi extends Zend_Controller_Plugin_Abstract
}
function add($data){
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
$this->data_process_in($data);
//include_once("helper/view.php");
//view::Dump($data);
return $dbh->insert($this->tbl_doi,$data);
$sql="insert into ".$this->tbl_doi." (doi,uuid,url,title_en,publisher_en,author_en,organization_en) values('".
$data['doi']."','".$data['uuid']."','".$data['url']."','".$data['title_en']."','".$data['publisher_en']."','".$data['author_en'].
"',array[".$data['organization_en']."])";
return $this->db->exec($sql);
}
function update($data,$uuid,$uid=0){
@ -63,8 +62,6 @@ class Doi extends Zend_Controller_Plugin_Abstract
return $this->add($data);
}else {
$this->data_process_in($data);
//include_once("helper/view.php");
//view::Dump($data);
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
if(is_numeric($uuid))
@ -73,7 +70,10 @@ class Doi extends Zend_Controller_Plugin_Abstract
}else{
$condition = " uuid='$uuid' ";
}
$state = $dbh->update($this->tbl_doi,$data,$condition,true);
$sql="update $this->tbl_doi set doi='".$data['doi']."',uuid='".$data['uuid']."',
url='".$data['url']."',title_en='".$data['title_en']."',publisher_en='".$data['publisher_en']
."',author_en='".$data['author_en']."',organization_en=array[".$data['organization_en']."] where ".$condition;
$state=$this->db->exec($sql);
if( $state == true)
{
@ -100,8 +100,6 @@ class Doi extends Zend_Controller_Plugin_Abstract
return $this->add($data);
}else {
$this->data_process_in($data);
//include_once("helper/view.php");
//view::Dump($data);
include_once("helper/dbh.php");
$dbh = new dbh($this->db);
if(is_numeric($uuid))
@ -113,7 +111,10 @@ class Doi extends Zend_Controller_Plugin_Abstract
$data['publisher']='';
$data['title_en']=$data['title'];
$data['title']='';
$state = $dbh->update($this->tbl_doi,$data,$condition,true);
$sql="update $this->tbl_doi set doi='".$data['doi']."',uuid='".$data['uuid']."',
url='".$data['url']."',title_en='".$data['title_en']."',publisher_en='".$data['publisher_en']
."',author_en='".$data['author_en']."',organization_en=array[".$data['organization_en']."] where ".$condition;
$state=$this->db->exec($sql);
if( $state == true)
{
@ -131,15 +132,13 @@ class Doi extends Zend_Controller_Plugin_Abstract
$orgs_en = array();
foreach($data['info'] as $k=>$v)
{
$authors_en[(int)$v['order']] = str_replace(",","||||",$v['author_en']);
$orgs_en[(int)$v['order']] = str_replace(",","||||",$v['organization_en']);
$authors_en[(int)$v['order']] = $v['author_en'];
$orgs_en[(int)$v['order']] = $v['organization_en'];
}
ksort($authors_en);
ksort($orgs_en);
//$authors = "{".join(",",$authors)."}";
//$orgs = "{".join(",",$orgs)."}";
$authors_en = "{".join(",",$authors_en)."}";
$orgs_en = "{".join(",",$orgs_en)."}";
$orgs_en = "'".join("','",$orgs_en)."'";
$data['authors'] = '{}';
$data['organization'] = '{}';
$data['author_en'] = $authors_en;
@ -149,51 +148,30 @@ class Doi extends Zend_Controller_Plugin_Abstract
function data_process_out(&$data){
$authors_en = $this->array_split($data['author_en']);
$orgs_en = $this->array_split($data['organization_en']);
$orgs_en = json_decode($data['orgen_json']);
$info = array();
foreach($authors_en as $k=>$v)
{
$info[$k] = array(
'author_en'=>str_replace("||||",",",str_replace("\"","",$authors_en[$k])),
'organization_en'=>str_replace("||||",",",str_replace("\"","",$orgs_en[$k])),
'order'=> count($authors_en)+($k-1)
'author_en'=>str_replace("\"","",$authors_en[$k]),
'organization_en'=>str_replace("\"","",$orgs_en[$k]),
'order'=> count($authors)+$k
);
}
//include_once("helper/view.php");
//view::Dump($info);
return $info;
}
function array_split($a){
/*$a = str_replace("{",'',$a);
$a = str_replace("}",'',$a);
$a = explode(",",$a); */
//return explode(',',substr($a,1,-1));
$regx1 = '/^{(.*)}$/';
$regx2 = "/\"((\\\\\\\\|\\\\\"|[^\"])+)\"|[^,]+/";
$regx3 = '/^[^"].*$|^"(.*)"$/';
$match = null;
preg_match( $regx1,$a,$match);
$str = $match[1];
preg_match_all($regx2, $str,$match);
$items = $match[0];
$array = array();
$count = count($items);
for($index = 0; $index < $count;++$index)
{
preg_match($regx3, $items[$index],$match);
$array[$index]=end($match);
}
return $array;
return explode(',',substr($a,1,-1));
}
function view($id)
{
if(is_numeric($id))
{
$sql = "SELECT * FROM ".$this->tbl_doi." WHERE id=$id";
$sql = "SELECT *,array_to_json(organization_en) as orgen_json FROM ".$this->tbl_doi." WHERE id=$id";
}else{
$sql = "SELECT * FROM ".$this->tbl_doi." WHERE uuid='$id'";
$sql = "SELECT *,array_to_json(organization_en) as orgen_json FROM ".$this->tbl_doi." WHERE uuid='$id'";
}
$rs = $this->db->query($sql);
$row = $rs->fetch();