Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
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
PSDI Army
yii2
Commits
0cbb05d7
Commit
0cbb05d7
authored
Jul 11, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Query::indexBy
parent
227a831b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
42 additions
and
1 deletion
+42
-1
Query.php
framework/yii/db/Query.php
+42
-1
No files found.
framework/yii/db/Query.php
View file @
0cbb05d7
...
...
@@ -130,6 +130,13 @@ class Query extends Component
* For example, `array(':name' => 'Dan', ':age' => 31)`.
*/
public
$params
;
/**
* @var string|callable $column the name of the column by which the query results should be indexed by.
* This can also be a callable (e.g. anonymous function) that returns the index value based on the given
* row data. For more details, see [[indexBy()]]. This property is only used by [[all()]].
*/
public
$indexBy
;
/**
* Creates a DB command that can be used to execute this query.
...
...
@@ -147,6 +154,27 @@ class Query extends Component
}
/**
* Sets the [[indexBy]] property.
* @param string|callable $column the name of the column by which the query results should be indexed by.
* This can also be a callable (e.g. anonymous function) that returns the index value based on the given
* row data. The signature of the callable should be:
*
* ~~~
* function ($row)
* {
* // return the index value corresponding to $row
* }
* ~~~
*
* @return Query the query object itself
*/
public
function
indexBy
(
$column
)
{
$this
->
indexBy
=
$column
;
return
$this
;
}
/**
* Executes the query and returns all results as an array.
* @param Connection $db the database connection used to generate the SQL statement.
* If this parameter is not given, the `db` application component will be used.
...
...
@@ -154,7 +182,20 @@ class Query extends Component
*/
public
function
all
(
$db
=
null
)
{
return
$this
->
createCommand
(
$db
)
->
queryAll
();
$rows
=
$this
->
createCommand
(
$db
)
->
queryAll
();
if
(
$this
->
indexBy
===
null
)
{
return
$rows
;
}
$result
=
array
();
foreach
(
$rows
as
$row
)
{
if
(
is_string
(
$this
->
indexBy
))
{
$key
=
$row
[
$this
->
indexBy
];
}
else
{
$key
=
call_user_func
(
$this
->
indexBy
,
$row
);
}
$result
[
$key
]
=
$row
;
}
return
$result
;
}
/**
...
...
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