cakephp错误插件上传者Miles J.

时间:2012-08-21 21:06:47

标签: cakephp plugins file-upload cakephp-appmodel

我在cakephp 2中有一个网站,我想用Miles J.的插件Uploader上传一个文件。 但是给我这个错误: 未找到列:1054'字段列表'中的未知列'数组'

SQL查询:

UPDATE `db148147_trimalcione`.`ingredient_images` 
SET 
    `id` = 6, 
    `filename` = Array, 
    `ingredient_id` = 8, 
    `modified` = '2012-08-21 23:01:13' 
WHERE `db148147_trimalcione`.`ingredient_images`.`id` = '6' 

为什么呢? 我创建了一个表ingredient_images,其中我有列文件名。 这是我的模特:

<?php
class IngredientImage extends AppModel {
    public $name = 'IngredientImage';
    public $useTable = 'ingredient_images';

    public $actsAs = array (
        'Uploader.Attachment' => array (
            'filename' => array(
                'name'      => 'setNameAsImgId',    // Name of the function to use to format filenames
                'saveAsFilename' => true,
                // 'baseDir'    => '',              // See UploaderComponent::$baseDir
                'uploadDir' => '/files/ingredient_images/', // See UploaderComponent::$uploadDir
                'dbColumn'  => 'filename',          // The database column name to save the path to
                'defaultPath'   => 'default.png',   // Default file path if no upload present
                'maxNameLength' => 20,              // Max file name length
                'overwrite' => true,                // Overwrite file with same name if it exists - Se si effettua un transform è da usare al suo interno altrimenti c'è un override a false
                'stopSave'  => true,                // Stop the model save() if upload fails
                'allowEmpty'    => true,            // Allow an empty file upload to continue
                'transforms'    => array (
                    array('method' => 'resize', 'width' => 160, 'height' => 160, 'dbColumn' => 'filename', 'append' => false, 'overwrite' => true)
                )
            )
        ),
        'Uploader.FileValidation' => array (
            'filename' => array (
                'maxWidth'  => array (
                    'value' => 1280,
                    'error' => 'La lunghezza dell\'avatar non deve superare i 1280 pixel'
                ),
                'maxHeight' => array (
                    'value' => 1280,
                    'error' => 'L\'altezza dell\'avatar non deve superare i 1280 pixel'
                ),
                'extension' => array (
                    'value' =>  array('gif', 'jpg', 'png', 'jpeg'),
                    'error' => 'Il formato dell\'avatar deve essere una GIF, JPG o PNG'
                ),
                'filesize'  => array (
                    'value' => 5242880,
                    'error' => 'La dimensione dell\'avatar non deve superare i 500kB'
                )
            )
        )
    );

    public $belongsTo = array(
        'Ingredient' => array(
            'className'     => 'Ingredient',
            'foreignKey'    => 'ingredient_id',
            'conditions'    => '',
            'order'         => ''
        )
    );

    public function setNameAsImgId ($name, $field, $file) {
        /**
        * Format the filename a specific way before uploading and attaching.
        * 
        * @access public
        * @param string $name   - The current filename without extension
        * @param string $field  - The form field name
        * @param array $file    - The $_FILES data
        * @return string
        */
        // devo ricavare l'id dell'immagine appena creata per rinominare il file

        return $this->id;
    }
}

?>

这是我的控制者:

CakePlugin::load('Uploader');
App::import('Vendor', 'Uploader.Uploader');
//.. my action
   $this->request->data['IngredientImage']['ingredient_id'] = $this->Ingredient->id;
$this->Ingredient->IngredientImage->save($this->request->data
//..

这是我的观点:

echo $this->Form->create('Ingredient', array ('class' => 'form', 'type' => 'file')); 
echo $this->Form->input('IngredientImage.id', array ('type'=>'hidden', 'value'=> $ingredient[0]['IngredientImage']['id'],'label'=> false, 'id' => 'IngredientImage.id'));
    echo $this->Form->input('IngredientImage.filename', array('type' => 'file'));
echo $this->Form->submit('Modifica', array('id'=>'edit'));
echo $this->Form->end();

请帮帮我

2 个答案:

答案 0 :(得分:1)

使用pr($this->request->data);检查打印内容您将找到filename索引的数组。假设您在$this->request->data['IngredientImage'][filename']['actual_filename']获取文件名。然后你可以使用:

来操纵它
CakePlugin::load('Uploader');
App::import('Vendor', 'Uploader.Uploader');
//.. my action
$this->request->data['IngredientImage']['ingredient_id'] = $this->Ingredient->id;
$this->request->data['IngredientImage']['filename'] = $this->request->data['IngredientImage']['filename']['actual_filename'];
$this->Ingredient->IngredientImage->save($this->request->data)
//..

答案 1 :(得分:0)

检查文件夹'uploadDir'=&gt;的权限'/文件/ ingredient_images /' 那是我设置的问题

希望它能够解决问题