fleaphp Framework Database query condition ($conditions) Writing summary _php example

Source: Internet
Author: User

This article describes the fleaphp Framework database query conditions ($conditions). Share to everyone for your reference, specific as follows:

In fleaphp, the function that uses the database query, all need to inquire the condition parameter $conditions, now narrate the usage is as follows:

Example:

$conditions Save query conditions
$conditions = ' level_ix > 1 ';
$tableOrders is an order Datasheet table data Entry object
$order = $tableOrders->find ($conditions, ' created DESC ', ' ID, title, Body ');
$conditions = Array (' username ' => ' dualface ');
$tableUsers is a table data entry object for a user information datasheet
$user = $tableUsers->find ($conditions);

$conditions parameters can be integers, strings, and arrays of three types:

1. If the $conditions argument is an integer, the integer is assumed to be the primary key field value.

The record of the query primary key field value is 1
$user = $tableUsers->find (1);
If the primary key field name is ' ID ', the resulting where clause is ' where ' id ' = 1 '

2. If the $conditions parameter is a string, the string will be used directly as a query condition, which supports the most flexible query conditions. For example:

$conditions = ' ID < 3 '
$user = $tableUsers->find ($conditions);
The generated WHERE clause is "where ID < 3"

3.1. If the $conditions parameter is an array and the key name and value are specified, the field name in the query condition is a key name and the field value equals the key value. For example:

The record
$conditions = Array (
  ' id ' => ' 1 ') of the Query ID field value is 3
 ;
$user = $tableUsers->find ($conditions);
The generated WHERE clause is "where" id ' = 1 '

3.2. If the $conditions parameter is an array, but its elements do not have a key name, the key value is assumed to be a custom query condition, for example:

$conditions = Array (' id = 1 ');
The generated WHERE clause is "where ' id ' = 1"
$user = $tableUsers->find ($conditions);

3.3. When $conditions an array, you can mix string and key values in two styles:

$conditions = Array (
  ' ID < 3 ', '
  sex ' => ' male '
);
$user = $tableUsers->find ($conditions);
The generated WHERE clause is "ID < 3 and ' sex ' = ' male '"

When a $conditions is an array, the and Boolean operators are used to connect between multiple query conditions.

3.4. "In ()" Query implementation in fleaphp. (Originally published by Dreampig in http://www.fleaphp.org/bbs/viewthread.php?tid=2168)
We sometimes have to use in such operations, then in the condition how to write it?

If the primary key name is "id", the value of the query ID 1, 2, and 3 is one of them, you can write this:
$condition = Array (
  ' in () ' => Array (1,2,3),
)
$user = $ Tableusers->find ($conditions);
The generated WHERE clause is "WHERE" id ' in (1, 2, 3) "

So what if it's not a primary key? It is also simple to provide a key value pair. For example:

$condition = Array (
  ' in () ' => array ('
          username ' => Array (' username1 ', ' username2 ')
         )
$ user = $tableUsers->find ($conditions);
The generated WHERE clause is "where ' username ' in (' username1 ', ' username2 ')"

The meanings and uses of other parameters in the 4.find () function are as follows:

4.1. $sort parameter specifies how the query is sorted, and the type can only be a string
For example, ' created ASC ' means sorting from small to large according to the "Created" field.

4.2. $fields parameters specify which fields to include in the query results, and the type can be a string or an array
When you have a large number of fields in a datasheet, you can improve performance by specifying $fields parameters to avoid querying fields that you do not need.

The $fields argument can be either the field name separated by "," comma, or an array that contains multiple field names, such as:

$fields = Array (' title ', ' created ');
It can also be written in the following string form, with the same effect, except that the automatically generated field names will be added on both sides of the "'" symbol to prevent the occurrence of a field name conflict with the SQL keyword. It is recommended to add "'" character
$fields = ' title, created ';
$user = $tableUsers->find (' ID < ten ', NULL, $fields);

It is recommended that you use arrays so that the table data entry is processed faster.

I hope this article will help you with the PHP program design based on fleaphp framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.