当前位置: 首页>>代码示例>>PHP>>正文


PHP AssetFileModel::hasErrors方法代码示例

本文整理汇总了PHP中AssetFileModel::hasErrors方法的典型用法代码示例。如果您正苦于以下问题:PHP AssetFileModel::hasErrors方法的具体用法?PHP AssetFileModel::hasErrors怎么用?PHP AssetFileModel::hasErrors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在AssetFileModel的用法示例。


在下文中一共展示了AssetFileModel::hasErrors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: storeFile

 /**
  * Saves the record for an asset.
  *
  * @param AssetFileModel $file
  *
  * @throws \Exception
  * @return bool
  */
 public function storeFile(AssetFileModel $file)
 {
     $isNewFile = !$file->id;
     if (!$isNewFile) {
         $fileRecord = AssetFileRecord::model()->findById($file->id);
         if (!$fileRecord) {
             throw new Exception(Craft::t("No asset exists with the ID “{id}”", array('id' => $file->id)));
         }
     } else {
         $fileRecord = new AssetFileRecord();
     }
     $fileRecord->sourceId = $file->sourceId;
     $fileRecord->folderId = $file->folderId;
     $fileRecord->filename = $file->filename;
     $fileRecord->kind = $file->kind;
     $fileRecord->size = $file->size;
     $fileRecord->width = $file->width;
     $fileRecord->height = $file->height;
     $fileRecord->dateModified = $file->dateModified;
     $fileRecord->validate();
     $file->addErrors($fileRecord->getErrors());
     if ($file->hasErrors()) {
         return false;
     }
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     try {
         if ($isNewFile && !$file->getContent()->title) {
             // Give it a default title based on the file name
             $file->getContent()->title = str_replace('_', ' ', IOHelper::getFileName($file->filename, false));
         }
         // Fire an 'onBeforeSaveAsset' event
         $this->onBeforeSaveAsset(new Event($this, array('asset' => $file, 'isNewAsset' => $isNewFile)));
         // Save the element
         if (craft()->elements->saveElement($file, false)) {
             // Now that we have an element ID, save it on the other stuff
             if ($isNewFile) {
                 $fileRecord->id = $file->id;
             }
             // Save the file row
             $fileRecord->save(false);
             if ($transaction !== null) {
                 $transaction->commit();
             }
         } else {
             return false;
         }
     } catch (\Exception $e) {
         if ($transaction !== null) {
             $transaction->rollback();
         }
         throw $e;
     }
     // If we've made it here, everything has been successful so far.
     // Fire an 'onSaveAsset' event
     $this->onSaveAsset(new Event($this, array('asset' => $file)));
     if ($this->hasEventHandler('onSaveFileContent')) {
         // Fire an 'onSaveFileContent' event (deprecated)
         $this->onSaveFileContent(new Event($this, array('file' => $file)));
     }
     return true;
 }
开发者ID:kant312,项目名称:sop,代码行数:69,代码来源:AssetsService.php

示例2: storeFile

 /**
  * Saves the record for an asset.
  *
  * @param AssetFileModel $file
  *
  * @throws \Exception
  * @return bool
  */
 public function storeFile(AssetFileModel $file)
 {
     $isNewFile = !$file->id;
     if (!$isNewFile) {
         $fileRecord = AssetFileRecord::model()->findById($file->id);
         if (!$fileRecord) {
             throw new Exception(Craft::t("No asset exists with the ID “{id}”.", array('id' => $file->id)));
         }
     } else {
         $fileRecord = new AssetFileRecord();
     }
     $fileRecord->sourceId = $file->sourceId;
     $fileRecord->folderId = $file->folderId;
     $fileRecord->filename = $file->filename;
     $fileRecord->kind = $file->kind;
     $fileRecord->size = $file->size;
     $fileRecord->width = $file->width;
     $fileRecord->height = $file->height;
     $fileRecord->dateModified = $file->dateModified;
     $fileRecord->validate();
     $file->addErrors($fileRecord->getErrors());
     if ($file->hasErrors()) {
         return false;
     }
     if ($isNewFile && !$file->getContent()->title) {
         // Give it a default title based on the file name
         $file->getContent()->title = str_replace('_', ' ', IOHelper::getFileName($file->filename, false));
     }
     $transaction = craft()->db->getCurrentTransaction() === null ? craft()->db->beginTransaction() : null;
     try {
         // Fire an 'onBeforeSaveAsset' event
         $event = new Event($this, array('asset' => $file, 'isNewAsset' => $isNewFile));
         $this->onBeforeSaveAsset($event);
         // Is the event giving us the go-ahead?
         if ($event->performAction) {
             // Save the element
             $success = craft()->elements->saveElement($file, false);
             // If it didn't work, rollback the transaction in case something changed in onBeforeSaveAsset
             if (!$success) {
                 if ($transaction !== null) {
                     $transaction->rollback();
                 }
                 return false;
             }
             // Now that we have an element ID, save it on the other stuff
             if ($isNewFile) {
                 $fileRecord->id = $file->id;
             }
             // Save the file row
             $fileRecord->save(false);
         } else {
             $success = false;
         }
         // Commit the transaction regardless of whether we saved the asset, in case something changed
         // in onBeforeSaveAsset
         if ($transaction !== null) {
             $transaction->commit();
         }
     } catch (\Exception $e) {
         if ($transaction !== null) {
             $transaction->rollback();
         }
         throw $e;
     }
     if ($success) {
         // Fire an 'onSaveAsset' event
         $this->onSaveAsset(new Event($this, array('asset' => $file, 'isNewAsset' => $isNewFile)));
         if ($this->hasEventHandler('onSaveFileContent')) {
             // Fire an 'onSaveFileContent' event (deprecated)
             $this->onSaveFileContent(new Event($this, array('file' => $file)));
         }
     }
     return $success;
 }
开发者ID:JulesVan,项目名称:solutions-con,代码行数:82,代码来源:AssetsService.php


注:本文中的AssetFileModel::hasErrors方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。