Hello, I'm trying to build an invitation system with CakePHP. I have Invitation model which hasMany People, and Person hasOne and belongsTo Invitation. I believe the relationships are working, since my Invitation index view can properly access and display its associated People through the Invitation model. But heres what the relationship rules look like:
class Invitation extends AppModel {
var $name = 'Invitation';
var $hasMany = array(
'People' => array(
'className' => 'Person',
'foreignKey' => 'invitation_id',
'dependent'=> true
)
);
...
}
class Person extends AppModel {
var $name = 'Person';
var $hasOne = 'Invitation';
var $belongsTo = 'Invitation';
...
}
UPDATED MODEL RELATIONSHIPS:
class Invitation extends AppModel {
var $name = 'Invitation';
var $hasMany = array('Person');
...
}
class Person extends AppModel {
var $name = 'Person';
var $belongsTo = array('Invitation');
...
}
In my Invitation add function, however, I am having trouble saving the data for new People. I want to simultaneously add one invitation and two people.
Heres my add function:
function add() {
$this->autoRender = false;
if($this->RequestHandler->isAjax()) {
$this->layout = 'ajax';
if(!empty($this->data)) {
$this->Invitation->create();
if($this->Invitation->saveAll($this->data)) {
//debug($this->data);
$this->Session->setFlash('Saved');
$this->redirect('invitations/add_invitations');
} else {
echo('Didnt save<br />');
}
}
}
}
Here is the output of my debug($this->data):
Array
(
[Invitation] => Array
(
[code] => 001
[password] => 85c8a3735499bf91d25e5960ab4ed9deeb0b457e
[type] => 1
[num_people] => 2
)
[Person] => Array
(
[0] => Array
(
[fname] => Jane
[lname] => Doe
)
[1] => Array
(
[fname] => John
[lname] => Doe
)
)
)
I don't get any errors. the Invitation data is saved properly, but the people are not.
UPDATE: I finally got this working with the above updated model relationships. Obviously the hasOne & belongsTo rules in the Person model were conflicting. After that I made a mistake (an embarrassing one) that was preventing saving to related the related Person model:
In the Invitation model, I hade $hasMany = array('People'); when it should have been $hasMany = array('Person').
I also had to change the configuration of my database to use InnoDB as the default, so that was certainly a necessary fix.