Example #1
0
 /**
  * 新增数据
  * @access public
  * @param mixed $data 数据
  * @param array $options 表达式
  * @param boolean $replace 是否replace
  * @return mixed
  */
 public function add($data = '', $options = array(), $replace = false)
 {
     if (empty($data)) {
         // 没有传递数据,获取当前数据对象的值
         if (!empty($this->data)) {
             $data = $this->data;
             // 重置数据
             $this->data = array();
         } else {
             $this->error = L('_DATA_TYPE_INVALID_');
             return false;
         }
     }
     // 数据处理
     $data = $this->_facade($data);
     // 分析表达式
     $options = $this->_parseOptions($options);
     if (false === $this->_before_insert($data, $options)) {
         return false;
     }
     // 写入数据到数据库
     $result = $this->db->insert($data, $options, $replace);
     if (false !== $result && is_numeric($result)) {
         $pk = $this->getPk();
         // 增加复合主键支持
         if (is_array($pk)) {
             return $result;
         }
         $insertId = $this->getLastInsID();
         if ($insertId) {
             // 自增主键返回插入ID
             $data[$pk] = $insertId;
             if (false === $this->_after_insert($data, $options)) {
                 return false;
             }
             return $insertId;
         }
         if (false === $this->_after_insert($data, $options)) {
             return false;
         }
     }
     return $result;
 }