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
252724ec
Commit
252724ec
authored
Jun 22, 2015
by
Alexander Butenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Partical fix for orderBy with setPrefix() and table specification bug
parent
07078e0a
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
6 deletions
+26
-6
MysqliDb.php
MysqliDb.php
+6
-0
readme.md
readme.md
+15
-1
dbObjectTests.php
tests/dbObjectTests.php
+5
-5
No files found.
MysqliDb.php
View file @
252724ec
...
@@ -553,6 +553,12 @@ class MysqliDb
...
@@ -553,6 +553,12 @@ class MysqliDb
$orderbyDirection
=
strtoupper
(
trim
(
$orderbyDirection
));
$orderbyDirection
=
strtoupper
(
trim
(
$orderbyDirection
));
$orderByField
=
preg_replace
(
"/[^-a-z0-9\.\(\),_`]+/i"
,
''
,
$orderByField
);
$orderByField
=
preg_replace
(
"/[^-a-z0-9\.\(\),_`]+/i"
,
''
,
$orderByField
);
// Add table prefix to orderByField if needed.
//FIXME: We are adding prefix only if table is enclosed into `` to distinguish aliases
// from table names
$orderByField
=
preg_replace
(
'/(\`)([`a-zA-Z0-9_]*\.)/'
,
'\1'
.
self
::
$prefix
.
'\2'
,
$orderByField
);
if
(
empty
(
$orderbyDirection
)
||
!
in_array
(
$orderbyDirection
,
$allowedDirection
))
if
(
empty
(
$orderbyDirection
)
||
!
in_array
(
$orderbyDirection
,
$allowedDirection
))
die
(
'Wrong order direction: '
.
$orderbyDirection
);
die
(
'Wrong order direction: '
.
$orderbyDirection
);
...
...
readme.md
View file @
252724ec
...
@@ -333,13 +333,27 @@ $results = $db->get('users');
...
@@ -333,13 +333,27 @@ $results = $db->get('users');
// Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND ();
// Gives: SELECT * FROM users ORDER BY id ASC,login DESC, RAND ();
```
```
o
rder by values example:
O
rder by values example:
```
php
```
php
$db
->
orderBy
(
'userGroup'
,
'ASC'
,
array
(
'superuser'
,
'admin'
,
'users'
));
$db
->
orderBy
(
'userGroup'
,
'ASC'
,
array
(
'superuser'
,
'admin'
,
'users'
));
$db
->
get
(
'users'
);
$db
->
get
(
'users'
);
// Gives: SELECT * FROM users ORDER BY FIELD (userGroup, 'superuser', 'admin', 'users') ASC;
// Gives: SELECT * FROM users ORDER BY FIELD (userGroup, 'superuser', 'admin', 'users') ASC;
```
```
If you are using setPrefix () functionality and need to use table names in orderBy() method make sure that table names are escaped with
``.
```php
$db->setPrefix ("t_");
$db->orderBy ("users.id","asc");
$results = $db->get ('users');
// WRONG: That will give: SELECT * FROM t_users ORDER BY users.id ASC;
$db->setPrefix ("t_");
$db->orderBy ("`
users
`.id", "asc");
$results = $db->get ('users');
// CORRECT: That will give: SELECT * FROM t_users ORDER BY t_users.id ASC;
```
### Grouping method
### Grouping method
```php
```php
$db->groupBy ("name");
$db->groupBy ("name");
...
...
tests/dbObjectTests.php
View file @
252724ec
...
@@ -136,7 +136,7 @@ if (!is_array ($products[0]['userId'])) {
...
@@ -136,7 +136,7 @@ if (!is_array ($products[0]['userId'])) {
exit
;
exit
;
}
}
$depts
=
product
::
join
(
'user'
)
->
orderBy
(
'
t_products
.id'
,
'desc'
)
->
get
(
5
);
$depts
=
product
::
join
(
'user'
)
->
orderBy
(
'
`products`
.id'
,
'desc'
)
->
get
(
5
);
foreach
(
$depts
as
$d
)
{
foreach
(
$depts
as
$d
)
{
if
(
!
is_object
(
$d
))
{
if
(
!
is_object
(
$d
))
{
echo
"Return should be an object
\n
"
;
echo
"Return should be an object
\n
"
;
...
@@ -246,10 +246,10 @@ if (!user::byId(1) instanceof user)
...
@@ -246,10 +246,10 @@ if (!user::byId(1) instanceof user)
if
(
!
is_array
(
user
::
ArrayBuilder
()
->
byId
(
1
)))
if
(
!
is_array
(
user
::
ArrayBuilder
()
->
byId
(
1
)))
echo
"wrong return type2"
;
echo
"wrong return type2"
;
if
(
!
is_array
(
product
::
join
(
'user'
)
->
orderBy
(
'
t_products
.id'
,
'desc'
)
->
get
(
2
)))
if
(
!
is_array
(
product
::
join
(
'user'
)
->
orderBy
(
'
`products`
.id'
,
'desc'
)
->
get
(
2
)))
echo
"wrong return type2"
;
echo
"wrong return type2"
;
if
(
!
is_array
(
product
::
orderBy
(
'
t_products
.id'
,
'desc'
)
->
join
(
'user'
)
->
get
(
2
)))
if
(
!
is_array
(
product
::
orderBy
(
'
`products`
.id'
,
'desc'
)
->
join
(
'user'
)
->
get
(
2
)))
echo
"wrong return type2"
;
echo
"wrong return type2"
;
$u
=
new
user
;
$u
=
new
user
;
...
@@ -257,10 +257,10 @@ if (!$u->byId(1) instanceof user)
...
@@ -257,10 +257,10 @@ if (!$u->byId(1) instanceof user)
echo
"wrong return type2"
;
echo
"wrong return type2"
;
$p
=
new
product
;
$p
=
new
product
;
if
(
!
is_array
(
$p
->
join
(
'user'
)
->
orderBy
(
'
t_products
.id'
,
'desc'
)
->
get
(
2
)))
if
(
!
is_array
(
$p
->
join
(
'user'
)
->
orderBy
(
'
`products`
.id'
,
'desc'
)
->
get
(
2
)))
echo
"wrong return type2"
;
echo
"wrong return type2"
;
if
(
!
is_array
(
$p
->
orderBy
(
'
t_products
.id'
,
'desc'
)
->
join
(
'user'
)
->
get
(
2
)))
if
(
!
is_array
(
$p
->
orderBy
(
'
`products`
.id'
,
'desc'
)
->
join
(
'user'
)
->
get
(
2
)))
echo
"wrong return type2"
;
echo
"wrong return type2"
;
echo
"All done"
;
echo
"All done"
;
...
...
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