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