Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Unable to upload multiple associated images #192

Closed
anuj9196 opened this issue Jun 28, 2016 · 5 comments
Closed

Unable to upload multiple associated images #192

anuj9196 opened this issue Jun 28, 2016 · 5 comments

Comments

@anuj9196
Copy link

anuj9196 commented Jun 28, 2016

I have two tables service_categories and service_category_images

CREATE TABLE `service_category_images` (
  `id` char(36) NOT NULL,
  `service_category_id` char(36) NOT NULL,
  `image` varchar(100) NOT NULL,
  `dir` varchar(100) NOT NULL,
  `created` datetime DEFAULT CURRENT_TIMESTAMP,
  `modified` datetime NOT NULL
)

The service_category_images is associated to service_categories

According to your example tutorial, this is all what I have done.

In ServiceCategoriesTable.php

    public function initialize(array $config)
    {
        parent::initialize($config);

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

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

In ServiceCategoryImagesTable.php

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'
        ]);
    }

add.ctp of ServiceCategoryImages class

<?= $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() ?>

and ServiceCategory.php entity contains

    protected $_accessible = [
        '*' => true,
        'id' => false,
        'service_category_images' => true,
    ];

I there something I'm missing or doing wrong. Because this configuration is not uploading files.

@davidyell
Copy link
Owner

My first thought is that the folder you're uploading to is not writable.

@anuj9196
Copy link
Author

The folder is writable and images are uploading fine when I upload single image. Like user avatar along with user profile information in same table.

@davidyell
Copy link
Owner

This could be a new use-case, as I have a test for uploading multiple files but these are different uploads fields, and not multiple to the same field.

If you'd like to figure out where the loop needs to go and submit a pull request against the develop branch, that would be great!

@anuj9196
Copy link
Author

I don't understand how to figure out loop. But I have submitted a pull request.

What I did to solve my problem...
I removed 'service_category_images' => true from associated entity and added 'image' => true to ServiceCategoryImage.php entity and replaced multiple occurring of 'service_category_images.0.image by a single image in add.ctp view.
Now, I'm able to upload a single image at a time.

@davidyell
Copy link
Owner

Relates to #207

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

2 participants