2017-09-12 7 views
1

私はcakephp 3.xアプリケーションの中で簡単なアップロード機能を構築することはできません。私はcakephp-upload behaviorを使用しました。この瞬間、必要なすべてのデータをデータベースに保存できますが、イメージはアップロードされません。私はstackoverflow上のいくつかの関連記事をチェックしましたが、cakephp 2.xではなくバージョン3.xではほとんどありません。cakephp-uploadの動作で画像がアップロードされない

ビューは次のようになります。最後に

// the $array consists of this data 
    [ 
     'data||1' => 'data', 
     'photo' => [ 
      'tmp_name' => '/Applications/MAMP/tmp/php/phpHbVBQz', 
      'error' => (int) 0, 
      'name' => 'Screen Shot 2017-08-22 at 14.26.17.png', 
      'type' => 'image/png', 
      'size' => (int) 163117 
     ] 
] 

private function addContent($array) { 
     $form = $this->getHighestFormId(); 

     foreach ($array as $field => $value) { 
      if ($value != null) { 
       // create new entity 
       $containerContent = $this->StoragecontainerContent->newEntity(); 

       // explode field name and container_block_element_id 
       $explodedField = explode("||", $field); // [0] field, [1] container_block_element_id 

       // remove the automatic generated _ character from the string and replace it by a whitespace 
       // $replacedName = str_replace("_", " ", $explodedField[0]); 

       // create array 
       $data = [ 
        'user_id' => $this->Auth->user('id'), 
        'form_id' => $form->maxFormId + 1, 
        'storagecontainer_block_element_id' => $explodedField[1], 
        'identifier' => $explodedField[0], 
        'content' => $value 
       ]; 

       // patch data 
       $containerContent = $this->StoragecontainerContent->patchEntity($containerContent, $data); 

       // safe value 
       $this->StoragecontainerContent->save($containerContent); 
      } 
     } 
    } 

、私のテーブルクラスのルックス:データベースへのすべての写真関連のものを保存する

<?php echo $this->Form->create($form, ['type' => 'file', 'id' => 'form', 'class' => 'form-horizontal', 'url' => '/storages/form/content/' . $formId]); ?> 

// some other input fields 

<?php echo $this->Form->input('photo', ['type' => 'file']); ?> 
<?php echo $this->form->end(); ?> 

機能は、このようになりますこのように:

class StoragecontainerContentTable extends Table { 

    /** 
    * Initialize method 
    * 
    * @param array $config The configuration for the Table. 
    * @return void 
    */ 
    public function initialize(array $config) { 
     parent::initialize($config); 

     $this->setTable('storagecontainer_content'); 
     $this->setPrimaryKey('id'); 

     $this->addBehavior('Josegonzalez/Upload.Upload', [ 
      'photo' => [ 
       'fields' => [ 
        'dir' => 'photo_dir', 
        'size' => 'photo_size', 
        'type' => 'photo_type' 
       ], 
       'nameCallback' => function ($data, $settings) { 
        return strtolower($data['name']); 
       }, 
       'transformer' => function ($table, $entity, $data, $field, $settings) { 
        $extension = pathinfo($data['name'], PATHINFO_EXTENSION); 

        // Store the thumbnail in a temporary file 
        $tmp = tempnam(sys_get_temp_dir(), 'upload') . '.' . $extension; 

        // Use the Imagine library to DO THE THING 
        $size = new \Imagine\Image\Box(40, 40); 
        $mode = \Imagine\Image\ImageInterface::THUMBNAIL_INSET; 
        $imagine = new \Imagine\Gd\Imagine(); 

        // Save that modified file to our temp file 
        $imagine->open($data['tmp_name']) 
         ->thumbnail($size, $mode) 
         ->save($tmp); 

        // Now return the original *and* the thumbnail 
        return [ 
         $data['tmp_name'] => $data['name'], 
         $tmp => 'thumbnail-' . $data['name'], 
        ]; 
       }, 
       'deleteCallback' => function ($path, $entity, $field, $settings) { 
        // When deleting the entity, both the original and the thumbnail will be removed 
        // when keepFilesOnDelete is set to false 
        return [ 
         $path . $entity->{$field}, 
         $path . 'thumbnail-' . $entity->{$field} 
        ]; 
       }, 
       'keepFilesOnDelete' => false 
      ] 
     ]); 
    } 
} 

答えて

2

カスタムデータセットを作成して保存するのではなく、実際に保存プロセスで行う必要のある特別なことはありません。そのため、ドキュメントには含まれていません。

あなたがしなければならないことは、あなたがすでに行っているようにフォームに設定された名前を使用し、変更なしでエンティティにリクエストデータをパッチして保存し、つまり、$arrayが生のリクエストデータである場合は、patchEntity()に直接渡します。

+0

他のテーブルの中にデータを保存することは可能ですか?この例では、ユーザーテーブル内に保存されていますが、それは私が望むものではありません。 – CodeWhisperer

+0

@ CodeWhispererもちろん、それは可能です。データは、ビヘイビアが関連付けられているどのテーブルにも保存されます。 – ndm

+0

私は自分の質問を変更しました。今行動は、関連するテーブルに結合されますが、どのように写真の要素を読み込んでこれを別のテーブルに挿入できますか? – CodeWhisperer

関連する問題