I have Products
mapping to Images
via the join table Products_Images
Product Model:
class Product extends AppModel {
public $hasAndBelongsToMany = array(
'Image' => array(
'className' => 'Image',
'joinTable' => 'products_images',
'dependent' => true,
'foreignKey' => 'product_id',
'associationForeignKey' => 'image_id'
)
);
}
Image Model:
class Image extends AppModel {
public $hasAndBelongsToMany = array(
'Product' => array(
'className' => 'Product',
'joinTable' => 'products_images',
'foreignKey' => 'image_id',
'associationForeignKey' => 'product_id'
)
);
}
Action Snippet in Product Controller:
if($this->request->is('post')) {
if(!empty($this->request->data)) {
$this->Product->create();
if($this->Product->save($this->request->data)) {
$newProductID = $this->Product->id;
foreach($this->request->data['Uploads'] as $image):
//Check if image has been uploaded
if(!empty($image['name'])) {
$ext = substr(strtolower(strrchr($image['name'], '.')), 1); //get the extension
$arr_ext = array('jpg', 'jpeg', 'png'); //set allowed extensions
//only process if the extension is valid
if(in_array($ext, $arr_ext))
{
//do the actual uploading of the file. First arg is the tmp name, second arg is
//where we are putting it
move_uploaded_file($image['tmp_name'], './img/uploads/products/' . $image['name']);
//prepare the Image for database entry
$this->request->data['Image']['name'] = $image['name'];
//save image
$this->Product->Image->save($this->request->data);
}
}
endforeach;
$this->Session->setFlash(
'Product has been successfully created.',
'default',
array('class' => 'alert-success')
);
return $this->redirect(array('action' => 'view',$newProductID));
} else {
$this->Session->setFlash(
'Product has could not be created. Please try again.',
'default',
array('class' => 'alert-danger')
);
return $this->redirect(array('action' => 'index'));
}
}
}
This is what the action in the Products
controller receives:
ISSUE: This adds the Product into the table nicely, it also adds the Image into the table nicely however it creates 4 seamingly random entries in the Products_Images table. It gets the image_id correct, but it inserts 4 random product_ids.