Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
PHP-MySQLi-Database-Class
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
PHP-MySQLi-Database-Class
Commits
35a43d03
Commit
35a43d03
authored
May 16, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
First real api
parent
79a83d07
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
207 additions
and
58 deletions
+207
-58
dbObject.php
dbObject.php
+133
-51
accData.php
dbObject/models/accData.php
+6
-1
department.php
dbObject/models/department.php
+33
-0
test.php
dbObject/test.php
+35
-6
No files found.
dbObject.php
View file @
35a43d03
<?php
abstract
class
dbObject
{
public
$db
;
public
function
__construct
()
{
private
$db
;
public
$data
;
public
$isNew
=
true
;
public
$returnType
=
'Object'
;
public
function
__construct
(
$data
=
null
)
{
$this
->
db
=
MysqliDb
::
getInstance
();
if
(
$data
)
$this
->
data
=
$data
;
}
p
rivate
function
cleanup
(
$data
)
{
if
(
method_exists
(
$this
,
"preLoad"
))
$this
->
preLoad
(
$data
);
p
ublic
function
__set
(
$name
,
$value
)
{
$this
->
data
[
$name
]
=
$value
;
}
if
(
count
(
$data
)
==
0
)
return
Array
();
public
function
__get
(
$name
)
{
if
(
isset
(
$this
->
data
[
$name
]))
return
$this
->
data
[
$name
];
foreach
(
$this
->
dbFields
as
$key
=>
$prop
)
{
$value
=
$data
[
$key
];
if
(
!
is_array
(
$value
))
{
$sqlData
[
$key
]
=
$value
;
continue
;
}
if
(
property_exists
(
$this
->
db
,
$name
))
return
$this
->
db
->
$name
;
}
if
(
$prop
[
'json'
])
$sqlData
[
$key
]
=
json_encode
(
$value
);
else
if
(
$prop
[
'array'
])
$sqlData
[
$key
]
=
implode
(
"|"
,
$value
);
else
$sqlData
[
$key
]
=
$value
;
}
public
function
__isset
(
$name
)
{
if
(
$this
->
data
[
$name
])
return
isset
(
$this
->
data
[
$name
]);
return
$sqlData
;
if
(
property_exists
(
$this
->
db
,
$name
))
return
isset
(
$this
->
db
->
$name
);
}
public
function
insert
(
&
$data
)
{
if
(
empty
(
$this
->
dbFields
))
return
false
;
public
function
__unset
(
$name
)
{
unset
(
$this
->
data
[
$name
]);
}
$data
=
$this
->
cleanup
(
$data
);
$
id
=
$this
->
db
->
insert
(
$this
->
dbTable
,
$data
);
$
data
[
$this
->
$primaryKey
]
=
$id
;
return
$
id
;
public
static
function
ObjectBuilder
()
{
$
obj
=
self
::
objectCopy
(
);
$
obj
->
returnType
=
'Object'
;
return
$
obj
;
}
public
function
remove
(
$data
)
{
$this
->
db
->
where
(
$this
->
primaryKey
,
$data
[
'id'
]);
return
$this
->
db
->
delete
(
$this
->
dbTable
);
public
static
function
ArrayBuilder
()
{
$obj
=
self
::
objectCopy
();
$obj
->
returnType
=
'Array'
;
return
$obj
;
}
public
function
update
(
$data
)
{
public
function
insert
()
{
$sqlData
=
$this
->
prepareData
();
$id
=
$this
->
db
->
insert
(
$this
->
dbTable
,
$sqlData
);
if
(
!
empty
(
$this
->
primaryKey
))
$this
->
data
[
$this
->
primaryKey
]
=
$id
;
$this
->
isNew
=
false
;
return
$id
;
}
public
function
update
(
$data
=
null
)
{
if
(
empty
(
$this
->
dbFields
))
return
false
;
$data
=
$this
->
cleanup
(
$data
);
$this
->
db
->
where
(
$this
->
primaryKey
,
$data
[
$this
->
primaryKey
]);
return
$this
->
db
->
update
(
$this
->
dbTable
,
$data
);
if
(
empty
(
$this
->
data
[
$this
->
primaryKey
]))
return
false
;
if
(
$data
)
{
foreach
(
$data
as
$k
=>
$v
)
$this
->
$k
=
$v
;
}
$sqlData
=
$this
->
prepareData
();
$this
->
db
->
where
(
$this
->
primaryKey
,
$this
->
data
[
$this
->
primaryKey
]);
return
$this
->
db
->
update
(
$this
->
dbTable
,
$sqlData
);
}
public
function
getOne
(
$id
,
$fields
=
null
)
{
public
function
save
()
{
if
(
$this
->
isNew
)
return
$this
->
insert
();
return
$this
->
update
();
}
public
function
remove
()
{
$this
->
db
->
where
(
$this
->
primaryKey
,
$this
->
data
[
$this
->
primaryKey
]);
return
$this
->
db
->
delete
(
$this
->
dbTable
);
}
public
function
byId
(
$id
,
$fields
=
null
)
{
$this
->
db
->
where
(
$this
->
primaryKey
,
$id
);
return
$this
->
getOne
(
$fields
);
}
public
function
getOne
(
$fields
=
null
)
{
$results
=
$this
->
db
->
getOne
(
$this
->
dbTable
,
$fields
);
if
(
isset
(
$this
->
jsonFields
)
&&
is_array
(
$this
->
jsonFields
))
{
foreach
(
$this
->
jsonFields
as
$key
)
...
...
@@ -66,12 +102,18 @@ abstract class dbObject {
foreach
(
$this
->
arrayFields
as
$key
)
$results
[
$key
]
=
explode
(
"|"
,
$results
[
$key
]);
}
return
$results
;
if
(
$this
->
returnType
==
'Array'
)
return
$results
;
$item
=
$this
->
objectCopy
(
$results
);
$item
->
isNew
=
false
;
return
$item
;
}
public
function
get
(
$limit
=
null
,
$fields
=
null
)
{
$
db
=
MysqliDb
::
getInstance
();
$results
=
$db
->
get
(
$this
->
dbTable
);
$
objects
=
Array
();
$results
=
$
this
->
db
->
get
(
$this
->
dbTable
);
foreach
(
$results
as
&
$r
)
{
if
(
isset
(
$this
->
jsonFields
)
&&
is_array
(
$this
->
jsonFields
))
{
foreach
(
$this
->
jsonFields
as
$key
)
...
...
@@ -81,28 +123,68 @@ abstract class dbObject {
foreach
(
$this
->
arrayFields
as
$key
)
$r
[
$key
]
=
explode
(
"|"
,
$r
[
$key
]);
}
if
(
$this
->
returnType
==
'Object'
)
{
$item
=
$this
->
objectCopy
(
$r
);
$item
->
isNew
=
false
;
$objects
[]
=
$item
;
}
}
if
(
$this
->
returnType
==
'Object'
)
return
$objects
;
return
$results
;
}
public
function
where
(
$whereProp
,
$whereValue
=
null
,
$operator
=
null
)
{
$
this
->
db
->
where
(
$whereProp
,
$whereValue
,
$operator
);
return
$
this
;
public
function
count
(
)
{
$
res
=
$this
->
db
->
getValue
(
$this
->
dbTable
,
"count(*)"
);
return
$
res
[
'cnt'
]
;
}
public
function
orWhere
(
$whereProp
,
$whereValue
=
null
,
$operator
=
null
)
{
$this
->
db
->
orWhere
(
$whereProp
,
$whereValue
,
$operator
);
public
function
__call
(
$method
,
$arg
)
{
call_user_func_array
(
array
(
$this
->
db
,
$method
),
$arg
);
return
$this
;
}
public
function
orderBy
(
$orderByField
,
$orderbyDirection
=
"DESC"
,
$customFields
=
null
)
{
$this
->
db
->
orderBy
(
$orderByField
,
$orderbyDirection
,
$customFields
);
return
$this
;
public
function
toJson
()
{
return
json_encode
(
$this
->
data
);
}
public
function
count
()
{
$res
=
$this
->
db
->
getValue
(
$this
->
dbTable
,
"count(*)"
);
return
$res
[
'cnt'
];
public
function
__toString
()
{
return
$this
->
toJson
();
}
private
function
prepareData
()
{
$sqlData
=
Array
();
if
(
method_exists
(
$this
,
"preLoad"
))
$this
->
preLoad
(
$data
);
if
(
count
(
$this
->
data
)
==
0
)
return
Array
();
foreach
(
$this
->
data
as
$key
=>
$value
)
{
if
(
!
in_array
(
$key
,
array_keys
(
$this
->
dbFields
)))
continue
;
if
(
!
is_array
(
$value
))
{
$sqlData
[
$key
]
=
$value
;
continue
;
}
if
(
in_array
(
$key
,
$this
->
jsonFields
))
$sqlData
[
$key
]
=
json_encode
(
$value
);
else
$sqlData
[
$key
]
=
implode
(
"|"
,
$value
);
}
return
$sqlData
;
}
private
static
function
objectCopy
(
$data
=
null
)
{
$className
=
get_called_class
();
return
new
$className
(
$data
);
}
}
?>
dbObject/models/accData.php
View file @
35a43d03
...
...
@@ -5,8 +5,13 @@ class accData extends dbObject {
protected
$dbTable
=
"acc_data"
;
protected
$primaryKey
=
"id"
;
public
$calldate
;
public
$callid
;
public
$clientid
;
public
$queueid
;
public
function
last
()
{
$this
->
db
->
where
(
"id"
,
1300
,
'>'
);
$this
->
where
(
"id"
,
1300
,
'>'
);
return
$this
;
}
}
...
...
dbObject/models/department.php
0 → 100644
View file @
35a43d03
<?php
require_once
"../dbObject.php"
;
/**
* To make IDEs autocomplete happy
*
* @property string id
* @property string userid
* @property string name
* @property string authcode
* @property string iscallerid
*/
class
department
extends
dbObject
{
protected
$dbTable
=
"departments"
;
protected
$primaryKey
=
"id"
;
protected
$dbFields
=
Array
(
'userid'
=>
'int:required'
,
'name'
=>
'int:required'
,
'authcode'
=>
'int'
,
'iscallerid'
=>
'int'
,
'testvar'
=>
'int'
);
protected
$jsonFields
=
Array
(
'authcode'
);
public
function
last
()
{
$this
->
setTrace
(
true
);
$this
->
where
(
"id"
,
130
,
'>'
);
return
$this
;
}
}
?>
dbObject/test.php
View file @
35a43d03
<?
require_once
(
"../MysqliDb.php"
);
require_once
(
"models/
accData
.php"
);
require_once
(
"models/
department
.php"
);
$db
=
new
Mysqlidb
(
'localhost'
,
'root'
,
''
,
'akorbi'
);
$accData
=
new
accData
();
$d
=
$accData
->
getOne
(
1288
);
print_r
(
$d
);
$dept
=
new
department
();
$dept
->
userid
=
10
;
$dept
->
name
=
'avb test'
;
$dept
->
authcode
=
Array
(
'1234'
,
'123456'
);
$dept
->
iscallerid
=
1
;
$dept
->
insert
();
print_r
(
$accData
->
last
()
->
get
());
$dept2
=
new
department
([
'userid'
=>
'11'
,
'name'
=>
'avb2 test'
,
'authcode'
=>
'5678'
,
'iscallerid'
=>
0
,
]);
$dept2
->
save
();
$dept2
->
iscallerid
=
1
;
print_r
(
$dept2
->
data
);
$dept2
->
save
();
//
$a = new accData
;
//
echo $db->getLastQuery()
;
echo
"List
\n
"
;
$depts
=
department
::
ObjectBuilder
()
->
last
()
->
get
();
foreach
(
$depts
as
$d
)
{
// print_r ($d->data);
echo
$d
.
"
\n
"
;
}
echo
"getOne
\n
"
;
$dept3
=
department
::
ObjectBuilder
()
->
byId
(
"181"
);
$dept3
->
authcode
=
333
;
$dept3
->
save
();
print_r
(
$dept3
->
data
)
.
"
\n
"
;
echo
$dept3
->
count
;
print_r
(
$dept3
->
trace
);
echo
$dept3
->
qqq
;
?>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment