thinkphp Convert database query data to corresponding type

Source: Internet
Author: User
By default, all of the field value types queried by thinkphp are string, and if you are developing the web, of course, no problem, but the development of the interface, it is troublesome, always can not let the client to go to the type.

Thinkphp Model.class.php, provides the _parsetype method, can be done after the query, do type conversion, but the framework is not so dry, we need to manually adjust.

Write a model base class:

BaseModel.class.php, because I use the correlation query, so inherit from Relationmodel

Use Think\model;use think\model\relationmodel;class Basemodel extends relationmodel{    //After query, convert data type    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 inherit from Basemodel.

Originally, this has been done, but found Model.class.php _parsetype method has a low-level bug, look at the picture:

©2016, frozen fish. Please respect the author's labor results, copy reprint reservation site link! Application Development notes

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