【问题标题】:Cakephp Error using Miles J Uploader plugin使用 Miles J Uploader 插件的 Cakephp 错误
【发布时间】:2012-03-01 08:06:36
【问题描述】:

我目前正在尝试使用位于 http://milesj.me/code/cakephp/uploader 的 Miles J 插件虽然我在学习 CakePhp 方面取得了很大进步,但我目前在使用该插件时遇到了问题,如果提供任何帮助,我将不胜感激。

我已按照使用插件的所有必要步骤进行操作。它已经下载到 Plugin 文件夹中,我使用 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);
          }


     }



}

现在,我的模型设置如下

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
                    )
                )
            )
        )
    );


}

}

在用于测试目的的模型上,我没有更改任何内容,只是复制并粘贴了与插件内的 Test/Model 文件夹中显示的完全相同的数组,用于显示插件的功能。

正在发生以下混淆、错误或缺乏理解:

我的文件没有上传到 webroot/files/uploads 文件夹 我的数据正在插入数据库中,但不完整,如图所示:

id | caption | path | path_alt | created  |
4  |         |      |          |2012:02:..|

上面,我希望保存路径,但没有。

我不得不承认我的困惑主要来自于我没有使用插件的经验,所以我知道我可能在我的模型或配置方面做错了。

非常感谢任何及时的帮助,因为我试图自己解决这个问题,但没有成功。

【问题讨论】:

  • 当尝试使用模型保存时,我也收到此消息:错误:SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列'数组' SQL 查询:INSERT INTO uploads (path, created) VALUES (Array, '2012-03-01 03:38:14') 注意:如果要自定义此错误信息,请创建 app\View\Errors\pdo_error.ctp

标签: cakephp plugins model controller


【解决方案1】:

一些事情:

1 - 在您的控制器中,您不需要导入 Uploader 类。您也不需要使用 Uploader.Upload 模型(它只是一个示例测试用例)。在控制器中您需要做的就是调用 $this->Image->save() ,它将上传文件并将路径作为一行保存到数据库中(如果您在图像中定义了附件)。

2 - 在您看来,创建文件输入。注意输入名称。

echo $this->Form->input('FILE_INPUT_NAME', array('type' => 'file'));

3 - 在您的图像模型中,为该特定输入字段设置 AttachmentBehavior 及其选项:

'Uploader.Attachment' => array(
    'FILE_INPUT_NAME' => array(
        'uploadDir' => '/files/uploads/',
        'dbColumn' => 'path'
    ),
    'ANOTHER_INPUT' => array()
);

确保图像表中存在“路径”列。就是这样。

有关每个选项的作用的更多信息,请查看以下内容:http://milesj.me/code/cakephp/uploader#uploading-files-through-the-model

【讨论】:

  • 嘿,我已经按照你说的那样设置了所有东西......它向数据库添加了新行,但我在目标目录中找不到图像......请帮助......
猜你喜欢
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多