Yes, please find the codes as follows for the add function with images upload feature (the one you helped replied me recently)
public function add() {
$this->loadModel('Categories');
$product = $this->Products->newEntity();
$this->set(compact('product'));
$user = $this->authUser;
if ($this->request->is('post')) {
$this->loadComponent('Upload');
$data = $this->request->getData();
$data['user_id'] = $user['id'];
$product = $this->Products->patchEntity($product, $data);
$this->loadModel('ProductImages');
$productImage = $this->ProductImages->newEntity();
$productImage = $this->ProductImages
->patchEntity($productImage, $this->request->getData());
if (!empty($product->getErrors())) {
$this->Flash->error(__('Please provide all required information.'));
return;
}
if (!empty($data['image']['name'])) {
$ext = Utils::allowedImageExtensions($data['image']['name']);
if (!$ext) {
$this->Flash->error(__('Provided image is not valid'));
return;
}
if ($this->Products->save($product)) {
$id = $product->id;
$imagePath = $this->Products->imagePath;
$directory = WWW_ROOT . DS . $imagePath . DS . $id;
$dir = new Folder($directory, true, 0755); //creates target folder if not exists.
$resultImg = $this->Upload->upload(
$data['image'],
WWW_ROOT . '/' . $imagePath . '/' . $id . '/',
null,
array('type' => 'resize', 'size' => '1000', 'output' => 'png'),
null
);
}
if (!empty($this->Upload->errors)) {
unset($data['image']);
$this->Flash->error(__($this->Upload->errors));
return;
}
$data['product_image'] = $resultImg;
}
$result = $this->Products->saveData($user, $product, $data);
if (!empty($result['errors'])) {
$this->Flash->error(__('The post could not be saved. Please, try again.'));
$product->setErrors($result['errors']);
return;
}
if ($result) {
return $this->redirect(['controller' => 'ProductImages', 'action' => 'index', $product->id]);
} else {
$this->Flash->error(__('The Product could not be saved. Please, try again.'));
}
}
}
the following is the add ctp file element
<?= $this->Form->create($product, ['id' => 'exampleFullForm', 'type' => 'file']); ?>
<div class="row row-lg">
<div class="col-xl-12 form-horizontal">
<div class="form-group row">
<label class="col-xl-3 col-md-3 form-control-label">Main Product Image
<span class="required">*</span>
</label>
<div class=" col-xl-8 col-md-9">
<?php echo $this->Form->control('image', array(
'label' => false,
'div' => false, 'type' => 'file',
'class' => 'form-control',
'id' => 'input-file-now',
'data-plugin' => 'dropify',
'data-default-file' => '',
)); ?>
<?php if ($product->product_image != null) { ?>
<label class="col-xl-3 col-md-3 form-control-label">Current Product Image
<span class="required">*</span>
</label>
<?php
$imgPath = $this->Url->build('/', true) . 'file/image/products/' . $product->id . DS . 'large' . DS . $product->product_image;
echo $this->Html->link(
$this->Html->image($imgPath, ['width' => '100', 'height' => '130']),
$imgPath,
['escape' => false, 'target' => '_blank']
);
}
?>
</div>
</div>
<div class="form-group row">
<label class="col-xl-3 col-md-3 form-control-label">Product Name
<span class="required">*</span>
</label>
<div class=" col-xl-8 col-md-9">
<?php echo $this->Form->control('name', array('label' => false, 'div' => false, 'type' => 'text', 'class' => 'form-control')); ?>
</div>
</div>
</div>
<div class="form-group col-xl-12 text-right padding-top-m">
<?php echo $this->Form->button(__('Save'), array('type' => 'submit', 'class' => 'btn btn-primary center-block')); ?>
<?php
echo $this->Html->link(
__('Next'),
['controller' => 'productImages', 'action' => 'index', $product->id],
['class' => 'btn btn-primary center-block']
);
?>
</div>
</div>
<?= $this->Form->end() ?>
<?php
$baseCSS = $this->Url->build('/', true);
?>
after i clicked save, the image is saved into Products table, but not at Product Images table, they want to push image file to both tables at same time