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
2efb5aaf
Commit
2efb5aaf
authored
Apr 02, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added filterWhere guide.
parent
0fa19291
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
1 deletion
+26
-1
query-builder.md
docs/guide/query-builder.md
+26
-1
No files found.
docs/guide/query-builder.md
View file @
2efb5aaf
...
...
@@ -214,7 +214,7 @@ Operator can be one of the following:
It will build a
`EXISTS (sub-query)`
expression.
-
`not exists`
: similar to the
`exists`
operator and builds a
`NOT EXISTS (sub-query)`
expression.
If you are building parts of condition dynamically it's very convenient to use
`andWhere
`
and
`orWhere
`
:
If you are building parts of condition dynamically it's very convenient to use
`andWhere
()`
and
`orWhere()
`
:
```
php
$status
=
10
;
...
...
@@ -232,6 +232,31 @@ In case `$search` isn't empty the following SQL will be generated:
WHERE
(
`status`
=
10
)
AND
(
`title`
LIKE
'%yii%'
)
```
#### Building Filter Conditions
When building filter conditions based on user inputs, you usually want to specially handle "empty inputs"
by ignoring them in the filters. For example, you have an HTML form that takes username and email inputs.
If the user only enters something in the username input, you may want to build a query that only tries to
match the entered username. You may use the
`filterWhere()`
method achieve this goal:
```
php
// $username and $email are from user inputs
$query
->
filterWhere
([
'username'
=>
$username
,
'email'
=>
$email
,
]);
```
The
`filterWhere()`
method is very similar to
`where()`
. The main difference is that
`filterWhere()`
will remove empty values from the provided condition. So if
`$email`
is "empty", the resulting query
will be
`...WHERE username=:username`
; and if both
`$username`
and
`$email`
are "empty", the query
will have no
`WHERE`
part.
A value is
*empty*
if it is null, an empty string, a string consisting of whitespaces, or an empty array.
You may also use
`andFilterWhere()`
and
`orFilterWhere()`
to append more filter conditions.
### `ORDER BY`
For ordering results
`orderBy`
and
`addOrderBy`
could be used:
...
...
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