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
96f1c4c1
Commit
96f1c4c1
authored
Apr 02, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renamed Query::filter() to Query::filterWhere()
parent
e5ba8c87
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
145 additions
and
156 deletions
+145
-156
Generator.php
extensions/gii/generators/crud/Generator.php
+2
-2
Query.php
extensions/sphinx/Query.php
+6
-12
Query.php
framework/db/Query.php
+33
-38
QueryInterface.php
framework/db/QueryInterface.php
+9
-9
QueryTrait.php
framework/db/QueryTrait.php
+85
-85
QueryTest.php
tests/unit/extensions/elasticsearch/QueryTest.php
+1
-1
QueryTest.php
tests/unit/extensions/mongodb/QueryTest.php
+1
-1
QueryTest.php
tests/unit/extensions/sphinx/QueryTest.php
+4
-4
QueryTest.php
tests/unit/framework/db/QueryTest.php
+4
-4
No files found.
extensions/gii/generators/crud/Generator.php
View file @
96f1c4c1
...
@@ -400,10 +400,10 @@ class Generator extends \yii\gii\Generator
...
@@ -400,10 +400,10 @@ class Generator extends \yii\gii\Generator
case
Schema
::
TYPE_TIME
:
case
Schema
::
TYPE_TIME
:
case
Schema
::
TYPE_DATETIME
:
case
Schema
::
TYPE_DATETIME
:
case
Schema
::
TYPE_TIMESTAMP
:
case
Schema
::
TYPE_TIMESTAMP
:
$conditions
[]
=
"
\$
query->andFilter(['
{
$column
}
' =>
\$
this->
{
$column
}
]);"
;
$conditions
[]
=
"
\$
query->andFilter
Where
(['
{
$column
}
' =>
\$
this->
{
$column
}
]);"
;
break
;
break
;
default
:
default
:
$conditions
[]
=
"
\$
this->a
ddFilter
(['like', '
{
$column
}
',
\$
this->
{
$column
}
]);"
;
$conditions
[]
=
"
\$
this->a
ndFilterWhere
(['like', '
{
$column
}
',
\$
this->
{
$column
}
]);"
;
break
;
break
;
}
}
}
}
...
...
extensions/sphinx/Query.php
View file @
96f1c4c1
...
@@ -466,7 +466,6 @@ class Query extends Component implements QueryInterface
...
@@ -466,7 +466,6 @@ class Query extends Component implements QueryInterface
{
{
$this
->
where
=
$condition
;
$this
->
where
=
$condition
;
$this
->
addParams
(
$params
);
$this
->
addParams
(
$params
);
return
$this
;
return
$this
;
}
}
...
@@ -480,13 +479,12 @@ class Query extends Component implements QueryInterface
...
@@ -480,13 +479,12 @@ class Query extends Component implements QueryInterface
* @see andFilter()
* @see andFilter()
* @see orFilter()
* @see orFilter()
*/
*/
public
function
filter
(
$condition
,
$params
=
[])
public
function
filter
Where
(
$condition
,
$params
=
[])
{
{
$condition
=
$this
->
filterCondition
(
$condition
);
$condition
=
$this
->
filterCondition
(
$condition
);
if
(
$condition
!==
[])
{
if
(
$condition
!==
[])
{
$this
->
where
(
$condition
,
$params
);
$this
->
where
(
$condition
,
$params
);
}
}
return
$this
;
return
$this
;
}
}
...
@@ -508,7 +506,6 @@ class Query extends Component implements QueryInterface
...
@@ -508,7 +506,6 @@ class Query extends Component implements QueryInterface
$this
->
where
=
[
'and'
,
$this
->
where
,
$condition
];
$this
->
where
=
[
'and'
,
$this
->
where
,
$condition
];
}
}
$this
->
addParams
(
$params
);
$this
->
addParams
(
$params
);
return
$this
;
return
$this
;
}
}
...
@@ -523,13 +520,12 @@ class Query extends Component implements QueryInterface
...
@@ -523,13 +520,12 @@ class Query extends Component implements QueryInterface
* @see filter()
* @see filter()
* @see orFilter()
* @see orFilter()
*/
*/
public
function
andFilter
(
$condition
,
$params
=
[])
public
function
andFilter
Where
(
$condition
,
$params
=
[])
{
{
$condition
=
$this
->
filterCondition
(
$condition
);
$condition
=
$this
->
filterCondition
(
$condition
);
if
(
$condition
!==
[])
{
if
(
$condition
!==
[])
{
$this
->
andWhere
(
$condition
,
$params
);
$this
->
andWhere
(
$condition
,
$params
);
}
}
return
$this
;
return
$this
;
}
}
...
@@ -551,7 +547,6 @@ class Query extends Component implements QueryInterface
...
@@ -551,7 +547,6 @@ class Query extends Component implements QueryInterface
$this
->
where
=
[
'or'
,
$this
->
where
,
$condition
];
$this
->
where
=
[
'or'
,
$this
->
where
,
$condition
];
}
}
$this
->
addParams
(
$params
);
$this
->
addParams
(
$params
);
return
$this
;
return
$this
;
}
}
...
@@ -566,13 +561,12 @@ class Query extends Component implements QueryInterface
...
@@ -566,13 +561,12 @@ class Query extends Component implements QueryInterface
* @see filter()
* @see filter()
* @see andFilter()
* @see andFilter()
*/
*/
public
function
orFilter
(
$condition
,
$params
=
[])
public
function
orFilter
Where
(
$condition
,
$params
=
[])
{
{
$condition
=
$this
->
filterCondition
(
$condition
);
$condition
=
$this
->
filterCondition
(
$condition
);
if
(
$condition
!==
[])
{
if
(
$condition
!==
[])
{
$this
->
orWhere
(
$condition
,
$params
);
$this
->
orWhere
(
$condition
,
$params
);
}
}
return
$this
;
return
$this
;
}
}
...
@@ -824,7 +818,7 @@ class Query extends Component implements QueryInterface
...
@@ -824,7 +818,7 @@ class Query extends Component implements QueryInterface
case
'OR'
:
case
'OR'
:
for
(
$i
=
1
,
$operandsCount
=
count
(
$condition
);
$i
<
$operandsCount
;
$i
++
)
{
for
(
$i
=
1
,
$operandsCount
=
count
(
$condition
);
$i
<
$operandsCount
;
$i
++
)
{
$subCondition
=
$this
->
filterCondition
(
$condition
[
$i
]);
$subCondition
=
$this
->
filterCondition
(
$condition
[
$i
]);
if
(
$this
->
p
arameterNotEmpty
(
$subCondition
))
{
if
(
$this
->
isP
arameterNotEmpty
(
$subCondition
))
{
$condition
[
$i
]
=
$subCondition
;
$condition
[
$i
]
=
$subCondition
;
}
else
{
}
else
{
unset
(
$condition
[
$i
]);
unset
(
$condition
[
$i
]);
...
@@ -848,13 +842,13 @@ class Query extends Component implements QueryInterface
...
@@ -848,13 +842,13 @@ class Query extends Component implements QueryInterface
case
'OR LIKE'
:
case
'OR LIKE'
:
case
'NOT LIKE'
:
case
'NOT LIKE'
:
case
'OR NOT LIKE'
:
case
'OR NOT LIKE'
:
if
(
!
$this
->
p
arameterNotEmpty
(
$condition
[
2
]))
{
if
(
!
$this
->
isP
arameterNotEmpty
(
$condition
[
2
]))
{
$condition
=
[];
$condition
=
[];
}
}
break
;
break
;
case
'BETWEEN'
:
case
'BETWEEN'
:
case
'NOT BETWEEN'
:
case
'NOT BETWEEN'
:
if
(
!
$this
->
parameterNotEmpty
(
$condition
[
2
])
&&
!
$this
->
p
arameterNotEmpty
(
$condition
[
3
]))
{
if
(
!
$this
->
isParameterNotEmpty
(
$condition
[
2
])
&&
!
$this
->
isP
arameterNotEmpty
(
$condition
[
3
]))
{
$condition
=
[];
$condition
=
[];
}
}
break
;
break
;
...
...
framework/db/Query.php
View file @
96f1c4c1
...
@@ -526,92 +526,88 @@ class Query extends Component implements QueryInterface
...
@@ -526,92 +526,88 @@ class Query extends Component implements QueryInterface
{
{
$this
->
where
=
$condition
;
$this
->
where
=
$condition
;
$this
->
addParams
(
$params
);
$this
->
addParams
(
$params
);
return
$this
;
return
$this
;
}
}
/**
/**
*
Sets the WHERE part of the query ignoring empty parameters
.
*
Adds an additional WHERE condition to the existing one
.
*
*
The new condition and the existing one will be joined using the 'AND' operator.
* @param string|array $condition the
conditions that should be put in the WHERE part
. Please refer to [[where()]]
* @param string|array $condition the
new WHERE condition
. Please refer to [[where()]]
* on how to specify this parameter.
* on how to specify this parameter.
* @param array $params the parameters (name => value) to be bound to the query.
* @param array $params the parameters (name => value) to be bound to the query.
* @return static the query object itself
* @return static the query object itself
* @see
andFilter
()
* @see
where
()
* @see or
Filter
()
* @see or
Where
()
*/
*/
public
function
filter
(
$condition
,
$params
=
[])
public
function
andWhere
(
$condition
,
$params
=
[])
{
{
$condition
=
$this
->
filterCondition
(
$condition
);
if
(
$this
->
where
===
null
)
{
if
(
$condition
!==
[])
{
$this
->
where
=
$condition
;
$this
->
where
(
$condition
,
$params
);
}
else
{
$this
->
where
=
[
'and'
,
$this
->
where
,
$condition
];
}
}
$this
->
addParams
(
$params
);
return
$this
;
return
$this
;
}
}
/**
/**
* Adds an additional WHERE condition to the existing one.
* Adds an additional WHERE condition to the existing one.
* The new condition and the existing one will be joined using the '
AND
' operator.
* The new condition and the existing one will be joined using the '
OR
' operator.
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter.
* on how to specify this parameter.
* @param array $params the parameters (name => value) to be bound to the query.
* @param array $params the parameters (name => value) to be bound to the query.
* @return static the query object itself
* @return static the query object itself
* @see where()
* @see where()
* @see
or
Where()
* @see
and
Where()
*/
*/
public
function
and
Where
(
$condition
,
$params
=
[])
public
function
or
Where
(
$condition
,
$params
=
[])
{
{
if
(
$this
->
where
===
null
)
{
if
(
$this
->
where
===
null
)
{
$this
->
where
=
$condition
;
$this
->
where
=
$condition
;
}
else
{
}
else
{
$this
->
where
=
[
'
and
'
,
$this
->
where
,
$condition
];
$this
->
where
=
[
'
or
'
,
$this
->
where
,
$condition
];
}
}
$this
->
addParams
(
$params
);
$this
->
addParams
(
$params
);
return
$this
;
return
$this
;
}
}
/**
/**
* Adds an additional WHERE condition to the existing one ignoring empty parameters.
* Sets the WHERE part of the query ignoring empty parameters.
* The new condition and the existing one will be joined using the 'AND' operator.
*
*
* @param string|array $condition the
new WHERE condition
. Please refer to [[where()]]
* @param string|array $condition the
conditions that should be put in the WHERE part
. Please refer to [[where()]]
* on how to specify this parameter.
* on how to specify this parameter.
* @param array $params the parameters (name => value) to be bound to the query.
* @param array $params the parameters (name => value) to be bound to the query.
* @return static the query object itself
* @return static the query object itself
* @see
f
ilter()
* @see
andF
ilter()
* @see orFilter()
* @see orFilter()
*/
*/
public
function
andFilter
(
$condition
,
$params
=
[])
public
function
filterWhere
(
$condition
,
$params
=
[])
{
{
$condition
=
$this
->
filterCondition
(
$condition
);
$condition
=
$this
->
filterCondition
(
$condition
);
if
(
$condition
!==
[])
{
if
(
$condition
!==
[])
{
$this
->
andW
here
(
$condition
,
$params
);
$this
->
w
here
(
$condition
,
$params
);
}
}
return
$this
;
return
$this
;
}
}
/**
/**
* Adds an additional WHERE condition to the existing one.
* Adds an additional WHERE condition to the existing one ignoring empty parameters.
* The new condition and the existing one will be joined using the 'OR' operator.
* The new condition and the existing one will be joined using the 'AND' operator.
*
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter.
* on how to specify this parameter.
* @param array $params the parameters (name => value) to be bound to the query.
* @param array $params the parameters (name => value) to be bound to the query.
* @return static the query object itself
* @return static the query object itself
* @see
where
()
* @see
filter
()
* @see
andWhere
()
* @see
orFilter
()
*/
*/
public
function
o
rWhere
(
$condition
,
$params
=
[])
public
function
andFilte
rWhere
(
$condition
,
$params
=
[])
{
{
if
(
$this
->
where
===
null
)
{
$condition
=
$this
->
filterCondition
(
$condition
);
$this
->
where
=
$condition
;
if
(
$condition
!==
[])
{
}
else
{
$this
->
andWhere
(
$condition
,
$params
);
$this
->
where
=
[
'or'
,
$this
->
where
,
$condition
];
}
}
$this
->
addParams
(
$params
);
return
$this
;
return
$this
;
}
}
...
@@ -626,13 +622,12 @@ class Query extends Component implements QueryInterface
...
@@ -626,13 +622,12 @@ class Query extends Component implements QueryInterface
* @see filter()
* @see filter()
* @see andFilter()
* @see andFilter()
*/
*/
public
function
orFilter
(
$condition
,
$params
=
[])
public
function
orFilter
Where
(
$condition
,
$params
=
[])
{
{
$condition
=
$this
->
filterCondition
(
$condition
);
$condition
=
$this
->
filterCondition
(
$condition
);
if
(
$condition
!==
[])
{
if
(
$condition
!==
[])
{
$this
->
orWhere
(
$condition
,
$params
);
$this
->
orWhere
(
$condition
,
$params
);
}
}
return
$this
;
return
$this
;
}
}
...
@@ -911,7 +906,7 @@ class Query extends Component implements QueryInterface
...
@@ -911,7 +906,7 @@ class Query extends Component implements QueryInterface
case
'OR'
:
case
'OR'
:
for
(
$i
=
1
,
$operandsCount
=
count
(
$condition
);
$i
<
$operandsCount
;
$i
++
)
{
for
(
$i
=
1
,
$operandsCount
=
count
(
$condition
);
$i
<
$operandsCount
;
$i
++
)
{
$subCondition
=
$this
->
filterCondition
(
$condition
[
$i
]);
$subCondition
=
$this
->
filterCondition
(
$condition
[
$i
]);
if
(
$this
->
p
arameterNotEmpty
(
$subCondition
))
{
if
(
$this
->
isP
arameterNotEmpty
(
$subCondition
))
{
$condition
[
$i
]
=
$subCondition
;
$condition
[
$i
]
=
$subCondition
;
}
else
{
}
else
{
unset
(
$condition
[
$i
]);
unset
(
$condition
[
$i
]);
...
@@ -935,13 +930,13 @@ class Query extends Component implements QueryInterface
...
@@ -935,13 +930,13 @@ class Query extends Component implements QueryInterface
case
'OR LIKE'
:
case
'OR LIKE'
:
case
'NOT LIKE'
:
case
'NOT LIKE'
:
case
'OR NOT LIKE'
:
case
'OR NOT LIKE'
:
if
(
!
$this
->
p
arameterNotEmpty
(
$condition
[
2
]))
{
if
(
!
$this
->
isP
arameterNotEmpty
(
$condition
[
2
]))
{
$condition
=
[];
$condition
=
[];
}
}
break
;
break
;
case
'BETWEEN'
:
case
'BETWEEN'
:
case
'NOT BETWEEN'
:
case
'NOT BETWEEN'
:
if
(
!
$this
->
parameterNotEmpty
(
$condition
[
2
])
&&
!
$this
->
p
arameterNotEmpty
(
$condition
[
3
]))
{
if
(
!
$this
->
isParameterNotEmpty
(
$condition
[
2
])
&&
!
$this
->
isP
arameterNotEmpty
(
$condition
[
3
]))
{
$condition
=
[];
$condition
=
[];
}
}
break
;
break
;
...
...
framework/db/QueryInterface.php
View file @
96f1c4c1
...
@@ -149,10 +149,10 @@ interface QueryInterface
...
@@ -149,10 +149,10 @@ interface QueryInterface
* @param array $condition the conditions that should be put in the WHERE part. Please refer to [[where()]]
* @param array $condition the conditions that should be put in the WHERE part. Please refer to [[where()]]
* on how to specify this parameter.
* on how to specify this parameter.
* @return static the query object itself
* @return static the query object itself
* @see andFilter()
* @see andFilter
Where
()
* @see orFilter()
* @see orFilter
Where
()
*/
*/
public
function
filter
(
$condition
);
public
function
filter
Where
(
$condition
);
/**
/**
* Adds an additional WHERE condition to the existing one.
* Adds an additional WHERE condition to the existing one.
...
@@ -171,10 +171,10 @@ interface QueryInterface
...
@@ -171,10 +171,10 @@ interface QueryInterface
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter.
* on how to specify this parameter.
* @return static the query object itself
* @return static the query object itself
* @see filter()
* @see filter
Where
()
* @see orFilter()
* @see orFilter
Where
()
*/
*/
public
function
andFilter
(
$condition
);
public
function
andFilter
Where
(
$condition
);
/**
/**
* Adds an additional WHERE condition to the existing one.
* Adds an additional WHERE condition to the existing one.
...
@@ -193,10 +193,10 @@ interface QueryInterface
...
@@ -193,10 +193,10 @@ interface QueryInterface
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter.
* on how to specify this parameter.
* @return static the query object itself
* @return static the query object itself
* @see filter()
* @see filter
Where
()
* @see andFilter()
* @see andFilter
Where
()
*/
*/
public
function
orFilter
(
$condition
);
public
function
orFilter
Where
(
$condition
);
/**
/**
* Sets the ORDER BY part of the query.
* Sets the ORDER BY part of the query.
...
...
framework/db/QueryTrait.php
View file @
96f1c4c1
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
*/
*/
namespace
yii\db
;
namespace
yii\db
;
use
yii\base\NotSupportedException
;
/**
/**
* The BaseQuery trait represents the minimum method set of a database Query.
* The BaseQuery trait represents the minimum method set of a database Query.
...
@@ -67,7 +68,6 @@ trait QueryTrait
...
@@ -67,7 +68,6 @@ trait QueryTrait
public
function
indexBy
(
$column
)
public
function
indexBy
(
$column
)
{
{
$this
->
indexBy
=
$column
;
$this
->
indexBy
=
$column
;
return
$this
;
return
$this
;
}
}
...
@@ -84,149 +84,154 @@ trait QueryTrait
...
@@ -84,149 +84,154 @@ trait QueryTrait
public
function
where
(
$condition
)
public
function
where
(
$condition
)
{
{
$this
->
where
=
$condition
;
$this
->
where
=
$condition
;
return
$this
;
return
$this
;
}
}
/**
/**
* Returns true if value passed is null, empty string, blank string, or empty array.
* Adds an additional WHERE condition to the existing one.
*
* The new condition and the existing one will be joined using the 'AND' operator.
* @param $value
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* @return boolean if parameter is empty
* on how to specify this parameter.
* @return static the query object itself
* @see where()
* @see orWhere()
*/
*/
protected
function
parameterNotEmpty
(
$value
)
public
function
andWhere
(
$condition
)
{
if
(
is_string
(
$value
))
{
$value
=
trim
(
$value
);
}
return
$value
!==
''
&&
$value
!==
[]
&&
$value
!==
null
;
}
/**
* Returns new condition with empty (null, empty string, blank string, or empty array) parameters in hash format
* removed
*
* @param array $condition original condition
* @return array condition with empty parameters removed
*/
protected
function
filterHashCondition
(
$condition
)
{
{
if
(
is_array
(
$condition
)
&&
!
isset
(
$condition
[
0
]))
{
if
(
$this
->
where
===
null
)
{
// hash format: 'column1' => 'value1', 'column2' => 'value2', ...
$this
->
where
=
$condition
;
$condition
=
array_filter
(
$condition
,
[
$this
,
'parameterNotEmpty'
]);
}
else
{
$this
->
where
=
[
'and'
,
$this
->
where
,
$condition
];
}
}
return
$
condition
;
return
$
this
;
}
}
/**
/**
* Returns new condition with empty (null, empty string, blank string, or empty array) parameters removed
* Adds an additional WHERE condition to the existing one.
*
* The new condition and the existing one will be joined using the 'OR' operator.
* @param array $condition original condition
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* @return array condition with empty parameters removed
* on how to specify this parameter.
* @return static the query object itself
* @see where()
* @see andWhere()
*/
*/
p
rotected
function
filterCondition
(
$condition
)
p
ublic
function
orWhere
(
$condition
)
{
{
return
$this
->
filterHashCondition
(
$condition
);
if
(
$this
->
where
===
null
)
{
$this
->
where
=
$condition
;
}
else
{
$this
->
where
=
[
'or'
,
$this
->
where
,
$condition
];
}
return
$this
;
}
}
/**
/**
* Sets the WHERE part of the query
ignoring empty parameters
.
* Sets the WHERE part of the query
but ignores [[isParameterNotEmpty|empty parameters]]
.
*
*
* See [[QueryInterface::where()]] for detailed documentation.
* This function can be used to pass fields of a search form directly as search condition
* by ignoring fields that have not been filled.
*
*
* @param array $condition the conditions that should be put in the WHERE part.
* @param array $condition the conditions that should be put in the WHERE part.
* See [[where()]] on how to specify this parameter.
* @return static the query object itself
* @return static the query object itself
* @see andFilter()
* @see where()
* @see orFilter()
* @see andFilterWhere()
* @see orFilterWhere()
*/
*/
public
function
filter
(
$condition
)
public
function
filter
Where
(
$condition
)
{
{
$condition
=
$this
->
filterCondition
(
$condition
);
$condition
=
$this
->
filterCondition
(
$condition
);
if
(
$condition
!==
[])
{
if
(
$condition
!==
[])
{
$this
->
where
(
$condition
);
$this
->
where
(
$condition
);
}
}
return
$this
;
return
$this
;
}
}
/**
/**
* Adds an additional WHERE condition to the existing one.
* Adds an additional WHERE condition to the existing one
but ignores [[isParameterNotEmpty|empty parameters]]
.
* The new condition and the existing one will be joined using the 'AND' operator.
* The new condition and the existing one will be joined using the 'AND' operator.
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter.
* on how to specify this parameter.
* @return static the query object itself
* @return static the query object itself
* @see
w
here()
* @see
filterW
here()
* @see orWhere()
* @see or
Filter
Where()
*/
*/
public
function
andWhere
(
$condition
)
public
function
and
Filter
Where
(
$condition
)
{
{
if
(
$this
->
where
===
null
)
{
$condition
=
$this
->
filterCondition
(
$condition
);
$this
->
where
=
$condition
;
if
(
$condition
!==
[])
{
}
else
{
$this
->
andWhere
(
$condition
);
$this
->
where
=
[
'and'
,
$this
->
where
,
$condition
];
}
}
return
$this
;
return
$this
;
}
}
/**
/**
* Adds an additional WHERE condition to the existing one ignoring empty parameters.
* Adds an additional WHERE condition to the existing one but ignores [[isParameterNotEmpty|empty parameters]].
* The new condition and the existing one will be joined using the 'AND' operator.
* The new condition and the existing one will be joined using the 'OR' operator.
*
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter.
* on how to specify this parameter.
* @return static the query object itself
* @return static the query object itself
* @see filter()
* @see filter
Where
()
* @see
orFilter
()
* @see
andFilterWhere
()
*/
*/
public
function
andFilter
(
$condition
)
public
function
orFilterWhere
(
$condition
)
{
{
$condition
=
$this
->
filterCondition
(
$condition
);
$condition
=
$this
->
filterCondition
(
$condition
);
if
(
$condition
!==
[])
{
if
(
$condition
!==
[])
{
$this
->
and
Where
(
$condition
);
$this
->
or
Where
(
$condition
);
}
}
return
$this
;
return
$this
;
}
}
/**
/**
* Adds an additional WHERE condition to the existing one.
* Returns a new condition with [[isParameterNotEmpty|empty parameters]] removed.
* The new condition and the existing one will be joined using the 'OR' operator.
*
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* @param array $condition original condition
* on how to specify this parameter.
* @return array condition with [[isParameterNotEmpty|empty parameters]] removed.
* @return static the query object itself
* @see where()
* @see andWhere()
*/
*/
p
ublic
function
orWhere
(
$condition
)
p
rotected
function
filterCondition
(
$condition
)
{
{
if
(
$this
->
where
===
null
)
{
if
(
is_array
(
$condition
)
&&
!
isset
(
$condition
[
0
])
)
{
$this
->
where
=
$condition
;
return
$this
->
filterHashCondition
(
$condition
)
;
}
else
{
}
else
{
$this
->
where
=
[
'or'
,
$this
->
where
,
$condition
]
;
throw
new
NotSupportedException
(
'filterWhere() only supports hash condition format.'
)
;
}
}
return
$this
;
}
}
/**
/**
* Adds an additional WHERE condition to the existing one ignoring empty parameters.
* Returns `true` if value passed is not "empty".
* The new condition and the existing one will be joined using the 'OR' operator.
*
*
* @param string|array $condition the new WHERE condition. Please refer to [[where()]]
* The value is considered "empty", if
* on how to specify this parameter.
*
* @return static the query object itself
* - it is `null`,
* @see filter()
* - an empty string (`''`),
* @see andFilter()
* - a string containing only whitespace characters,
* - or an empty array.
*
* @param $value
* @return boolean if parameter is empty
*/
*/
p
ublic
function
orFilter
(
$condition
)
p
rotected
function
isParameterNotEmpty
(
$value
)
{
{
$condition
=
$this
->
filterCondition
(
$condition
);
if
(
is_string
(
$value
))
{
if
(
$condition
!==
[])
{
$value
=
trim
(
$value
);
$this
->
orWhere
(
$condition
);
}
}
return
$value
!==
''
&&
$value
!==
[]
&&
$value
!==
null
;
}
return
$this
;
/**
* Returns a new hash condition without [[isParameterNotEmpty|empty parameters]].
*
* @param array $condition original condition
* @return array condition without [[isParameterNotEmpty|empty parameters]].
*/
protected
function
filterHashCondition
(
$condition
)
{
if
(
is_array
(
$condition
)
&&
!
isset
(
$condition
[
0
]))
{
// hash format: 'column1' => 'value1', 'column2' => 'value2', ...
return
array_filter
(
$condition
,
[
$this
,
'isParameterNotEmpty'
]);
}
return
$condition
;
}
}
/**
/**
...
@@ -245,7 +250,6 @@ trait QueryTrait
...
@@ -245,7 +250,6 @@ trait QueryTrait
public
function
orderBy
(
$columns
)
public
function
orderBy
(
$columns
)
{
{
$this
->
orderBy
=
$this
->
normalizeOrderBy
(
$columns
);
$this
->
orderBy
=
$this
->
normalizeOrderBy
(
$columns
);
return
$this
;
return
$this
;
}
}
...
@@ -267,7 +271,6 @@ trait QueryTrait
...
@@ -267,7 +271,6 @@ trait QueryTrait
}
else
{
}
else
{
$this
->
orderBy
=
array_merge
(
$this
->
orderBy
,
$columns
);
$this
->
orderBy
=
array_merge
(
$this
->
orderBy
,
$columns
);
}
}
return
$this
;
return
$this
;
}
}
...
@@ -285,7 +288,6 @@ trait QueryTrait
...
@@ -285,7 +288,6 @@ trait QueryTrait
$result
[
$column
]
=
SORT_ASC
;
$result
[
$column
]
=
SORT_ASC
;
}
}
}
}
return
$result
;
return
$result
;
}
}
}
}
...
@@ -298,7 +300,6 @@ trait QueryTrait
...
@@ -298,7 +300,6 @@ trait QueryTrait
public
function
limit
(
$limit
)
public
function
limit
(
$limit
)
{
{
$this
->
limit
=
$limit
;
$this
->
limit
=
$limit
;
return
$this
;
return
$this
;
}
}
...
@@ -310,7 +311,6 @@ trait QueryTrait
...
@@ -310,7 +311,6 @@ trait QueryTrait
public
function
offset
(
$offset
)
public
function
offset
(
$offset
)
{
{
$this
->
offset
=
$offset
;
$this
->
offset
=
$offset
;
return
$this
;
return
$this
;
}
}
}
}
tests/unit/extensions/elasticsearch/QueryTest.php
View file @
96f1c4c1
...
@@ -155,7 +155,7 @@ class QueryTest extends ElasticSearchTestCase
...
@@ -155,7 +155,7 @@ class QueryTest extends ElasticSearchTestCase
{
{
// should work with hash format
// should work with hash format
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
filter
([
$query
->
filter
Where
([
'id'
=>
0
,
'id'
=>
0
,
'title'
=>
' '
,
'title'
=>
' '
,
'author_ids'
=>
[],
'author_ids'
=>
[],
...
...
tests/unit/extensions/mongodb/QueryTest.php
View file @
96f1c4c1
...
@@ -72,7 +72,7 @@ class QueryTest extends MongoDbTestCase
...
@@ -72,7 +72,7 @@ class QueryTest extends MongoDbTestCase
{
{
// should work with hash format
// should work with hash format
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
filter
([
$query
->
filter
Where
([
'id'
=>
0
,
'id'
=>
0
,
'title'
=>
' '
,
'title'
=>
' '
,
'author_ids'
=>
[],
'author_ids'
=>
[],
...
...
tests/unit/extensions/sphinx/QueryTest.php
View file @
96f1c4c1
...
@@ -64,13 +64,13 @@ class QueryTest extends SphinxTestCase
...
@@ -64,13 +64,13 @@ class QueryTest extends SphinxTestCase
{
{
// should just call where() when string is passed
// should just call where() when string is passed
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
filter
(
'id = :id'
,
[
':id'
=>
null
]);
$query
->
filter
Where
(
'id = :id'
,
[
':id'
=>
null
]);
$this
->
assertEquals
(
'id = :id'
,
$query
->
where
);
$this
->
assertEquals
(
'id = :id'
,
$query
->
where
);
$this
->
assertEquals
([
':id'
=>
null
],
$query
->
params
);
$this
->
assertEquals
([
':id'
=>
null
],
$query
->
params
);
// should work with hash format
// should work with hash format
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
filter
([
$query
->
filter
Where
([
'id'
=>
0
,
'id'
=>
0
,
'title'
=>
' '
,
'title'
=>
' '
,
'author_ids'
=>
[],
'author_ids'
=>
[],
...
@@ -86,7 +86,7 @@ class QueryTest extends SphinxTestCase
...
@@ -86,7 +86,7 @@ class QueryTest extends SphinxTestCase
// should work with operator format
// should work with operator format
$query
=
new
Query
;
$query
=
new
Query
;
$condition
=
[
'like'
,
'name'
,
'Alex'
];
$condition
=
[
'like'
,
'name'
,
'Alex'
];
$query
->
filter
(
$condition
);
$query
->
filter
Where
(
$condition
);
$this
->
assertEquals
(
$condition
,
$query
->
where
);
$this
->
assertEquals
(
$condition
,
$query
->
where
);
$query
->
andFilter
([
'between'
,
'id'
,
null
,
null
]);
$query
->
andFilter
([
'between'
,
'id'
,
null
,
null
]);
...
@@ -120,7 +120,7 @@ class QueryTest extends SphinxTestCase
...
@@ -120,7 +120,7 @@ class QueryTest extends SphinxTestCase
public
function
testFilterRecursively
()
public
function
testFilterRecursively
()
{
{
$query
=
new
Query
();
$query
=
new
Query
();
$query
->
filter
([
'and'
,
[
'like'
,
'name'
,
''
],
[
'like'
,
'title'
,
''
],
[
'id'
=>
1
],
[
'not'
,
[
'like'
,
'name'
,
''
]]]);
$query
->
filter
Where
([
'and'
,
[
'like'
,
'name'
,
''
],
[
'like'
,
'title'
,
''
],
[
'id'
=>
1
],
[
'not'
,
[
'like'
,
'name'
,
''
]]]);
$this
->
assertEquals
([
'id'
=>
1
],
$query
->
where
);
$this
->
assertEquals
([
'id'
=>
1
],
$query
->
where
);
}
}
...
...
tests/unit/framework/db/QueryTest.php
View file @
96f1c4c1
...
@@ -53,13 +53,13 @@ class QueryTest extends DatabaseTestCase
...
@@ -53,13 +53,13 @@ class QueryTest extends DatabaseTestCase
{
{
// should just call where() when string is passed
// should just call where() when string is passed
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
filter
(
'id = :id'
,
[
':id'
=>
null
]);
$query
->
filter
Where
(
'id = :id'
,
[
':id'
=>
null
]);
$this
->
assertEquals
(
'id = :id'
,
$query
->
where
);
$this
->
assertEquals
(
'id = :id'
,
$query
->
where
);
$this
->
assertEquals
([
':id'
=>
null
],
$query
->
params
);
$this
->
assertEquals
([
':id'
=>
null
],
$query
->
params
);
// should work with hash format
// should work with hash format
$query
=
new
Query
;
$query
=
new
Query
;
$query
->
filter
([
$query
->
filter
Where
([
'id'
=>
0
,
'id'
=>
0
,
'title'
=>
' '
,
'title'
=>
' '
,
'author_ids'
=>
[],
'author_ids'
=>
[],
...
@@ -75,7 +75,7 @@ class QueryTest extends DatabaseTestCase
...
@@ -75,7 +75,7 @@ class QueryTest extends DatabaseTestCase
// should work with operator format
// should work with operator format
$query
=
new
Query
;
$query
=
new
Query
;
$condition
=
[
'like'
,
'name'
,
'Alex'
];
$condition
=
[
'like'
,
'name'
,
'Alex'
];
$query
->
filter
(
$condition
);
$query
->
filter
Where
(
$condition
);
$this
->
assertEquals
(
$condition
,
$query
->
where
);
$this
->
assertEquals
(
$condition
,
$query
->
where
);
$query
->
andFilter
([
'between'
,
'id'
,
null
,
null
]);
$query
->
andFilter
([
'between'
,
'id'
,
null
,
null
]);
...
@@ -109,7 +109,7 @@ class QueryTest extends DatabaseTestCase
...
@@ -109,7 +109,7 @@ class QueryTest extends DatabaseTestCase
public
function
testFilterRecursively
()
public
function
testFilterRecursively
()
{
{
$query
=
new
Query
();
$query
=
new
Query
();
$query
->
filter
([
'and'
,
[
'like'
,
'name'
,
''
],
[
'like'
,
'title'
,
''
],
[
'id'
=>
1
],
[
'not'
,
[
'like'
,
'name'
,
''
]]]);
$query
->
filter
Where
([
'and'
,
[
'like'
,
'name'
,
''
],
[
'like'
,
'title'
,
''
],
[
'id'
=>
1
],
[
'not'
,
[
'like'
,
'name'
,
''
]]]);
$this
->
assertEquals
([
'id'
=>
1
],
$query
->
where
);
$this
->
assertEquals
([
'id'
=>
1
],
$query
->
where
);
}
}
...
...
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