views:

44

answers:

1

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.

+1  A: 
var $hasOne = 'Invitation';
var $belongsTo = 'Invitation';

These relations to the same model should have different Aliases otherwise Cake will get confused, as I did trying to unpick it. See http://book.cakephp.org/view/1046/Multiple-relations-to-the-same-model

Leo
Interesting. After looking more closely at the types of relationships, I removed the "Person hasOne Invitation" rule, and now have just the "Person belongsTo Invitation", since there is only a single relationship here. I have tried this with no relationship rule in Invitation model, as well as "Invitation hasMany People," and both cases get the same result: Invitation data is saved, and People data is not.
Logic Artist
Also... I tried separating the model saving, and using $this->Invitation->Person->saveAll($this->data['Person']), and this gives me a error: undefined property Invitation::$Person... or something to that effect. This seems to suggest that the relationships is not properly defined, since Person is not available on Invitation.
Logic Artist
From the book on saveAll(): "atomic: If true (default), will attempt to save all records in a single transaction. Should be set to false if database/table does not support transactions. If false, we return an array similar to the $data array passed, but values are set to true/false depending on whether each record saved successfully. Also take a look at this - you may not be using a transactional engine: "http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-transactions.html
Leo
Thanks for your tips. I have followed the steps in the document you provided, and made sure my database is defaulting to InnoDB. However, the same problem remains. I'm not quite sure what relationship rules I should be using... is the "Invitation hasMany People" sufficient? Do I also need the "Person belongsTo Invitation"?
Logic Artist
I understood from your original question that you had the Person belongsTo Invitation relationship. I would certainly put that in although I'm not sure it'll help here. Can you append to your original Q the sql for the two tables and the current Model associations that you have?
Leo
Thanks for your help, I figured out why it wasn't working. See above for the update.
Logic Artist