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
86f06677
Commit
86f06677
authored
Jun 25, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored AR findOne() and findAll().
parent
df6a9a08
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
47 deletions
+37
-47
ActiveRecord.php
framework/db/ActiveRecord.php
+16
-28
BaseActiveRecord.php
framework/db/BaseActiveRecord.php
+21
-19
No files found.
framework/db/ActiveRecord.php
View file @
86f06677
...
@@ -150,45 +150,33 @@ class ActiveRecord extends BaseActiveRecord
...
@@ -150,45 +150,33 @@ class ActiveRecord extends BaseActiveRecord
}
}
/**
/**
* @inheritdoc
* Finds ActiveRecord instance(s) by the given condition.
* @return static ActiveRecord instance matching the condition, or `null` if nothing matches.
* This method is internally called by [[findOne()]] and [[findAll()]].
* @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter
* @param boolean $one whether this method is called by [[findOne()]] or [[findAll()]]
* @return static|static[]
* @throws InvalidConfigException if there is no primary key defined
* @internal
*/
*/
p
ublic
static
function
findOne
(
$condition
)
p
rotected
static
function
findByCondition
(
$condition
,
$one
)
{
{
$query
=
static
::
find
();
$query
=
static
::
find
();
if
(
ArrayHelper
::
isAssociative
(
$condition
))
{
// hash condition
if
(
!
ArrayHelper
::
isAssociative
(
$condition
))
{
return
$query
->
andWhere
(
$condition
)
->
one
();
}
else
{
// query by primary key
// query by primary key
$primaryKey
=
static
::
primaryKey
();
$primaryKey
=
static
::
primaryKey
();
if
(
isset
(
$primaryKey
[
0
]))
{
if
(
isset
(
$primaryKey
[
0
]))
{
return
$query
->
andWhere
([
static
::
tableName
()
.
'.'
.
$primaryKey
[
0
]
=>
$condition
])
->
one
();
$pk
=
$primaryKey
[
0
];
if
(
!
empty
(
$query
->
join
)
||
!
empty
(
$query
->
joinWith
))
{
$pk
=
static
::
tableName
()
.
'.'
.
$pk
;
}
$condition
=
[
$pk
=>
$condition
];
}
else
{
}
else
{
throw
new
InvalidConfigException
(
get_called_class
()
.
' must have a primary key.'
);
throw
new
InvalidConfigException
(
get_called_class
()
.
' must have a primary key.'
);
}
}
}
}
}
/**
return
$one
?
$query
->
andWhere
(
$condition
)
->
one
()
:
$query
->
andWhere
(
$condition
)
->
all
();
* @inheritdoc
* @return static[] an array of ActiveRecord instances, or an empty array if nothing matches.
*/
public
static
function
findAll
(
$condition
)
{
$query
=
static
::
find
();
if
(
ArrayHelper
::
isAssociative
(
$condition
))
{
// hash condition
return
$query
->
andWhere
(
$condition
)
->
all
();
}
else
{
// query by primary key(s)
$primaryKey
=
static
::
primaryKey
();
if
(
isset
(
$primaryKey
[
0
]))
{
return
$query
->
andWhere
([
static
::
tableName
()
.
'.'
.
$primaryKey
[
0
]
=>
$condition
])
->
all
();
}
else
{
throw
new
InvalidConfigException
(
get_called_class
()
.
' must have a primary key.'
);
}
}
}
}
/**
/**
...
...
framework/db/BaseActiveRecord.php
View file @
86f06677
...
@@ -98,19 +98,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
...
@@ -98,19 +98,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
*/
*/
public
static
function
findOne
(
$condition
)
public
static
function
findOne
(
$condition
)
{
{
$query
=
static
::
find
();
return
static
::
findByCondition
(
$condition
,
true
);
if
(
ArrayHelper
::
isAssociative
(
$condition
))
{
// hash condition
return
$query
->
andWhere
(
$condition
)
->
one
();
}
else
{
// query by primary key
$primaryKey
=
static
::
primaryKey
();
if
(
isset
(
$primaryKey
[
0
]))
{
return
$query
->
andWhere
([
$primaryKey
[
0
]
=>
$condition
])
->
one
();
}
else
{
throw
new
InvalidConfigException
(
get_called_class
()
.
' must have a primary key.'
);
}
}
}
}
/**
/**
...
@@ -119,19 +107,33 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
...
@@ -119,19 +107,33 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
*/
*/
public
static
function
findAll
(
$condition
)
public
static
function
findAll
(
$condition
)
{
{
return
static
::
findByCondition
(
$condition
,
false
);
}
/**
* Finds ActiveRecord instance(s) by the given condition.
* This method is internally called by [[findOne()]] and [[findAll()]].
* @param mixed $condition please refer to [[findOne()]] for the explanation of this parameter
* @param boolean $one whether this method is called by [[findOne()]] or [[findAll()]]
* @return static|static[]
* @throws InvalidConfigException if there is no primary key defined
* @internal
*/
protected
static
function
findByCondition
(
$condition
,
$one
)
{
$query
=
static
::
find
();
$query
=
static
::
find
();
if
(
ArrayHelper
::
isAssociative
(
$condition
))
{
// hash condition
if
(
!
ArrayHelper
::
isAssociative
(
$condition
))
{
return
$query
->
andWhere
(
$condition
)
->
all
();
// query by primary key
}
else
{
// query by primary key(s)
$primaryKey
=
static
::
primaryKey
();
$primaryKey
=
static
::
primaryKey
();
if
(
isset
(
$primaryKey
[
0
]))
{
if
(
isset
(
$primaryKey
[
0
]))
{
return
$query
->
andWhere
([
$primaryKey
[
0
]
=>
$condition
])
->
all
()
;
$condition
=
[
$primaryKey
[
0
]
=>
$condition
]
;
}
else
{
}
else
{
throw
new
InvalidConfigException
(
get_called_class
()
.
' must have a primary key.'
);
throw
new
InvalidConfigException
(
get_called_class
()
.
' must have a primary key.'
);
}
}
}
}
return
$one
?
$query
->
andWhere
(
$condition
)
->
one
()
:
$query
->
andWhere
(
$condition
)
->
all
();
}
}
/**
/**
...
...
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