<?php
class
My_model
extends
CI_Model {
public
$errors
=
array
();
const
dataBase =
'qndnew'
;
public
function
__construct()
{
parent::__construct();
}
public
function
pageData(
$model
,
$table
,
$param
=
array
(),
$select_fields
=
''
,
$page
=
'1'
,
$limit
=
'15'
,
$order
=
array
(),
$isReturnCount
= true){
if
(
empty
(
$model
) ||
empty
(
$table
)){
return
false;
}
$this
-> load -> model(
$model
);
$table
=
$this
->db->dbprefix.
$table
;
if
(!
empty
(
$select_fields
)){
$this
->db->select(
$select_fields
)->from(
$table
);
}
elseif
(isset(
$this
->
$model
-> selectFields)){
$this
->db->select(
$this
->
$model
-> selectFields)->from(
$table
);
}
else
{
$this
->db->select(
'*'
)->from(
$table
);
}
if
(
is_array
(
$param
) &&
count
(
$param
) > 0){
$this
-> parseParam(
$param
);
}
if
(
$isReturnCount
){
$rs
[
'count'
] =
$this
->db->count_all_results(
''
,false);
array_push
(
$this
-> errors,
$this
->db->last_query());
}
if
(isset(
$page
) && isset(
$param
[
'limit'
])){
$offset
=
$param
[
'page'
] <= 1 ? 0 : (
$param
[
'page'
]-1) *
$param
[
'limit'
];
$this
->db->limit(
$param
[
'limit'
],
$offset
);
}
if
(!
empty
(
$order
) &&
is_array
(
$order
))
{
foreach
(
$order
as
$key
=>
$val
)
{
$this
->db->order_by(
$key
,
$val
);
}
}
else
{
$primary
=
$this
->getPrimary();
if
(!
empty
(
$primary
))
{
$this
->db->order_by(
$primary
,
'DESC'
);
}
}
$query
=
$this
->db->get();
array_push
(
$this
-> errors,
$this
->db->last_query());
$rs
[
'list'
] =
$query
->result_array();
return
$rs
;
}
private
function
parseParam(
$param
){
if
(isset(
$param
[
'compare'
])){
foreach
(
$param
[
'compare'
]
as
$key
=>
$val
){
if
(!
empty
(
$val
))
$this
->db->where(
$key
,
$val
);
}
}
if
(isset(
$param
[
'like'
])){
foreach
(
$param
[
'like'
]
as
$key
=>
$val
){
if
(!
empty
(
$val
))
$this
->db->like(
$key
,
$val
);
}
}
if
(isset(
$param
[
'in'
])){
foreach
(
$param
[
'in'
]
as
$key
=>
$val
){
if
(!
empty
(
$val
))
$this
->db->where_in(
$key
,
$val
);
}
}
if
(isset(
$param
[
'customStr'
])){
if
(!
empty
(
$val
))
$this
->db->where(
$param
[
'customStr'
]);
}
}
public
function
add(
$table
=
''
,
$param
=
array
())
{
if
(
empty
(
$table
) || !
is_array
(
$param
) ||
empty
(
$param
)){
return
FALSE;
}
$this
->db->insert(
$table
,
$param
);
array_push
(
$this
-> errors,
$this
->db->last_query());
return
$this
->db->insert_id();
}
public
function
update(
$table
=
''
,
$primary
=
''
,
$id
= 0,
$param
=
array
())
{
if
(
empty
(
$table
) ||
empty
(
$primary
) ||
empty
(
$param
) ||
empty
(
$id
))
{
return
FALSE;
}
$id
= (int)
$id
;
$this
->db->where(
$primary
,
$id
)
->limit(1)
->update(
$table
,
$param
);
array_push
(
$this
-> errors,
$this
->db->last_query());
return
$this
->db->affected_rows();
}
public
function
delete
(
$table
=
''
,
$primary
=
''
,
$id
=
array
()){
if
(
empty
(
$table
) ||
empty
(
$primary
) ||
empty
(
$id
)){
return
FALSE;
}
$this
->db->where_in(
$primary
,
$id
)
->
delete
(
$table
);
array_push
(
$this
-> errors,
$this
->db->last_query());
return
$this
->db->affected_rows();
}
public
function
getPrimary(
$table
=
''
,
$database
= self::dataBase)
{
if
(
empty
(
$database
) ||
empty
(
$table
))
{
return
FALSE;
}
$sql
= "SELECT k.column_name
FROM information_schema.table_constraints t
JOIN information_schema.key_column_usage k
USING (constraint_name,table_schema,table_name)
WHERE t.constraint_type=
'PRIMARY KEY'
AND t.table_schema=
'qndnew'
AND t.table_name=
'qnd_user'
";
$query
=
$this
->db->query(
$sql
)->result_array();
return
isset(
$query
[0][
'column_name'
]) ?
$query
[0][
'column_name'
] : false;
}
public
function
debugSql(){
if
(
count
(
$this
->errors) > 0){
foreach
(
$this
->errors
as
$val
){
echo
$val
.
'<br>'
;
}
}
}
}