<!--?php
class
dao_dao2
extends
Zend_Db_Table {
protected
$cfg_
;
function
init() {
$web_config
=
$this
--->getCfg();
$this
->db2_config =
$web_config
->db2->config->toArray();
$this
->db = Zend_Db::factory(
$web_config
->db2->adapter,
$this
->db2_config);
Zend_Db_Table::setDefaultAdapter(
$this
->db);
}
public
function
returnDb(){
return
$this
->db;
}
public
function
getData(
$table
,
$where
= false,
$order
=
'id ASC'
,
$pagesize
= false,
$offset
= false,
$count
= false,
$from
= false,
$join
= false,
$group
= false) {
$select
=
$this
->db->select();
if
(
$where
&&
is_array
(
$where
)) {
foreach
(
$where
as
$key
=>
$val
) {
if
(
$val
[
'type'
]==1){
$select
->where(
$key
,
$this
->convert2gbk(
$val
[
'val'
]));
}
else
{
$select
->orwhere(
$key
,
$this
->convert2gbk(
$val
[
'val'
]));
}
}
}
if
(!
$from
)
$from
=
'*'
;
if
(
$pagesize
) {
$select
->limit(
$pagesize
,
$offset
);
}
if
(
is_array
(
$order
)) {
foreach
(
$order
as
$value
) {
$select
->order(
$value
);
}
}
else
{
$select
->order(
$order
);
}
$select
->from(
$table
,
$count
?
"COUNT("
.
$table
.
".id)"
:
$from
);
if
(
is_array
(
$group
)) {
foreach
(
$group
as
$key
=>
$val
) {
$select
->group(
$val
);
}
if
(
$count
) {
$result
=
$this
->db->fetchAll(
$select
);
return
$result
;
}
}
else
{
if
(
$count
) {
$result
=
$this
->db->fetchOne(
$select
);
return
$result
;
}
}
if
(
is_array
(
$join
)) {
foreach
(
$join
as
$key
=>
$val
) {
$select
->joinleft(
$key
,
$val
[0],
$val
[1]);
}
}
$result
=
$this
->db->fetchAll(
$select
);
foreach
(
$result
as
$key
=>
$value
) {
foreach
(
$value
as
$key2
=>
$value2
) {
$result
[
$key
][
$key2
] =
$this
->convert2utf8(
$value2
);
}
}
return
$result
;
}
function
SaveData(
$adata
,
$table
,
$insterid
= 0,
$aLog
= false) {
foreach
(
$adata
as
$key
=>
$value
) {
$adata
[
$key
] =
$this
->convert2gbk(
$value
);
}
if
(
$this
->db->insert(
$table
,
$adata
)) {
$insertedID
=
$this
->db->lastInsertId();
if
(
$insterid
) {
return
$insertedID
;
}
else
{
return
TRUE;
}
}
else
{
return
false;
}
}
function
DelData(
$table
,
$where
,
$aLog
= false) {
if
(
$this
->db->
delete
(
$table
,
$where
)) {
return
TRUE;
}
else
{
return
FALSE;
}
}
function
UpdateData(
$table
,
$adata
,
$cond
,
$aLog
= false) {
foreach
(
$adata
as
$key
=>
$value
) {
$adata
[
$key
] =
$this
->convert2gbk(
$value
);
}
if
(
$this
->db->update(
$table
,
$adata
,
$cond
)) {
return
TRUE;
}
else
{
return
false;
}
}
public
function
clearTable(
$table
) {
$result
=
$this
->db->query(
'TRUNCATE TABLE '
.
$table
);
}
public
function
executeSql(
$strSql
) {
$result
=
$this
->db->query(
$strSql
);
}
function
convert2utf8(
$string
)
{
$config
=
$this
->getCfg();
$pdoType
=
$config
->db->config->pdoType;
if
(
$pdoType
==
'dblib'
){
return
iconv(
"gbk"
,
"utf-8"
,
$string
);
}
elseif
(
$pdoType
==
'sqlsrv'
){
return
mb_convert_encoding(
$string
,
"UTF-8"
,
"UTF-8"
);
}
}
function
convert2gbk(
$string
)
{
$config
=
$this
->getCfg();
$pdoType
=
$config
->db->config->pdoType;
if
(
$pdoType
==
'dblib'
){
return
iconv(
"utf-8"
,
"gbk"
,
$string
);
}
elseif
(
$pdoType
==
'sqlsrv'
){
return
mb_convert_encoding(
$string
,
"UTF-8"
,
"UTF-8"
);
}
}
protected
function
&getCfg() {
if
(
$this
->cfg_ === null) {
$registry
= Zend_Registry::getInstance();
$this
->cfg_ =
$registry
->get(
'web_config'
);
}
return
$this
->cfg_;
}
}