Reputation: 129
How can I make entries in my uploads table, this code is not working
this is my project array:
[Project] => Array ( [name] => Testproject [description] => TestDescription [Upload] => Array ( [0] => Array ( [name] => testlink1 [Type] => link )
[1] => Array
(
[name] => testlink2
[Type] => link
)
[2] => Array
(
[name] => testlink3
[Type] => link
)
)
)
I've written
foreach($this->data['Project']['Upload'] as $key=>$value)
{
$this->data['Upload'][$key]['project_id'] = $this->Project->id;
$this->data['Upload'][$key]['name'] = $value['name'];
$this->data['Upload'][$key]['type'] = $value['Type'];
$this->data['Upload'][$key]['created'] = date("Y-m-d H:i:s");
$this->data['Upload'][$key]['modified'] = date("Y-m-d H:i:s");
$this->Project->Upload->save($this->data['Upload'][$key]);
}
Upvotes: 0
Views: 1092
Reputation: 5933
You don't need the foreach. Just make shure, you have your associations right:
/app/models/project.php
var $hasMany = array('Upload');
/app/models/upload.php
var $belongsTo = array('Project');
If you then have your data-array like said above you can just use $this->Project->saveAll($this->data)
for saving everything at once.
Have a look into the manual to get more information on saveAll and how to to save related data: Saving related model data
Upvotes: 1