thinkphp automatic creation of data object parsing

Source: Internet
Author: User

Thinkphp has a Create method that automatically creates a data object, the core code is as follows

   Public Function Create ($data = ', $type = ') {      //If no value is passed by default to the post data if      (empty ($data)) {          $data   =   I (' Post. ');      } ElseIf (Is_object ($data)) {          $data   =   get_object_vars ($data);      }      Determine if there is a primary key, there is representative modification, does not represent insert      $type = $type?:(! Empty ($data [$this->GETPK ()])? self::model_update:self::model_insert);      Generate the data object, first get the field $fields of the model, filter out the fields in $data that are not in field data (the last remaining table exists in the field)      if ($this->autocheckfields) {//open field detection Filter the illegal field data          $fields =   $this->getdbfields ();          foreach ($data as $key = + $val) {              if (!in_array ($key, $fields)) {                  unset ($data [$key]);              } ElseIf (MAGIC_QUOTES_GPC && is_string ($val)) {                  $data [$key] =   stripslashes ($val);}}      }      $this->data =   $data;      return $data;   }

Where the $this->getdbfields method is as follows, the method returns an indexed array of corresponding table field names

  /**   * Get data table field information   * @access public   * @return Array */public  function Getdbfields () {      if (isset ($ this->options[' table ')) {//dynamically specify          the name $array      =   explode (', $this->options[' table ');          $fields     =   $this->db->getfields ($array [0]);          Return  $fields? Array_keys ($fields): false;      }      if ($this->fields) {          $fields     =  $this->fields;          unset ($fields [' _type '], $fields [' _pk ']);          return $fields;      }      return false;  }

$this the->db->getfields method, which returns the field details for the corresponding table

  Public Function GetFields ($tableName) {      $result =   $this->query (' SHOW COLUMNS from '. $this->parsekey ($ TableName));      $info   =   Array ();      if ($result) {          foreach ($result as $key = + $val) {              $info [$val [' Field ']] = array (                  ' name ' + =    $val [' Field '],                  ' type ' =    = $val [' type '],                  ' notnull ' = = (bool) (Strtoupper ($val [' Null ']) = = = = ' NO '),//Not Null is empty, and null is Yes                  ' default ' = = $val [' Default '],                  ' primary ' = = (Strtolower ($val [' Key ')] = = = ' pri '),                  ' autoinc ' = (strtolower ($val [' Extra ']) = = ' Auto_increment '),              );}      }      return $info;  }

thinkphp automatic creation of data object parsing

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.