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
5350b8d8
Commit
5350b8d8
authored
May 25, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bugfixes and API change
parent
d2aa5131
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
220 additions
and
116 deletions
+220
-116
dbObject.php
dbObject.php
+55
-35
accData.php
dbObject/models/accData.php
+0
-20
product.php
dbObject/models/product.php
+6
-11
test.php
dbObject/test.php
+157
-48
tests.php
tests.php
+2
-2
No files found.
dbObject.php
View file @
5350b8d8
<?php
abstract
class
dbObject
{
class
dbObject
{
private
$db
;
public
$data
;
public
$isNew
=
true
;
public
static
$returnType
=
'Object'
;
public
$returnType
=
'Object'
;
private
$_with
=
Array
();
public
function
__construct
(
$data
=
null
)
{
$this
->
db
=
MysqliDb
::
getInstance
();
...
...
@@ -16,31 +17,33 @@ abstract class dbObject {
}
public
function
__get
(
$name
)
{
if
(
property_exists
(
$this
,
'relations'
))
{
if
(
isset
(
$this
->
relations
[
$name
]))
{
$relationType
=
strtolower
(
$this
->
relations
[
$name
][
0
]);
$modelName
=
$this
->
relations
[
$name
][
1
];
switch
(
$relationType
)
{
case
'hasone'
:
return
$modelName
::
ObjectBuilder
()
->
byId
(
$this
->
data
[
$name
]);
break
;
case
'hasmany'
:
$key
=
$this
->
relations
[
$name
][
2
];
return
$modelName
::
ObjectBuilder
()
->
where
(
$key
,
$this
->
data
[
$this
->
primaryKey
])
->
get
();
break
;
default
:
break
;
}
}
}
if
(
isset
(
$this
->
data
[
$name
]))
return
$this
->
data
[
$name
];
if
(
property_exists
(
$this
->
db
,
$name
))
return
$this
->
db
->
$name
;
if
(
!
property_exists
(
$this
,
'relations'
))
{
if
(
isset
(
$this
->
data
[
$name
]))
return
$this
->
data
[
$name
];
if
(
property_exists
(
$this
->
db
,
$name
))
return
$this
->
db
->
$name
;
}
if
(
!
isset
(
$this
->
relations
[
$name
]))
return
;
$relationType
=
strtolower
(
$this
->
relations
[
$name
][
0
]);
$modelName
=
$this
->
relations
[
$name
][
1
];
switch
(
$relationType
)
{
case
'hasone'
:
$obj
=
new
$modelName
;
$obj
->
returnType
=
$this
->
returnType
;
return
$obj
->
byId
(
$this
->
data
[
$name
]);
break
;
case
'hasmany'
:
$key
=
$this
->
relations
[
$name
][
2
];
$obj
=
new
$modelName
;
$obj
->
returnType
=
$this
->
returnType
;
return
$obj
->
where
(
$key
,
$this
->
data
[
$this
->
primaryKey
])
->
get
();
break
;
default
:
break
;
}
}
public
function
__isset
(
$name
)
{
...
...
@@ -112,9 +115,16 @@ abstract class dbObject {
return
$this
->
getOne
(
$fields
,
$id
);
}
private
function
processWith
(
&
$data
)
{
if
(
count
(
$this
->
_with
)
==
0
)
return
;
foreach
(
$this
->
_with
as
$w
)
$data
[
$w
]
=
$this
->
$w
;
}
private
function
getOne
(
$fields
=
null
,
$primaryKey
=
null
)
{
if
(
$primaryKey
)
$this
->
db
->
where
(
$this
->
primaryKey
,
$primaryKey
);
$this
->
db
->
where
(
$this
->
dbTable
.
'.'
.
$this
->
primaryKey
,
$primaryKey
);
$results
=
$this
->
db
->
getOne
(
$this
->
dbTable
,
$fields
);
if
(
isset
(
$this
->
jsonFields
)
&&
is_array
(
$this
->
jsonFields
))
{
...
...
@@ -125,8 +135,10 @@ abstract class dbObject {
foreach
(
$this
->
arrayFields
as
$key
)
$results
[
$key
]
=
explode
(
"|"
,
$results
[
$key
]);
}
if
(
static
::
$returnType
==
'Array'
)
if
(
$this
->
returnType
==
'Array'
)
{
$this
->
processWith
(
$results
);
return
$results
;
}
$item
=
new
static
(
$results
);
$item
->
isNew
=
false
;
...
...
@@ -146,18 +158,25 @@ abstract class dbObject {
foreach
(
$this
->
arrayFields
as
$key
)
$r
[
$key
]
=
explode
(
"|"
,
$r
[
$key
]);
}
if
(
static
::
$
returnType
==
'Object'
)
{
if
(
$this
->
returnType
==
'Object'
)
{
$item
=
new
static
(
$r
);
$item
->
isNew
=
false
;
$objects
[]
=
$item
;
}
}
else
$this
->
processWith
(
$r
);
}
if
(
static
::
$
returnType
==
'Object'
)
if
(
$this
->
returnType
==
'Object'
)
return
$objects
;
return
$results
;
}
public
function
join
(
$objectName
,
$key
=
null
,
$joinType
=
'LEFT'
)
{
private
function
with
(
$objectName
)
{
$this
->
_with
[]
=
$objectName
;
return
$this
;
}
private
function
join
(
$objectName
,
$key
=
null
,
$joinType
=
'LEFT'
)
{
$joinObj
=
new
$objectName
;
if
(
!
$key
)
$key
=
$objectName
.
"id"
;
...
...
@@ -212,11 +231,12 @@ abstract class dbObject {
continue
;
}
if
(
in_array
(
$key
,
$this
->
jsonFields
))
if
(
i
sset
(
$this
->
jsonFields
)
&&
i
n_array
(
$key
,
$this
->
jsonFields
))
$sqlData
[
$key
]
=
json_encode
(
$value
);
else
else
if
(
isset
(
$this
->
arrayFields
)
&&
in_array
(
$key
,
$this
->
arrayFields
))
$sqlData
[
$key
]
=
implode
(
"|"
,
$value
);
else
$sqlData
[
$key
]
=
$value
;
}
return
$sqlData
;
}
...
...
dbObject/models/accData.php
deleted
100644 → 0
View file @
d2aa5131
<?php
require_once
"../dbObject.php"
;
class
accData
extends
dbObject
{
protected
$dbTable
=
"acc_data"
;
protected
$primaryKey
=
"id"
;
public
$calldate
;
public
$callid
;
public
$clientid
;
public
$queueid
;
public
function
last
()
{
$this
->
where
(
"id"
,
1300
,
'>'
);
return
$this
;
}
}
?>
dbObject/models/
departmen
t.php
→
dbObject/models/
produc
t.php
View file @
5350b8d8
...
...
@@ -10,23 +10,18 @@ require_once ("user.php");
* @property string authcode
* @property string iscallerid
*/
class
departmen
t
extends
dbObject
{
protected
$dbTable
=
"
departmen
ts"
;
class
produc
t
extends
dbObject
{
protected
$dbTable
=
"
produc
ts"
;
protected
$primaryKey
=
"id"
;
protected
$dbFields
=
Array
(
'userid'
=>
'int:required'
,
'name'
=>
'int:required'
,
'authcode'
=>
'int'
,
'userid'
=>
'int'
,
'iscallerid'
=>
'int'
,
'testvar'
=>
'int'
'userId'
=>
'int:required'
,
'customerId'
=>
'int:required'
,
'productName'
=>
'char:required'
);
protected
$relations
=
Array
(
'user
i
d'
=>
Array
(
"hasOne"
,
"user"
)
'user
I
d'
=>
Array
(
"hasOne"
,
"user"
)
);
protected
$jsonFields
=
Array
(
'authcode'
);
public
function
last
()
{
$this
->
where
(
"id"
,
130
,
'>'
);
return
$this
;
...
...
dbObject/test.php
View file @
5350b8d8
<?
require_once
(
"../MysqliDb.php"
);
require_once
(
"../dbObject.php"
);
require_once
(
"models/department.php"
);
$db
=
new
Mysqlidb
(
'localhost'
,
'root'
,
''
,
'akorbi'
);
$dept4
=
department
::
ArrayBuilder
()
->
join
(
'user'
)
->
get
(
2
);
echo
json_encode
(
$dept4
);
$dept
=
new
department
();
$dept
->
userid
=
10
;
$dept
->
name
=
'avb test'
;
$dept
->
authcode
=
Array
(
'1234'
,
'123456'
);
$dept
->
iscallerid
=
1
;
$dept
->
insert
();
echo
"ID = "
.
$dept
->
id
.
"
\n
"
;
$dept2
=
new
department
([
'userid'
=>
'11'
,
'name'
=>
'john doe'
,
'authcode'
=>
'5678'
,
'iscallerid'
=>
0
,
]);
$dept2
->
save
();
$dept2
->
iscallerid
=
1
;
print_r
(
$dept2
->
data
);
$dept2
->
save
();
echo
"department is of class "
.
get_class
(
$dept2
)
.
"
\n
"
;
echo
"List
\n
"
;
$depts
=
department
::
get
();
require_once
(
"models/product.php"
);
$db
=
new
Mysqlidb
(
'localhost'
,
'root'
,
''
,
'testdb'
);
$tables
=
Array
(
'users'
=>
Array
(
'login'
=>
'char(10) not null'
,
'active'
=>
'bool default 0'
,
'customerId'
=>
'int(10) not null'
,
'firstName'
=>
'char(10) not null'
,
'lastName'
=>
'char(10)'
,
'password'
=>
'text not null'
,
'createdAt'
=>
'datetime'
,
'expires'
=>
'datetime'
,
'loginCount'
=>
'int(10) default 0'
),
'products'
=>
Array
(
'customerId'
=>
'int(10) not null'
,
'userId'
=>
'int(10) not null'
,
'productName'
=>
'char(50)'
)
);
$data
=
Array
(
'user'
=>
Array
(
Array
(
'login'
=>
'user1'
,
'customerId'
=>
10
,
'firstName'
=>
'John'
,
'lastName'
=>
'Doe'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword+salt"
)),
'createdAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
()
),
Array
(
'login'
=>
'user2'
,
'customerId'
=>
10
,
'firstName'
=>
'Mike'
,
'lastName'
=>
NULL
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'createdAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
(
2
)
),
Array
(
'login'
=>
'user3'
,
'active'
=>
true
,
'customerId'
=>
11
,
'firstName'
=>
'Pete'
,
'lastName'
=>
'D'
,
'password'
=>
$db
->
func
(
'SHA1(?)'
,
Array
(
"secretpassword2+salt"
)),
'createdAt'
=>
$db
->
now
(),
'expires'
=>
$db
->
now
(
'+1Y'
),
'loginCount'
=>
$db
->
inc
(
3
)
)
),
'product'
=>
Array
(
Array
(
'customerId'
=>
1
,
'userId'
=>
1
,
'productName'
=>
'product1'
,
),
Array
(
'customerId'
=>
1
,
'userId'
=>
1
,
'productName'
=>
'product2'
,
),
Array
(
'customerId'
=>
1
,
'userId'
=>
1
,
'productName'
=>
'product3'
,
),
Array
(
'customerId'
=>
1
,
'userId'
=>
2
,
'productName'
=>
'product4'
,
),
Array
(
'customerId'
=>
1
,
'userId'
=>
2
,
'productName'
=>
'product5'
,
),
)
);
function
createTable
(
$name
,
$data
)
{
global
$db
;
//$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL";
$q
=
"CREATE TABLE
$name
(id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT"
;
foreach
(
$data
as
$k
=>
$v
)
{
$q
.=
",
$k
$v
"
;
}
$q
.=
")"
;
$db
->
rawQuery
(
$q
);
}
// rawQuery test
foreach
(
$tables
as
$name
=>
$fields
)
{
$db
->
rawQuery
(
"DROP TABLE "
.
$name
);
createTable
(
$name
,
$fields
);
}
foreach
(
$data
as
$name
=>
$datas
)
{
foreach
(
$data
[
$name
]
as
$userData
)
{
$obj
=
new
$name
(
$userData
);
$id
=
$obj
->
save
();
echo
"
$name
$id
created
\n
"
;
}
}
$products
=
product
::
ArrayBuilder
()
->
get
(
2
);
foreach
(
$products
as
$p
)
{
if
(
!
is_array
(
$p
))
{
echo
"ArrayBuilder do not return an array
\n
"
;
exit
;
}
}
$products
=
product
::
ArrayBuilder
()
->
get
(
2
);
$products
=
product
::
ArrayBuilder
()
->
with
(
'userId'
)
->
get
(
2
);
print_r
(
$products
);
$depts
=
product
::
join
(
'user'
)
->
orderBy
(
'products.id'
,
'desc'
)
->
get
(
5
);
foreach
(
$depts
as
$d
)
{
// print_r ($d->data);
echo
$d
.
"
\n
"
;
echo
"department is of class "
.
get_class
(
$d
)
.
"
\n
"
;
if
(
!
is_object
(
$d
))
{
echo
"Return should be an object
\n
"
;
exit
;
}
}
echo
"getOne
\n
"
;
$dept3
=
department
::
byId
(
$dept
->
id
);
echo
'cnt '
.
$dept3
->
count
.
"
\n
"
;
$dept3
->
authcode
=
443
;
$dept3
->
save
();
print_r
(
$dept3
->
data
)
.
"
\n
"
;
echo
"department is of class "
.
get_class
(
$dept3
)
.
"
\n
"
;
$dept
=
product
::
join
(
'user'
)
->
byId
(
5
);
if
(
count
(
$dept
->
data
)
!=
12
)
{
echo
"wrong props count "
.
count
(
$dept
->
data
)
.
"
\n
"
;
exit
;
}
echo
"hasOne
\n
"
;
echo
json_encode
(
$dept3
->
userid
->
data
);
echo
"user is of class "
.
get_class
(
$dept3
->
userid
)
.
"
\n
"
;
// hasOne
$products
=
product
::
get
();
$cnt
=
0
;
foreach
(
$products
as
$p
)
{
if
(
get_class
(
$d
)
!=
'product'
)
{
echo
"wrong class returned
\n
"
;
exit
;
}
echo
"
\n
hasMany
\n
"
;
foreach
(
$dept3
->
userid
->
departments
as
$d
)
{
echo
$d
;
echo
"department is of class "
.
get_class
(
$d
)
.
"
\n
"
;
if
(
!
(
$p
->
userId
instanceof
user
))
{
echo
"wrong return class of hasOne result
\n
"
;
exit
;
}
$cnt
++
;
}
$user
=
user
::
byId
(
41
);
echo
"user is of class "
.
get_class
(
$user
)
.
"
\n
"
;
if
((
$cnt
!=
$db
->
count
)
&&
(
$cnt
!=
5
))
{
echo
"wrong count after get
\n
"
;
exit
;
}
// hasMany
$user
=
user
::
where
(
'id'
,
1
)
->
getOne
();
if
(
!
is_array
(
$user
->
products
)
||
(
count
(
$user
->
products
)
!=
3
))
{
echo
"wrong count in hasMany
\n
"
;
exit
;
}
foreach
(
$user
->
products
as
$p
)
{
if
(
!
(
$p
instanceof
product
))
{
echo
"wrong return class of hasMany result
\n
"
;
exit
;
}
}
?>
tests.php
View file @
5350b8d8
...
...
@@ -40,7 +40,7 @@ $tables = Array (
)
);
$data
=
Array
(
'user
s
'
=>
Array
(
'user'
=>
Array
(
Array
(
'login'
=>
'user1'
,
'customerId'
=>
10
,
'firstName'
=>
'John'
,
...
...
@@ -70,7 +70,7 @@ $data = Array (
'loginCount'
=>
$db
->
inc
(
3
)
)
),
'product
s
'
=>
Array
(
'product'
=>
Array
(
Array
(
'customerId'
=>
1
,
'userId'
=>
1
,
'productName'
=>
'product1'
,
...
...
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