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