Case sensitivity of database field names in thinkphp

Source: Internet
Author: User
Tags lowercase

When I use thinkphp, I have encountered the case-sensitivity problem of database field names. When I use the M () method, thinkphp searches for lower-case database fields by default. The solution is as follows:

Modify the Library/Db/Driver. class. Php file in the thinkphp source code

PDO: ATTR_CASE => PDO: CASE_LOWER changed to PDO: ATTR_CASE => PDO: CASE_NATURAL,

Or add

DB_PARAMS '=> array (\ PDO: ATTR_CASE => \ PDO: CASE_NATURAL.

This solution works for the first time, but it never works for another project. However, Baidu has found a solution for a long time, because thinkphp converts uppercase letters to lowercase letters by default, we can find the source code of thinkphp so that it cannot be converted.

Modify the source code of the parse_name function in Common/functions. php:


Function parse_name ($ name, $ type = 0 ){
/* If ($ type ){
Return ucfirst (preg_replace_callback ('/_ ([a-zA-Z])/', function ($ match) {return strtoupper ($ match [1]);}, $ name ));
} Else {
// Here, the upper case of the database table name is converted to the lower case, and the change is not converted.
Return strtolower (trim (preg_replace ("/[A-Z]/", "_ \ 0", $ name ),"_"));
}*/
Return $ name;
}

The comments are original, but not added.

Modify the getTableName function in Library/Think/Model. class. php:


// $ This-> trueTableName = strtolower ($ tableName); // The table name is converted to lowercase and not converted here.
$ This-> trueTableName = $ tableName;

Modify one of these statements to solve the problem of database field name capitalization.

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.