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
338b6aa7
Commit
338b6aa7
authored
Mar 30, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored ActiveRecord::find() so that find(null) will also return an…
Refactored ActiveRecord::find() so that find(null) will also return an ActiveRecord instance instead of ActiveQuery.
parent
2c31305f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
63 deletions
+54
-63
ActiveRecord.php
extensions/elasticsearch/ActiveRecord.php
+7
-3
controller.php
extensions/gii/generators/crud/default/controller.php
+1
-4
ActiveRecordInterface.php
framework/db/ActiveRecordInterface.php
+37
-11
BaseActiveRecord.php
framework/db/BaseActiveRecord.php
+9
-45
No files found.
extensions/elasticsearch/ActiveRecord.php
View file @
338b6aa7
...
...
@@ -68,13 +68,17 @@ class ActiveRecord extends BaseActiveRecord
public
static
function
find
(
$q
=
null
)
{
$query
=
static
::
createQuery
();
$args
=
func_get_args
();
if
(
empty
(
$args
))
{
return
$query
;
}
$q
=
reset
(
$args
);
if
(
is_array
(
$q
))
{
return
$query
->
andWhere
(
$q
)
->
one
();
}
else
if
(
$q
!==
null
)
{
}
else
{
return
static
::
get
(
$q
);
}
return
$query
;
}
/**
...
...
extensions/gii/generators/crud/default/controller.php
View file @
338b6aa7
...
...
@@ -142,18 +142,15 @@ class <?= $controllerClass ?> extends <?= StringHelper::basename($generator->bas
<?php
if
(
count
(
$pks
)
===
1
)
{
$condition
=
'$id'
;
// find() would return Query when $id === null
$nullCheck
=
'$id !== null && '
;
}
else
{
$condition
=
[];
foreach
(
$pks
as
$pk
)
{
$condition
[]
=
"'
$pk
' =>
\$
$pk
"
;
}
$condition
=
'['
.
implode
(
', '
,
$condition
)
.
']'
;
$nullCheck
=
''
;
}
?>
if (
<?=
$nullCheck
?>
($model =
<?=
$modelClass
?>
::find(
<?=
$condition
?>
)) !== null) {
if (($model =
<?=
$modelClass
?>
::find(
<?=
$condition
?>
)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
...
...
framework/db/ActiveRecordInterface.php
View file @
338b6aa7
...
...
@@ -96,25 +96,51 @@ interface ActiveRecordInterface
/**
* Creates an [[ActiveQueryInterface|ActiveQuery]] instance for query purpose.
*
* This method is usually ment to be used like this:
* The returned [[ActiveQueryInterface|ActiveQuery]] instance can be further customized by calling
* methods defined in [[ActiveQueryInterface]] before `one()` or `all()` is called to return
* populated ActiveRecord instances. For example,
*
* ```php
* Customer::find(1); // find one customer by primary key
* Customer::find()->all(); // find all customers
* // find the customer whose ID is 1
* $customer = Customer::find()->where(['id' => 1])->one();
*
* // find all active customers and order them by their age:
* $customers = Customer::find()
* ->where(['status' => 1])
* ->orderBy('age')
* ->all();
* ```
*
*
@param mixed $q the query parameter. This can be one of the followings
:
*
This method can also take a parameter which can be
:
*
* - a scalar value (integer or string): query by a single primary key value and return the
* corresponding record.
* - an array of name-value pairs: query by a set of attribute values and return a single record
matching all of them.
*
- null (not specified): return a new [[ActiveQuery]] object for further query purpose
.
* corresponding record
(or null if not found)
.
* - an array of name-value pairs: query by a set of attribute values and return a single record
*
matching all of them (or null if not found)
.
*
* @return ActiveQueryInterface|static|null When `$q` is null, a new [[ActiveQuery]] instance
* is returned; when `$q` is a scalar or an array, an ActiveRecord object matching it will be
* returned (null will be returned if there is no matching).
* Note that in this case, the method will automatically call the `one()` method and return an
* [[ActiveRecordInterface|ActiveRecord]] instance. For example,
*
* ```php
* // find a single customer whose primary key value is 10
* $customer = Customer::find(10);
*
* // the above code is equivalent to:
* $customer = Customer::find()->where(['id' => 10])->one();
*
* // find a single customer whose age is 30 and whose status is 1
* $customer = Customer::find(['age' => 30, 'status' => 1]);
*
* // the above code is equivalent to:
* $customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
* ```
*
* @return ActiveQueryInterface|static|null When this method receives no parameter, a new [[ActiveQuery]] instance
* will be returned; Otherwise, the parameter will be treated as a primary key value or a set of column
* values, and an ActiveRecord object matching it will be returned (null will be returned if there is no matching).
* @see createQuery()
*/
public
static
function
find
(
$q
=
null
);
public
static
function
find
();
/**
* Creates an [[ActiveQueryInterface|ActiveQuery]] instance.
...
...
framework/db/BaseActiveRecord.php
View file @
338b6aa7
...
...
@@ -92,54 +92,20 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
private
$_related
=
[];
/**
* Creates an [[ActiveQuery]] instance for query purpose.
*
* The returned [[ActiveQuery]] instance can be further customized by calling
* methods defined in [[ActiveQuery]] before `one()`, `all()` or `value()` is
* called to return the populated active records:
*
* ~~~
* // find all customers
* $customers = Customer::find()->all();
*
* // find all active customers and order them by their age:
* $customers = Customer::find()
* ->where(['status' => 1])
* ->orderBy('age')
* ->all();
*
* // find a single customer whose primary key value is 10
* $customer = Customer::find(10);
*
* // the above is equivalent to:
* $customer = Customer::find()->where(['id' => 10])->one();
*
* // find a single customer whose age is 30 and whose status is 1
* $customer = Customer::find(['age' => 30, 'status' => 1]);
*
* // the above is equivalent to:
* $customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
* ~~~
*
* @param mixed $q the query parameter. This can be one of the followings:
*
* - a scalar value (integer or string): query by a single primary key value and return the
* corresponding record.
* - an array of name-value pairs: query by a set of column values and return a single record matching all of them.
* - null: return a new [[ActiveQuery]] object for further query purpose.
*
* @return ActiveQuery|static|null When `$q` is null, a new [[ActiveQuery]] instance
* is returned; when `$q` is a scalar or an array, an ActiveRecord object matching it will be
* returned (null will be returned if there is no matching).
* @throws InvalidConfigException if the AR class does not have a primary key
* @see createQuery()
* @inheritdoc
*/
public
static
function
find
(
$q
=
null
)
public
static
function
find
()
{
$query
=
static
::
createQuery
();
$args
=
func_get_args
();
if
(
empty
(
$args
))
{
return
$query
;
}
$q
=
reset
(
$args
);
if
(
is_array
(
$q
))
{
return
$query
->
andWhere
(
$q
)
->
one
();
}
else
if
(
$q
!==
null
)
{
}
else
{
// query by primary key
$primaryKey
=
static
::
primaryKey
();
if
(
isset
(
$primaryKey
[
0
]))
{
...
...
@@ -148,8 +114,6 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
throw
new
InvalidConfigException
(
get_called_class
()
.
' must have a primary key.'
);
}
}
return
$query
;
}
/**
...
...
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