示例#1
0
文件: Mapper.php 项目: tempbottle/owl
 /**
  * 删除Data
  *
  * @param Data $data
  * @return boolean
  */
 public function destroy(Data $data)
 {
     if ($this->isReadonly()) {
         throw new \RuntimeException($this->class . ' is readonly');
     }
     if ($data->isFresh()) {
         return true;
     }
     $this->__before('delete', $data);
     if (!$this->doDelete($data)) {
         throw new \Exception($this->class . ' destroy failed');
     }
     $this->__after('delete', $data);
     Registry::getInstance()->remove($this->class, $data->id());
     return true;
 }
示例#2
0
 /**
  * Data属性值有效性检查
  *
  * @param Data $data
  * @return boolean
  * @throws \UnexpectedValueException 不允许为空的属性没有被赋值
  */
 protected function validateData(Data $data)
 {
     $is_fresh = $data->isFresh();
     $attributes = $this->getAttributes();
     if ($is_fresh) {
         $record = $this->unpack($data);
         $keys = array_keys($attributes);
     } else {
         $record = $this->unpack($data, ['dirty' => true]);
         $keys = array_keys($record);
     }
     foreach ($keys as $key) {
         $attribute = $attributes[$key];
         do {
             if ($attribute['allow_null']) {
                 break;
             }
             if ($attribute['auto_generate'] && $is_fresh) {
                 break;
             }
             if (isset($record[$key])) {
                 break;
             }
             throw new \UnexpectedValueException($this->class . ' property ' . $key . ' not allow null');
         } while (false);
     }
     return true;
 }