【问题标题】:Problems saving title/description with cakephp2.0 file upload plug in使用 cakephp 2.0 文件上传插件保存标题/描述的问题
【发布时间】:2012-02-23 03:41:31
【问题描述】:

我正在使用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->data) 的样子

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

我该如何解决这个问题?

【问题讨论】:

    标签: php cakephp-2.0 image-uploading


    【解决方案1】:

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

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

    【讨论】:

    • @CodeCrack 你能用这两个表的方案和你的表格更新你的问题吗?
    • 我将如何合并它,因为它在控制器调用 $this->request->data 时已经合并,如果我 var_export($data_s); 看起来像这样上传 2 张图片时,它看起来像这样... array ( 'selectImages' => '2', 'Images' => array ( 'title' => 'Alex', 'description' => 'Test', 'tags ' => 'test', 'file1' => array ('name' => '328464_10150322152163622_584788621_8019260_905395239_o.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/tmp/phpbPMHEM', 'error ' => 0, 'size' => 369085, ), 'file2' => 数组 ('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, '大小' => 0, ), ), )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 2011-06-03
    相关资源
    最近更新 更多