Cakephp:添加新附件而不添加新项目

时间:2013-11-05 20:23:29

标签: cakephp file-upload cakephp-2.0

我正在使用Upload Plugin 2.0 josegonzalez。 Cakephp的图像上传器非常棒。

但我遇到了问题,我可以使用插件的createWithAttachments部分。
除了一件事,一切都运作良好。当我上传新附件(照片)时,我不想创建一个新的项目(ID,名称等)。

项目有更多附件(照片),用户可以为每个项目上传总共10张照片。

来自Project.php (模型)

代码

<?php
    class Project extends AppModel {
      /* the rest of your model here */

      public function createWithAttachments($data) {
        // Sanitize your images before adding them
        $images = array();
        if (!empty($data['Image'][0])) {
          foreach ($data['Image'] as $i => $image) {
            if (is_array($data['Image'][$i])) {
              // Force setting the `model` field to this model
              $image['model'] = 'Project';

              $images[] = $image;
            }
          }
        }
        $data['Image'] = $images;

        // Try to save the data using Model::saveAll()
        $this->create();
        if ($this->saveAll($data)) {
          return true;
        }

        // Throw an exception for the controller
        throw new Exception(__("This post could not be saved. Please try again"));
      }
    } 
?>

来自ProjectsController.php (控制器)的代码

<?php
    class ProjectsController extends AppController {
      /* the rest of your controller here */
      public function cms_album() {
        if ($this->request->is('post')) {
          try {
            $this->Project->createWithAttachments($this->request->data);
            $this->Session->setFlash(__('The message has been saved'));
          } catch (Exception $e) {
            $this->Session->setFlash($e->getMessage());
          }
        }
      }
    } 
?>

每次我将10张照片添加到数据库表attachments时,它在数据库表projects中创建了一个新项目。我只是希望附件是新的,并保存我从表单部分echo $this->Form->input('Image.'.$i.'.foreign_key', array('type' => 'hidden', 'value' => ''.$this->params->pass[0].''));获得的项目中的id

我希望我能清楚地写出我的问题并且有人可以帮助我。我试了很多东西,甚至试图用AttachmentsController 完成(没有运气)

更新:(在 Anil kumar 之后)

在使用print_r

之前$data if($this->saveAll($data))
Array
(
    [Image] => Array
        (
            [0] => Array
                (
                    [model] => Project
                    [foreign_key] => 7
                    [attachment] => Array
                        (
                            [name] => DSCN4923.JPG
                            [type] => image/jpeg
                            [tmp_name] => /tmp/phpGbIKTl
                            [error] => 0
                            [size] => 141994
                        )    
                )    
            [1] => Array
                (
                    [model] => Project
                    [foreign_key] => 7
                    [attachment] => Array
                        (
                            [name] => DSCN4921.JPG
                            [type] => image/jpeg
                            [tmp_name] => /tmp/phpJBeYxk
                            [error] => 0
                            [size] => 216931
                        )    
                )    
            [2] => Array
                (
                    [model] => Project
                    [foreign_key] => 7
                    [attachment] => Array
                        (
                            [name] => DSCN3810.JPG
                            [type] => image/jpeg
                            [tmp_name] => /tmp/phpR6sflk
                            [error] => 0
                            [size] => 1304426
                        )
                )
        )
)

2 个答案:

答案 0 :(得分:2)

删除项目模型中的$this->create();

并确保$data不包含与项目$this->saveAll($data);

相关的任何数据
The "saveAll" function is just a wrapper around the "saveMany" and "saveAssociated" methods.

所以它会保存模型数据以及相关的模型数据, 最好使用$this->Image->saveMany($data)代替。{ $this->saveAll($data)

有关详情,请查看saveAll

<强>更新

只是评论这些行。

// $this->create();
// if ($this->saveAll($data)) {
//   return true;
// }

并用

替换上面的内容
if ($this->Image->saveMany($data)) {
    return true;
}

答案 1 :(得分:1)

要添加新的关联记录,您需要指定要将内容添加到的项目的ID:

<?php
    class Project extends AppModel {
      /* the rest of your model here */

      public function createWithAttachments($data) {
        $data['Image'] = $this->sanitizeImages($data);

        // Try to save the data using Model::saveAll()
        $this->create();
        if ($this->saveAll($data)) {
          return true;
        }

        // Throw an exception for the controller
        throw new Exception(__("This post could not be saved. Please try again"));
      }

      public function addAttachments($data) {
        $data['Image'] = $this->sanitizeImages($data);

        // Make sure you have and id on $this-id and Try to save the data
        if ($this->getID() && $this->Image->save($data)) {
          return true;
        }

        // Throw an exception for the controller
        throw new Exception(__("This post could not be saved. Please try again"));
      }

      private function sanitizeImages($data) {
        // Sanitize your images before adding them
        $images = array();
        if (!empty($data['Image'][0])) {
          foreach ($data['Image'] as $i => $image) {
            if (is_array($data['Image'][$i])) {
              // Force setting the `model` field to this model
              $image['model'] = 'Project';

              $images[] = $image;
            }
          }
        }

        return $images;
      }
    } 

&GT;

在你的控制器中你可以做到

 $this->Project->id = $id;
 $this->Project->addAttachments($data);

我不是100%确定代码是否可行,它只是为了让您了解需要完成的工作。

另一个解决方案是在数据中发送项目ID。

Array
(
    [Project] => Array(
        [id] => 1 //Here goes the id of the project you would like to add attachments to
    ),
    [Image] => Array
        (
            [0] => Array
                (
                    [model] => Project
                    [foreign_key] => 7
                    [attachment] => Array
                        (
                            [name] => DSCN4923.JPG
                            [type] => image/jpeg
                            [tmp_name] => /tmp/phpGbIKTl
                            [error] => 0
                            [size] => 141994
                        )    
                )    
            [1] => Array
                (
                    [model] => Project
                    [foreign_key] => 7
                    [attachment] => Array
                        (
                            [name] => DSCN4921.JPG
                            [type] => image/jpeg
                            [tmp_name] => /tmp/phpJBeYxk
                            [error] => 0
                            [size] => 216931
                        )    
                )    
            [2] => Array
                (
                    [model] => Project
                    [foreign_key] => 7
                    [attachment] => Array
                        (
                            [name] => DSCN3810.JPG
                            [type] => image/jpeg
                            [tmp_name] => /tmp/phpR6sflk
                            [error] => 0
                            [size] => 1304426
                        )
                )
        )
)