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
29209841
Commit
29209841
authored
May 17, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Api changes and relations methods added
parent
c4e00629
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
46 deletions
+86
-46
dbObject.php
dbObject.php
+66
-36
department.php
dbObject/models/department.php
+6
-2
test.php
dbObject/test.php
+14
-8
No files found.
dbObject.php
View file @
29209841
...
...
@@ -3,8 +3,7 @@ abstract class dbObject {
private
$db
;
public
$data
;
public
$isNew
=
true
;
public
$returnType
=
'Object'
;
public
static
$returnType
=
'Object'
;
public
function
__construct
(
$data
=
null
)
{
$this
->
db
=
MysqliDb
::
getInstance
();
...
...
@@ -17,11 +16,31 @@ 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
::
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
;
}
public
function
__isset
(
$name
)
{
...
...
@@ -36,15 +55,14 @@ abstract class dbObject {
unset
(
$this
->
data
[
$name
]);
}
public
static
function
Object
Builder
()
{
$obj
=
self
::
objectCopy
()
;
$obj
->
returnType
=
'Object
'
;
public
static
function
Array
Builder
()
{
$obj
=
new
static
;
static
::
$returnType
=
'Array
'
;
return
$obj
;
}
public
static
function
ArrayBuilder
()
{
$obj
=
self
::
objectCopy
();
$obj
->
returnType
=
'Array'
;
public
static
function
ObjectBuilder
()
{
$obj
=
new
static
;
return
$obj
;
}
...
...
@@ -90,53 +108,66 @@ abstract class dbObject {
}
public
function
byId
(
$id
,
$fields
=
null
)
{
$this
->
db
->
where
(
$this
->
primaryKey
,
$id
);
return
$this
->
getOne
(
$fields
);
public
static
function
byId
(
$id
,
$fields
=
null
)
{
return
static
::
getOne
(
$fields
,
$id
);
}
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
)
public
static
function
getOne
(
$fields
=
null
,
$primaryKey
=
null
,
$obj
=
null
)
{
$obj
=
new
static
;
if
(
$primaryKey
)
$obj
->
db
->
where
(
$obj
->
primaryKey
,
$primaryKey
);
$results
=
$obj
->
db
->
getOne
(
$obj
->
dbTable
,
$fields
);
if
(
isset
(
$obj
->
jsonFields
)
&&
is_array
(
$obj
->
jsonFields
))
{
foreach
(
$obj
->
jsonFields
as
$key
)
$results
[
$key
]
=
json_decode
(
$results
[
$key
]);
}
if
(
isset
(
$
this
->
arrayFields
)
&&
is_array
(
$this
->
arrayFields
))
{
foreach
(
$
this
->
arrayFields
as
$key
)
if
(
isset
(
$
obj
->
arrayFields
)
&&
is_array
(
$obj
->
arrayFields
))
{
foreach
(
$
obj
->
arrayFields
as
$key
)
$results
[
$key
]
=
explode
(
"|"
,
$results
[
$key
]);
}
if
(
$this
->
returnType
==
'Array'
)
if
(
static
::
$
returnType
==
'Array'
)
return
$results
;
$item
=
$this
->
objectCopy
(
$results
);
$item
=
new
static
(
$results
);
$item
->
isNew
=
false
;
return
$item
;
}
public
function
get
(
$limit
=
null
,
$fields
=
null
)
{
public
static
function
get
(
$limit
=
null
,
$fields
=
null
)
{
$obj
=
new
static
;
$objects
=
Array
();
$results
=
$
this
->
db
->
get
(
$this
->
dbTable
);
$results
=
$
obj
->
db
->
get
(
$obj
->
dbTable
,
$limit
,
$fields
);
foreach
(
$results
as
&
$r
)
{
if
(
isset
(
$
this
->
jsonFields
)
&&
is_array
(
$this
->
jsonFields
))
{
foreach
(
$
this
->
jsonFields
as
$key
)
if
(
isset
(
$
obj
->
jsonFields
)
&&
is_array
(
$obj
->
jsonFields
))
{
foreach
(
$
obj
->
jsonFields
as
$key
)
$r
[
$key
]
=
json_decode
(
$r
[
$key
]);
}
if
(
isset
(
$
this
->
arrayFields
)
&&
is_array
(
$this
->
arrayFields
))
{
foreach
(
$
this
->
arrayFields
as
$key
)
if
(
isset
(
$
obj
->
arrayFields
)
&&
is_array
(
$obj
->
arrayFields
))
{
foreach
(
$
obj
->
arrayFields
as
$key
)
$r
[
$key
]
=
explode
(
"|"
,
$r
[
$key
]);
}
if
(
$this
->
returnType
==
'Object'
)
{
$item
=
$this
->
objectCopy
(
$r
);
if
(
static
::
$
returnType
==
'Object'
)
{
$item
=
new
static
(
$r
);
$item
->
isNew
=
false
;
$objects
[]
=
$item
;
}
}
if
(
$this
->
returnType
==
'Object'
)
if
(
static
::
$
returnType
==
'Object'
)
return
$objects
;
return
$results
;
}
public
function
join
(
$objectName
,
$key
=
null
,
$joinType
=
'LEFT'
)
{
$joinObj
=
new
$objectName
;
if
(
!
$key
)
$key
=
$objectName
.
"id"
;
$joinStr
=
"
{
$this
->
dbTable
}
.
{
$key
}
=
{
$joinObj
->
dbTable
}
.
{
$joinObj
->
primaryKey
}
"
;
$this
->
db
->
join
(
$joinObj
->
dbTable
,
$joinStr
,
$joinType
);
return
$this
;
}
public
function
count
()
{
$res
=
$this
->
db
->
getValue
(
$this
->
dbTable
,
"count(*)"
);
return
$res
[
'cnt'
];
...
...
@@ -148,6 +179,13 @@ abstract class dbObject {
return
$this
;
}
public
static
function
__callStatic
(
$method
,
$arg
)
{
$obj
=
new
static
;
call_user_func_array
(
array
(
$obj
,
$method
),
$arg
);
return
$obj
;
}
public
function
toJson
()
{
return
json_encode
(
$this
->
data
);
}
...
...
@@ -181,13 +219,5 @@ abstract class dbObject {
}
return
$sqlData
;
}
private
static
function
objectCopy
(
$data
=
null
)
{
$className
=
get_called_class
();
return
new
$className
(
$data
);
}
}
?>
dbObject/models/department.php
View file @
29209841
<?php
require_once
"../dbObject.php"
;
require_once
(
"user.php"
)
;
/**
* To make IDEs autocomplete happy
...
...
@@ -17,13 +17,17 @@ class department extends dbObject {
'userid'
=>
'int:required'
,
'name'
=>
'int:required'
,
'authcode'
=>
'int'
,
'userid'
=>
'int'
,
'iscallerid'
=>
'int'
,
'testvar'
=>
'int'
);
protected
$relations
=
Array
(
'userid'
=>
Array
(
"hasOne"
,
"user"
)
);
protected
$jsonFields
=
Array
(
'authcode'
);
public
function
last
()
{
$this
->
setTrace
(
true
);
$this
->
where
(
"id"
,
130
,
'>'
);
return
$this
;
}
...
...
dbObject/test.php
View file @
29209841
<?
require_once
(
"../MysqliDb.php"
);
require_once
(
"../dbObject.php"
);
require_once
(
"models/department.php"
);
$db
=
new
Mysqlidb
(
'localhost'
,
'root'
,
''
,
'akorbi'
);
...
...
@@ -13,7 +14,7 @@ $dept->insert();
$dept2
=
new
department
([
'userid'
=>
'11'
,
'name'
=>
'
avb2 test
'
,
'name'
=>
'
john doe
'
,
'authcode'
=>
'5678'
,
'iscallerid'
=>
0
,
]);
...
...
@@ -22,25 +23,30 @@ $dept2->iscallerid=1;
print_r
(
$dept2
->
data
);
$dept2
->
save
();
//echo $db->getLastQuery();
echo
"List
\n
"
;
$depts
=
department
::
ObjectBuilder
()
->
last
()
->
get
();
$depts
=
department
::
get
();
foreach
(
$depts
as
$d
)
{
// print_r ($d->data);
echo
$d
.
"
\n
"
;
}
echo
"getOne
\n
"
;
$dept3
=
department
::
ObjectBuilder
()
->
byId
(
"181"
);
$dept3
=
department
::
byId
(
"181"
);
echo
'cnt '
.
$dept3
->
count
.
"
\n
"
;
$dept3
->
authcode
=
333
;
$dept3
->
save
();
print_r
(
$dept3
->
data
)
.
"
\n
"
;
echo
$dept3
->
count
;
print_r
(
$dept3
->
trace
);
echo
$dept3
->
qqq
;
echo
"hasOne
\n
"
;
echo
json_encode
(
$dept3
->
userid
->
data
);
echo
"
\n
hasMany
\n
"
;
foreach
(
$dept3
->
userid
->
departments
as
$d
)
{
echo
$d
;
}
$dept4
=
department
::
ArrayBuilder
()
->
join
(
'user'
)
->
get
(
2
);
echo
json_encode
(
$dept4
);
?>
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