示例#1
0
 /**
  * Creates a model in the db
  *
  * @return static
  */
 public function create()
 {
     $fieldNames = array_keys(static::$fields);
     $query = sprintf('INSERT INTO %s (%s) VALUES (%s)', static::__getTableName(), join(',', $fieldNames), join(',', array_map(function ($k) {
         return ':' . $k;
     }, $fieldNames)));
     $stmt = DbManager::prepare($query);
     $values = array();
     foreach (static::$fields as $k => $v) {
         $values[$k] = $this->get($k);
     }
     $stmt->execute($values);
     $this->id = DbManager::lastInsertId();
     return $this;
 }