0

我正在开发一个 CakePHP 3 项目并使用 Proffer 插件来上传图像。该插件适用于上传单个图像。

现在我想让上传多张图片成为可能。

我将图像存储到与外键service_category_images相关的表中。service_categoriesservice_category_id

service_category_images 表

根据 Proffer 插件指南中给出的上传多个图像。

我的ServiceCategoryImagesTable.php文件有

<?php
namespace App\Model\Table;

use App\Model\Entity\ServiceCategoryImage;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * ServiceCategoryImages Model
 *
 * @property \Cake\ORM\Association\BelongsTo $ServiceCategories
 */
class ServiceCategoryImagesTable extends Table
{

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

        $this->table('service_category_images');
        $this->displayField('id');
        $this->primaryKey('id');

        $this->addBehavior('Timestamp');
        $this->addBehavior('Proffer.Proffer', [
          'image' => [
            'root' => DS . 'var' . DS . 'www' . DS . 'html' . DS . 'flitfix' . DS . 'media' . DS . 'files',
            'dir' => 'dir'
          ]
        ]);

        $this->belongsTo('ServiceCategories', [
            'foreignKey' => 'service_category_id',
            'joinType' => 'INNER'
        ]);
    }

    public function validationDefault(Validator $validator)
    {
        $validator->provider('proffer', 'Proffer\Model\Validation\ProfferRules');

        $validator
            ->uuid('id')
            ->allowEmpty('id', 'create');

        $validator
            ->add('image', 'proffer', [
              'rule' => ['dimensions', [
                'min' => ['w' => '100', 'h' => '100'],
                'max' => ['w' => '1000', 'h' => '1000']
                ]],
                'message' => 'Please upload image of correct dimension',
                'provider' => 'proffer'
            ])
            ->requirePresence('image', 'create')
            ->allowEmpty('image');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['service_category_id'], 'ServiceCategories'));
        return $rules;
    }
}

并且ServiceCategoryImage.php实体包含

<?php
namespace App\Model\Entity;

use Cake\ORM\Entity;

/**
 * ServiceCategoryImage Entity.
 *
 * @property string $id
 * @property string $service_category_id
 * @property \App\Model\Entity\ServiceCategory $service_category
 * @property string $image_01
 * @property string $image_02
 * @property string $image_03
 * @property string $image_04
 * @property string $dir
 * @property \Cake\I18n\Time $created
 * @property \Cake\I18n\Time $modified
 */
class ServiceCategoryImage extends Entity
{
    protected $_accessible = [
        '*' => true,
        'id' => false,
        'service_category_images' => true,
    ];
}

add.ctp控制器视图ServiceCategoryImages包含

<div class="serviceCategoryImages form large-9 medium-8 columns content">
    <?= $this->Form->create($serviceCategoryImage, ['type' => 'file']) ?>
    <fieldset>
        <legend><?= __('Add Service Category Image') ?></legend>
        <?php
            echo $this->Form->input('service_category_id', ['options' => $serviceCategories]);
            echo $this->Form->input('service_category_images.0.image', ['type' => 'file']);
            echo $this->Form->input('service_category_images.1.image', ['type' => 'file']);
            echo $this->Form->input('service_category_images.2.image', ['type' => 'file']);
            echo $this->Form->input('service_category_images.3.image', ['type' => 'file']);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

ServiceCategoriesTable.phphasMany 关系为

$this->hasMany('ServiceCategoryImages', [
    'foreignKey' => 'service_category_id'
]);

但是,它不是上传文件。有什么问题吗 ?

编辑 2:error.log 内容

2016-06-28 06:57:30 Warning: Warning (2): getimagesize() [<a href='http://php.net/function.getimagesize'>function.getimagesize</a>]: Filename cannot be empty in [/var/www/html/flitfix/admin/vendor/davidyell/proffer/src/Model/Validation/ProfferRules.php, line 29]
Request URL: /service-category-images/add
Referer URL: http://admin.flitfix2.com/service-category-images/add
Client IP: 127.0.0.1
Trace:
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 146
getimagesize - [internal], line ??
Proffer\Model\Validation\ProfferRules::dimensions() - ROOT/vendor/davidyell/proffer/src/Model/Validation/ProfferRules.php, line 29
Cake\Validation\ValidationRule::process() - CORE/src/Validation/ValidationRule.php, line 138
Cake\Validation\Validator::_processRules() - CORE/src/Validation/Validator.php, line 1505
Cake\Validation\Validator::errors() - CORE/src/Validation/Validator.php, line 137
Cake\ORM\Marshaller::_validate() - CORE/src/ORM/Marshaller.php, line 203
Cake\ORM\Marshaller::merge() - CORE/src/ORM/Marshaller.php, line 488
Cake\ORM\Table::patchEntity() - CORE/src/ORM/Table.php, line 2099
App\Controller\ServiceCategoryImagesController::add() - APP/Controller/ServiceCategoryImagesController.php, line 56
Cake\Controller\Controller::invokeAction() - CORE/src/Controller/Controller.php, line 429
Cake\Routing\Dispatcher::_invoke() - CORE/src/Routing/Dispatcher.php, line 114
Cake\Routing\Dispatcher::dispatch() - CORE/src/Routing/Dispatcher.php, line 87
[main] - ROOT/webroot/index.php, line 36

debug.log 内容

2016-06-28 06:57:30 Notice: Notice (8): Undefined index: tmp_name in [/var/www/html/flitfix/admin/vendor/davidyell/proffer/src/Model/Validation/ProfferRules.php, line 29]
Request URL: /service-category-images/add
Referer URL: http://admin.flitfix2.com/service-category-images/add
Client IP: 127.0.0.1
Trace:
Cake\Error\BaseErrorHandler::handleError() - CORE/src/Error/BaseErrorHandler.php, line 146
Proffer\Model\Validation\ProfferRules::dimensions() - ROOT/vendor/davidyell/proffer/src/Model/Validation/ProfferRules.php, line 29
Cake\Validation\ValidationRule::process() - CORE/src/Validation/ValidationRule.php, line 138
Cake\Validation\Validator::_processRules() - CORE/src/Validation/Validator.php, line 1505
Cake\Validation\Validator::errors() - CORE/src/Validation/Validator.php, line 137
Cake\ORM\Marshaller::_validate() - CORE/src/ORM/Marshaller.php, line 203
Cake\ORM\Marshaller::merge() - CORE/src/ORM/Marshaller.php, line 488
Cake\ORM\Table::patchEntity() - CORE/src/ORM/Table.php, line 2099
App\Controller\ServiceCategoryImagesController::add() - APP/Controller/ServiceCategoryImagesController.php, line 56
Cake\Controller\Controller::invokeAction() - CORE/src/Controller/Controller.php, line 429
Cake\Routing\Dispatcher::_invoke() - CORE/src/Routing/Dispatcher.php, line 114
Cake\Routing\Dispatcher::dispatch() - CORE/src/Routing/Dispatcher.php, line 87
[main] - ROOT/webroot/index.php, line 36
4

0 回答 0