Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
Yeray Santana Hualde
syncEnrollments
Commits
3392971c
Commit
3392971c
authored
Jun 23, 2011
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added dynamic finders to fluent query builder and eloquent.
parent
3b63335c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
0 deletions
+74
-0
query.php
system/db/query.php
+14
-0
dynamic.php
system/db/query/dynamic.php
+60
-0
No files found.
system/db/query.php
View file @
3392971c
...
@@ -500,6 +500,20 @@ class Query {
...
@@ -500,6 +500,20 @@ class Query {
*/
*/
public
function
__call
(
$method
,
$parameters
)
public
function
__call
(
$method
,
$parameters
)
{
{
// ---------------------------------------------------------
// Dynamic methods allows the building of very expressive
// queries. All dynamic methods start with "where_".
//
// Ex: DB::table('users')->where_email($email)->first();
// ---------------------------------------------------------
if
(
strpos
(
$method
,
'where_'
)
===
0
)
{
return
Query\Dynamic
::
build
(
$method
,
$parameters
,
$this
);
}
// ---------------------------------------------------------
// Handle any of the aggregate functions.
// ---------------------------------------------------------
if
(
in_array
(
$method
,
array
(
'count'
,
'min'
,
'max'
,
'avg'
,
'sum'
)))
if
(
in_array
(
$method
,
array
(
'count'
,
'min'
,
'max'
,
'avg'
,
'sum'
)))
{
{
return
(
$method
==
'count'
)
?
$this
->
aggregate
(
Str
::
upper
(
$method
),
'*'
)
:
$this
->
aggregate
(
Str
::
upper
(
$method
),
$parameters
[
0
]);
return
(
$method
==
'count'
)
?
$this
->
aggregate
(
Str
::
upper
(
$method
),
'*'
)
:
$this
->
aggregate
(
Str
::
upper
(
$method
),
$parameters
[
0
]);
...
...
system/db/query/dynamic.php
0 → 100644
View file @
3392971c
<?php
namespace
System\DB\Query
;
use
System\Str
;
class
Dynamic
{
/**
* Add conditions to a query from a dynamic method call.
*
* @param string $method
* @param array $parameters
* @param Query $query
* @return Query
*/
public
static
function
build
(
$method
,
$parameters
,
$query
)
{
// ---------------------------------------------------------
// Strip the "where_" off of the method.
// ---------------------------------------------------------
$method
=
substr
(
$method
,
6
);
// ---------------------------------------------------------
// Split the column names from the connectors.
// ---------------------------------------------------------
$segments
=
preg_split
(
'/(_and_|_or_)/i'
,
$method
,
-
1
,
PREG_SPLIT_DELIM_CAPTURE
);
// ---------------------------------------------------------
// The connector variable will determine which connector
// will be used for the condition. We'll change it as we
// come across new connectors in the dynamic method string.
//
// The index variable helps us get the correct parameter
// value for the where condition. We increment it each time
// we add a condition.
// ---------------------------------------------------------
$connector
=
'AND'
;
$index
=
0
;
// ---------------------------------------------------------
// Iterate through each segment and add the conditions.
// ---------------------------------------------------------
foreach
(
$segments
as
$segment
)
{
if
(
$segment
!=
'_and_'
and
$segment
!=
'_or_'
)
{
$query
->
where
(
$segment
,
'='
,
$parameters
[
$index
],
$connector
);
$index
++
;
}
else
{
$connector
=
trim
(
Str
::
upper
(
$segment
),
'_'
);
}
}
return
$query
;
}
}
\ No newline at end of file
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