CMS*_*ing 1 plugins controller cakephp model
我目前正在尝试使用位于此处的Miles J插件http://milesj.me/code/cakephp/uploader虽然我在学习CakePhp方面取得了很大进展,但我目前在使用该插件时遇到了问题,我将不胜感激.
我已按照所有必要步骤使用该插件.它已被下载到插件文件夹,我用CakePlugin :: loadAll()引导.
到现在为止还挺好.
接下来,我按照插件开发人员的说明继续设置表.
好的,现在回到我自己的代码.我有以下设置:
images_controller.php,image.php及其视图.
我现在的目标是在这些文件中使用插件:
App::import('Vendor', 'Uploader.Uploader');
Class ImagesController extends AppController {
var $components = array('Auth');
var $helpers = array('Design');
var $uses = array('Image', 'Uploader.Upload');
function manage(){
//here I show a simple upload form that uses the action saveimage
}
function saveimage(){
$this->Uploader = new Uploader();
if(!empty($this->data)){
$this->Upload->save($this->data);
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我的模型设置如下
Class Image extends AppModel {
public $useTable = 'uploads';
public $actsAs = array(
'Uploader.FileValidation' => array(
'file' => array(
'extension' => array(
'value' => array('gif', 'jpg', 'jpeg'),
'error' => 'Only gif, jpg and jpeg images are allowed!'
),
'minWidth' => 500,
'minHeight' => 500,
'required' => true
),
'import' => array(
'required' => false
)
),
'Uploader.Attachment' => array(
'file' => array(
'name' => 'uploaderFilename',
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path',
'maxNameLength' => 30,
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
// Save additional images in the databases after transforming
array(
'method' => 'resize',
'width' => 100,
'height' => 100,
'dbColumn' => 'path_alt'
)
),
'metaColumns' => array(
'size' => 'filesize', // The size value will be saved to the filesize column
'type' => 'type' // And the same for the mimetype
)
),
'import' => array(
'uploadDir' => '/files/uploads/',
'name' => 'uploaderFilename',
'dbColumn' => 'path',
'overwrite' => true,
'stopSave' => false,
'transforms' => array(
array(
'method' => 'scale',
'percent' => .5,
'dbColumn' => 'path' // Overwrite the original image
)
)
)
)
);
}
}
Run Code Online (Sandbox Code Playgroud)
在我用于测试目的的模型上,我没有改变任何东西,只是复制并粘贴了插件内的Test/Model文件夹中显示的相同数组,这是为了显示插件的功能.
发生以下混淆,错误或缺乏理解:
我的文件没有上传到webroot/files/uploads文件夹我的数据正在数据库中插入,但是没有完整的方式留空,如下所示:
id | caption | path | path_alt | created |
4 | | | |2012:02:..|
Run Code Online (Sandbox Code Playgroud)
在上面,我希望保存路径,但事实并非如此.
我不得不承认我的困惑主要来自于我使用插件的经验不足,所以我知道我可能在模型或配置方面做错了.
任何迅速的帮助都将受到极大的赞赏,因为我试图自己解决这个问题而没有任何成功.
一些东西:
1 - 在控制器中,您不需要导入Uploader类.您也不需要使用Uploader.Upload模型(它只是一个示例测试用例).您需要在控制器中执行的操作是调用$ this-> Image-> save(),它将上传文件并将路径作为一行保存到数据库中(如果您在图像中定义了附件).
2 - 在视图中,创建文件输入.注意输入名称.
echo $this->Form->input('FILE_INPUT_NAME', array('type' => 'file'));
Run Code Online (Sandbox Code Playgroud)
3 - 在Image模型中,为该特定输入字段设置AttachmentBehavior及其选项:
'Uploader.Attachment' => array(
'FILE_INPUT_NAME' => array(
'uploadDir' => '/files/uploads/',
'dbColumn' => 'path'
),
'ANOTHER_INPUT' => array()
);
Run Code Online (Sandbox Code Playgroud)
确保图像表中存在"path"列.就是这样.
有关每个选项的功能的更多信息,请查看以下内容:http://milesj.me/code/cakephp/uploader#uploading-files-through-the-model
归档时间: |
|
查看次数: |
1944 次 |
最近记录: |