[Li Jingshan php] every day tp5-20170112|thinkphp5-model.php-5

Source: Internet
Author: User
Tags throw exception

/** *  setting the fields allowed to write  *  @access  public *  @param  bool|array  $field   Allow Write fields   If True only allow write to data table fields  *  @return   $this  */public function allowfield ($ field) {//  about database operations, set license fields     if  (true ===  $field)  {//  IF field  true         $field  =  $this->db ()->gettableinfo (' ',  ' type ');//  get   table fields    information          $this->db ( )->setfieldtype ($field);//  settings   field type          $field  =  array_keys ($field);//  return   field information     }     $this->field  =  $field;//  Otherwise   Get fields   SET field information to the current model     return  $this;//  Returns whether the object itself}/** *  Update data  *  @access  public *  @param  bool   $update  *  @param  mixed  $where  *  @return   $this  */public function isupdate ($update  =  true,  $where  = null) {//  is   update   data      $this isupdate =  $update;//  default   is  true    if  (!empty ($where))   {         $this->updatewhere =  $where; //  if not   Conditions   is not an UPDATE statement     }    return  $this;//  returns the object itself}/**  *  Data autocomplete  *  @access  public *  @param  array  $auto   List of fields to update automatically  *  @return  void */protected function autocompletedata ($auto  = []) {//   Data Auto-complete   auto-complete data ingress     foreach  ($auto  as  $field  => $ Value)  {//  cycle         if  (Is_integer ($field))  {//   If the field is an integral type              $field  =  $value;//  assignment fields   come               $value  = null;//  assignment to null          }        if  (!in_array ( $field,  $this->change)  {//  If field   is within range of change, use change function   settings               $this->setattr ($field,  !is_null ($value)  ?  $value  :  (Isset ($this->data[$field])  ?  $this->data[$field] :  $value));         }    }}/** *  Delete the current record  * @ access public *  @return  integer */public function delete () {//  were formed, respectively, on  db  two times   packaging     if  (false ===  $this->trigger (' Before_delete ',  $this)  {//  KaiThings before moving         return false;    }      $result  =  $this->db ()->delete ($this->data);//  Delete     $ This->trigger (' After_delete ',  $this);//  after the deletion of the things     return  $result;   Return Delete results}/** *  set auto-complete fields (  rules defined by modifier)  *  @access  public *  @param  array  $fields   fields that need to be auto-completed  *  @return   $this  */public function auto ($ Fields)//  AutoComplete   field {     $this->auto =  $fields;//  fields   Auto-complete   fields     return  $this;} /** *  setting Field validation  *  @access  public *  @param  array|string|bool  $rule   Validation Rule  true represents an automatic Read validator class  *  @param  array               $msg   Tips  *  @return   $this  */pubLic function validate ($rule  = true,  $msg  = [])//  setting fields   verifying {     if  (Is_array ($rule))  {//  settings   Auto-complete rules           $this->validate = [              ' rule '  =>  $rule,             ' Msg '   =>  $msg,         ];    }  else {         $this->validate = true ===   $rule  ?  $this->name :  $rule;//  Auto-complete   fields     }     return  $this;} /** *  set whether to throw an exception after validation fails  *  @access  public *  @param  bool  $fail   Throw exception  *  @return   $this  */public function validatefailexception ($fail = true) {     $this->failexception =  $fail;//    anomalies   Throw     return  $this;} /** *  Auto-Validate data  *  @access  protected *  @param  array  $data   Validate data  *  @return  bool */protected function validatedata ($data)//  automatically validate data {     if  (!empty ($this->validate))  {//  If the rule is empty   floats directly, otherwise           $info  =  $this->validate;//  Get information          if  (Is_array ($info))  {//  Arrays               $validate  = loader::validate ();//  Load Auto-Validate class               $validate->rule ($info [' rule ']);//  set rules               $validate->message ($info [' msg ']);/  Setup Information         } else {              $name  = is_string ($info)  ?  $info  : $ this->name;//single field   validation,            if  ( Strpos ($name,  '. '))  {                list ($ name,  $scene)  = explode ('. ',  $name);             }             $validate  =  loader::validate ($name);            if  (!empty ($scene))  {                  $validate->scene ($scene);//  Special handling usage             }        }         if  (! $validate->check ($data))  {//  data validation   results               $this->error =  $validate->geterror ();//  failed to pass the validation error message             if  ($this->failexception)  {//  throws an exception if it is allowed to throw an exception                  throw new validateexception ($this->error);             } else {                 return false;             }        }          $this->validate = null;//  emptying   validation rules     }    return true;} /** *  error message for return model  *  @access  public *  @return  string */public  Function geterror ()//  returns error message {    return  $this->error;} /** *  Registration callback method  *  @access  public *  @param  string          $event   Event name  *  @param  callable      $ callback  callback method  *  @param  bool          $ override  whether to cover  *  @return  void */public static function event ($event,   $callback,  $override  = false) {     $class  = get_called_class ();//  Get callback Class     if  ($override)  {         self:: $event [$class] [$event] =&NBSp [];    }    self:: $event [$class] [$event][] =  $callback;//   Event callback method}/** *  trigger event  *  @access  protected *  @param  string      $event   Event name  *  @param  mixed      $params   Incoming parameters (references)  *  @return  bool */protected function trigger ($event, & $params)/ /  triggers the corresponding event {    if  (Isset (self:: $event [$this->class][$event])  {//  Gets the   time   function         foreach  (self:: $event [$this- class][$event] as  $callback)  {             if  (Is_callable ($callback))  {//  can call                   $result  = call_user_func_array ($callback,  [ &  $params]);//  Call result                 if   (false ===  $result)  {                     return false;//  if one of them terminates the thing, stop the execution                  }             }        }     }    return true;} /** *  writing data  *  @access  public *  @param  array       $data   Data arrays  *  @return   $this  */public static function create ($data  = []) {     $model  = new static ();     $model- >isupdate (False)->save ($data,  []);//  data writes, writes by model ,It's still very powerful.     return  $model;} /** *  Update Data  *  @access  public *  @param  array       $data   Data Arrays  *  @param  array      $where   Update conditions  *  @return   $this  */public static function update ($data  = [],  $where   = []) {     $model   = new static ();     $result  =  $model->isupdate (True)->save ($data,  $where);//  data   conditions      return  $model;} /** *  find a single record  *  @access  public *  @param  mixed          $data    primary key values or query criteria (closures)  *  @param  array|string  $with     Associate pre-query  *  @param  bool          $cache   whether to cache  *  @return  static *  @throws  eXception\dbexception */public static function get ($data  = null,  $with   = [],  $cache  = false) {     $query  = static::p arsequery ($data,   $with,  $cache);    return  $query->find ($data);//single record}/** *  Find all records  *  @access  public *  @param  mixed          $data    primary key list or query criteria (closures)  *  @param  array|string  $with    correlated pre-query  *   @param  bool          $cache   Cache  * @ return static[]|false *  @throws  exception\DbException */public static  Function all ($data  = null,  $with  = [],  $cache  = false) {      $query  = static::p arsequery ($data,  $with,  $cache);     return  $query->sElect ($data);//  more than one data} 


This article is from the "Focus on PHP Group number: 414194301" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1881007

[Li Jingshan php] every day tp5-20170112|thinkphp5-model.php-5

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.