Alright I am trying my first saveAll (or saveMany or saveAssociated not sure which is "correct") that includes a hasMany relationship and I cannot seem to figure out how cake wants this data. I can get the name field on the main table to update but I cannot get the hasMany relationship to update.
I am just looking for a bump in the right direction, im desperate to learn but the thought of using my head to put a hole in the wall is looking more and more tempting.
Just a little background I have two tables a "vendors" table and a "vendor_payment_types" table. the "vendors" table hasMany "vendor_payment_types. Structure below:
vendors table id, company_id, name
vendor_payment_types id, vendor_id, type
currently the array I am sending to the controller is (if a payment type has an id that means it already exists in the database and nothing needs to happen, if there is no id it needs to be added, the third part is that if it exists in the database but not in this array it needs to be deleted from the database):
Array
(
[0] => Array
(
[Vendor] => Array
(
[id] => 7
[company_id] => 2
[name] => Test Vendor
[VendorPaymentTypes] => Array
(
[0] => Array
(
[vendor_id] => 7
[id] => 13
[type] => payable
)
[1] => Array
(
[vendor_id] => 7
[type] => check
)
[2] => Array
(
[vendor_id] => 7
[id] => 14
[type] => cash
)
)
)
)
What I need to happen is to a) update the name b) add any new VendorTypes c) delete any VendorTypes that are not in the POST'ed array but exist in the database. The following is what I have in my VendorController:
public function save() {
$vendors = array();
$companyid = $this->Auth->user('company_id');
$needle = 'new';
$i = 0;
// Setup array and also if id of payment_type includes "new" unset the id so cake will add a new and not try and match id and update
if ($this->request->is('post')) {
foreach ($this->request->data['Vendor'] as $vendorId => $vendor) {
$vendors[$vendorId]['Vendor']['id'] = $vendorId;
$vendors[$vendorId]['Vendor']['company_id'] = $companyid;
$vendors[$vendorId]['Vendor']['name'] = $vendor['name'];
foreach ($vendor['VendorTypes'] as $type => $val) {
if ($val != '0') {
$vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['vendor_id'] = $vendorId;
$vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['id'] = $val;
$vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['type'] = $type;
if (strstr($vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['id'], $needle)) {
unset($vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['id']);
}
$i++;
}
}
}
// Clean up array to make it an array starting at 0 and not based on vendor['id']
$cleanedarr = array();
foreach ($vendors as $vendor => $data) {
$cleanedarr[] = $data;
}
// Currently this only updates the name
if ($this->Vendor->saveAll($cleanedarr, array('deep' => TRUE) )) {
$this->Session->setFlash('Updated Vendors!');
die(pr($this->Vendor->getDataSource()->getLog()));
} else {
$this->Session->setFlash('Failed to Update');
}
}
}
the Vendor model is setup as
public $name = 'Vendor';
public $hasMany = array(
'VendorPaymentTypes' => array(
'className' => 'VendorPaymentTypes',
'foreignKey' => 'vendor_id',
'dependent' => true
)
);
i haven't even started to tackle the delete part because I cant get the new vendor_types to add. spent a good day on countless "attempts" that all of turned up nothing.
Thank you a million times for any help!