There are several issues - the call to create that Kai mentioned is one of them.
First, here's your code, but I've added comments in places where I see obvious issues (I many not have caught all of them)
<?php
if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
$id = $this->EquipmentType->getLastInsertId();
$this->loadModel('EquipmentTypesSize');
$sizesArray = $this->request->data['EquipmentType']['size'];
foreach($sizesArray as $val){
$data[] = array('EquipmentTypesSize' => array('sizes' => $val));
// WHERE IS THIS $data VARIABLE USED? It seems it's never used?
}
$this->request->data['EquipmentTypesSize']['equipment_type_id'] = $id;
$this->EquipmentTypesSize->set($this->request->data); // NO NEED TO CALL SET HERE - YOU ALREADY PASS DATA IN AS A PARAM TO SAVE
// YOU MUST CALL CREATE BEFORE ADDING A NEW RECORD
$this->EquipmentTypesSize->save($this->request->data['EquipmentTypesSize']);
// I DON'T THINK $this->request->data['EquipmentTypesSize'] HOLDS THE VALUE YOU THINK IT DOES AT THIS POINT
$this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
$this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}
Now, it's hard to tell exactly what you're wanting to do, but here's my attempt to write what you intended. If it doesn't work, it should hopefully set you on the right track.
if ($this->EquipmentType->save($this->request->data['EquipmentType'], false)) {
$id = $this->EquipmentType->getLastInsertId();
$this->loadModel('EquipmentTypesSize');
$sizesArray = $this->request->data['EquipmentType']['size'];
foreach($sizesArray as $val){
$this->EquipmentTypesSize->create();
$this->EquipmentTypesSize->save(array(
'sizes' => $val,
'equipment_type_id' => $id,
));
}
$this->Session->setFlash('Equipment type has been added successfully ', 'default', 'success');
$this->redirect(array('controller' => 'equipments', 'action' => 'listequipmenttypes', 'admin' => true));
}
Lastly, two things:
Really, you should push as much of that logic into your EquipmentType model as possible. So you might create a saveWithSizes method in your EquipmentType model that holds most of that code.
You should look into Cake's saveAll method (http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array). Ideally, you'd set your form data up so that you could just call saveAll and have Cake handle it all automatically. That may or may not be possible for your situation though.