ThinkPHP converts the database query result data to the corresponding type. thinkphp converts the database

Source: Internet
Author: User

ThinkPHP converts the database query result data to the corresponding type. thinkphp converts the database

The example in this article describes how ThinkPHP converts the database query result data to the corresponding type. We will share this with you for your reference. The details are as follows:

I recently used ThinkPHP3.2.3 for API development and found that all the field value types returned by the ThinkPHP3.x query database are String. I didn't pay much attention to this when I used to develop the web. Now I find it difficult to use APIs for development. The data type is incorrect. Each field cannot be forcibly converted by the client.

After checking the data, we found that Model. class. php of ThinkPHP3.x provides the _ parseType method, which converts the data type after the query, but we need to manually tune it.

You need to write a Model base class by yourself:

MBaseModel. class. php inherits from Model

use Think\Model;class BaseModel extends Model{  protected function _after_select(&$resultSet, $options)  {    parent::_after_select($resultSet,$options);    foreach ($resultSet as &$result) {      $this->_after_find($result, $options);    }  }  protected function _after_find(&$result, $options)  {    parent::_after_find($result,$options);    foreach ($result as $field => $value) {      $this->_parseType($result, $field);    }  }}

All Model classes written by the user are inherited from the MBaseModel.

Note: The above two methods must be written to the subclass of the Model.

Originally, this was done, but we found a low-level bug in the _ parseType method of Model. class. php:

/*** Data type check ** @ access protected * @ param mixed $ data * @ param string $ key field name * @ return void */protected function _ parseType (& $ data, $ key) {if (! Isset ($ this-> options ['bind'] [':'. $ key]) & isset ($ this-> fields ['_ type'] [$ key]) {$ fieldType = strtolower ($ this-> fields ['_ type'] [$ key]); if (false! = Strpos ($ fieldType, 'enum') {// supports enum type priority detection} elseif (false === strpos ($ fieldType, 'bigint') & amp; false! = Strpos ($ fieldType, 'int') {$ data [$ key] = intval ($ data [$ key]);} elseif (false! = Strpos ($ fieldType, 'float') | false! = Strpos ($ fieldType, 'double') {$ data [$ key] = floatval ($ data [$ key]);} elseif (false! = Strpos ($ fieldType, 'bool ') {$ data [$ key] = (bool) $ data [$ key] ;}} // modify the 13th rows above to} elseif (false! = Strpos ($ fieldType, 'bigint') | false! = Strpos ($ fieldType, 'int') | false! = Strpos ($ fieldType, 'tinyint ')){

Related Article

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.