前提・実現したいこと
よろしくお願いします。
ただ今cakephp2を勉強しており、
公式にあるブログチュートリアルをベースに
upload pluginを追加して画像の投稿を
をできるようにしようとしているところです。
発生している問題・エラーメッセージ
upload pluginの導入方法について以下の
サイトを参考に進めておりました。
http://app-review.poox.xyz/archives/1280
このサイト内にある、新規でテーブルを追加する場合というのを
参考に新規でAttachmentテーブルを作成してやってみましたが、
画像の名前自体はMysqlに登録されるのですが、肝心のimgファイルが
dirの中に入らず詰まっております
その後githubにあるupload pluginの製作者が作ったドキュメント
https://cakephp-upload.readthedocs.io/en/2.x/polymorphic.html
を参考に再度、新規テーブルを追加するやり方でやったのですが、
同じようにwebrootに入らない状態です。
以下ソースコードです。
Model/Attachment.php
<?php App::uses('AppModel', 'Model'); class Attachment extends AppModel { public $actsAs = array( 'Upload.Upload' => array( 'attachment' => array( 'thumbnailSizes' => array( 'xvga' => '1024x768', 'vga' => '640x480', 'thumb' => '80x80', ), ), ), ); public $belongsTo = array( 'Post' => array( 'className' => 'Post', 'foreignKey' => 'foreign_key', ), 'Message' => array( 'className' => 'Message', 'foreignKey' => 'foreign_key', ), ); } ?>
Model/Message.php
<?php App::uses('AppModel', 'Model'); class Message extends AppModel { public $hasMany = array( 'Video' => array( 'className' => 'Attachment', 'foreignKey' => 'foreign_key', 'conditions' => array( 'Video.model' => 'Message', ), ), ); } ?>
Model/Post.php
<?php App::uses('AppModel', 'Model'); class Post extends AppModel { public $actsAs = array('Search.Searchable',); public $filterArgs = array( // 例 'tag_id' => array( 'type' => 'value' ), 'category_id' => array( 'type' => 'value' ), 'title' => array( 'type' => 'like' ), ); public $validate = array( 'user_id' => array( 'numeric' => array( 'rule' => array('numeric'), ), ), 'title' => array( 'notBlank' => array( 'rule' => array('notBlank'), ), ), ); public $belongsTo = array( 'User' => array( 'className' => 'User', 'foreignKey' => 'user_id', 'conditions' => '', 'fields' => '', 'order' => '' ), 'Category'=>array( 'className' =>'Category', 'foreignKey' =>'category_id' ) ); public $hasMany = array( 'Image' => array( 'className' => 'Attachment', 'foreignKey' => 'foreign_key', 'conditions' => array( 'Image.model' => 'Post', ), ), ); 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'] = 'Post'; // Unset the foreign_key if the user tries to specify it if (isset($image['foreign_key'])) { unset($image['foreign_key']); } $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")); } }
View/Posts/add.ctp
<div class="posts form"> <?php echo $this->Form->create('Post'); ?> <fieldset> <legend><?php echo __('Add Post'); ?></legend> <?php echo $this->Form->input('user_id'); echo $this->Form->input('category_id', array( 'type' =>'select' ) ); echo $this->Form->input('tag_id', array( 'type' =>'select' ) ); echo $this->Form->input('title'); echo $this->Form->input('body'); echo $this->Form->create('Post', array('type' => 'file')); echo $this->Form->input('Image.0.attachment', array('type' => 'file', 'label' => 'Image')); echo $this->Form->input('Image.0.model', array('type' => 'hidden', 'value' => 'Post')); echo $this->Form->end(__('投稿')); ?> </fieldset> </div> <div class="actions"> <h3><?php echo __('Actions'); ?></h3> <ul> <li><?php echo $this->Html->link(__('List Posts'), array('action' => 'index')); ?></li> <li><?php echo $this->Html->link(__('List Users'), array('controller' => 'users', 'action' => 'index')); ?> </li> <li><?php echo $this->Html->link(__('New User'), array('controller' => 'users', 'action' => 'add')); ?> </li> </ul> </div>
画像を保存する際のディレクトリはapp/webroot/files/model/fieldsとの事らしいので
filesの中にattachmentファイルを作成してその中にattachmentファイルを作成しています。
パーミッションの変更はしており、upload pluginのverも2系の物を使用しております。
できることは色々やったつもりですが、いまいち原因がわからない状態です。
すいませんがどこが問題かご教授ください。
回答1件
あなたの回答
tips
プレビュー