Fleaphp Framework Database query criteria ($conditions) Summary, fleaphpconditions_php Tutorial

Source: Internet
Author: User

Fleaphp Framework Database query criteria ($conditions) Summary, fleaphpconditions


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

In fleaphp, all the functions that use database query need to query the condition parameter $conditions, now the usage is as follows:

Example:

$conditions Save Query conditions $conditions = ' Level_ix > 1 ';//$tableOrders is a table data entry object for an order data table $order = $tableOrders->find ($ Conditions, ' created DESC ', ' ID, title, Body '), $conditions = Array (' username ' = ' dualface ');//$tableUsers is a user information data sheet The table data entry Object $user = $tableUsers->find ($conditions);

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

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

Query for records with a primary key field value of 1 $user = $tableUsers->find (1);//If the primary key field is named "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 can support the most flexible query conditions. For example:

$conditions = ' ID < 3 ' $user = $tableUsers->find ($conditions);//The WHERE clause generated 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 the key name and the field value equals the key value. For example:

The Query ID field value is 3 records $conditions = Array (  ' id ' = ' 1 ',), $user = $tableUsers->find ($conditions);//The generated WHERE clause is " WHERE ' id ' = 1 "

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

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

3.3. When $conditions an array, you can mix strings and key-value pairs in two flavors:

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

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

3.4. The implementation of the "in ()" Query 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 how to write in the condition?

If the primary key is named "id", one of the values of the query ID is 1, 2, 3, you can write this: $condition = Array (  ' in () ' = = Array (three-way),) $user = $tableUsers Find ($conditions);//The generated WHERE clause is "where ' id ' in (1, 2, 3)"

So what if it's not the primary key? It is also simple to provide key-value pairs. 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 usages of other parameters in the 4.find () function are as follows:

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

4.2. $fields parameter specifies which fields are to be included 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 unwanted fields.

The $fields parameter can be either a comma-delimited field name or an array with multiple field names, for example:

$fields = Array (' title ', ' created ');//can also be written in the following string form, the same as the two, the difference is 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 occurs. The suggested handwriting is also added "'" character $fields = ' title, created '; $user = $tableUsers->find (' ID < ', NULL, $fields);

It is recommended to use an array so that the table data entry is processed faster.

It is hoped that this article is helpful to the PHP program design based on fleaphp framework.

Articles you may be interested in:

    • Php method for importing CVS data into MySQL based on fleaphp framework
    • Fleaphp Rolesnamefield Bug Resolution method
    • How to use the Find function of fleaphp crud operation
    • How to use the Findbyfield function of fleaphp crud operation
    • fleaphp methods of pager used in the common method of paging
    • Fleaphp Method of security setting
    • A clever solution to the uncertain multi-condition query under fleaphp

http://www.bkjia.com/PHPjc/1111893.html www.bkjia.com true http://www.bkjia.com/PHPjc/1111893.html techarticle fleaphp Framework database query conditions ($conditions) To summarize, fleaphpconditions This article describes the fleaphp Framework database query conditions ($conditions) writing. Share for everyone to join us ...

  • 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.