使用cakephp2.0文件上传插件保存标题/描述时出现问题

时间:2012-02-23 03:41:31

标签: php cakephp-2.0 image-uploading

我正在使用http://milesj.me/code/cakephp/uploader#configuration上传图片。我让它工作正常(在上传图像方面)但我无法在我的数据库中保存标题/描述。

所以我有Image.php模型,其中包含以下代码

<?php
class Image extends AppModel {

    var $name = 'Image';

    public $actsAs = array('Uploader.Attachment', 'Uploader.FileValidation');
    public $validate = array(
                            'title' => array( 'rule' => 'notEmpty')
                            );  

}

在我看来,我有很多字段,如

echo $this->Form->input('title');

我的ImagesController.php添加功能看起来像这样

 function add($number_of_images = 1){ 

        if (!empty($this->data)) {
            var_export($this->data);
            exit();
            $count = 1;
            foreach($this->data['Images'] as $entry){
                $file_name = "file" . $count;
                if ($data_s = $this->Uploader->upload($file_name)) {
                    $this->Image->saveAll($data_s);
                }
                $count++;
            }

            $this->Session->setFlash("Your image(s) has been saved");
            $this->redirect(array('action'=>'index'));

        }else{   
            // make sure 10 is max amount of images a user can upload
            if($number_of_images <= 10 ){
                $this->set('number_of_images', $number_of_images);
            }else{
                // set to default 1
                $this->set('number_of_images', '1');
            }
        }
    }

当我点击保存时,图片会上传,但标题/说明不会上传或验证。这就是我的var_export($ this-&gt;数据)看起来像

的样子
array ( 'selectImages' => '1', 'Images' => array ( 'title' => 'adsafdas', 'description' => 'asdfasd', 'tags' => '', 'file1' => array ( 'name' => '308462_926071922398_11704522_41424436_637322498_n.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/php7tycbu', 'error' => 0, 'size' => 81638, ), ), )

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

根据链接,$this->Uploader->upload()仅返回上传文件的数据。因此,您需要在$this->data之前将此数组与表单saveAll的其他字段合并。

但是,如果您需要在上传文件之前验证表单数据,请使用$this->Image->validates($this->request->data)