CMS scripting
CMS scripting

Reputation: 669

Cakephp Model Association Not kicking in

   //index.ctp, this forms points to action updateData in profilesController
   $this->Form->input('User.lastname');
   $this->Form->input('Profile.age');
   $this->Form->input('Profile.height');
   $this->Form->input('Associate.city');
   $this->Form->end('Submit');

//user.php 
Class User extends AppModel {
      var $hasOne = array('Profile', 'Associate'};
      var $primaryKey = 'user_id';

}

//profile.php
Class Profile extends AppModel {
   var $belongsTo = array('User');
   var $hasOne = 'Associate';
   var $primaryKey = 'user_id';
}

//associate.php
Class Associate extends AppModel {
   var $belongsTo = array('User');
   var $primaryKey = 'user_id';
}

 //profiles_controller.php

    Class ProfilesController extends AppController{

    function updateData(){

       //output incoming request for debugging purposes
       debug($this->request->data);

       //here i fetch the db to get the id of user
       $options = 
       array('conditions' => array('User.username' => $this->Auth->user('username')),
                 'fields' => array('User.id')
               );

               //find user id so we can find user in related tables
               $id = $this->Profile->User->find('first', $options);

               //here I modify request data so cakephp finds the users through primaryKeys
               $this->request->data['Profile']['user_id'] = $id['User']['id'];
               $this->request->data['Associate']['user_id'] = $id['User']['id'];
               $this->request->data['User']['id'] = $id['User']['id'];



       if($this->request->is('post'){
       //this updates data in table no problem
       $this->Profile->save($this->request->data);
       //this updates data in table no problem either
       $this->Profile->Associate->save($this->request->data);
       //this returns false...it breaks here
       $this->Profile->User->save($this->request->data);


       }

    }

}

Table structure:

User
|id|int|auto increment
|firstname|varchar
|lastname|varchar
|date|timestamp

Profile
|id|int|autoincrement
|user_id|int
|age|int
|height|int

Associate
|id|int|autoincrement
|user_id|int
|city|varchar
|country|varchar

Ok I know what some of you might tell me, why do I do this on the profilesController and not on the UsersController. Well, my idea is to separate some actual important user data from the profile data so it's my intention to write the code for profile on the ProfilesController...as I was developing I was assuming that the same Model association would have automatically updated the User.lastname field in the User table..but that is the part where my code breaks and I have tried but I can't make it work

The current association in my mind at least is as follows:

User has one Profile User has one Associate Profile belongs to User Associate belongs to Profile and User

Can anyone tell me what am I doing wrong? i am following what I think is a logical approach for my application, cakephp updates Profile and Associate models but User remains unaffected.

Upvotes: 0

Views: 472

Answers (2)

CMS scripting
CMS scripting

Reputation: 669

As it turns out after reading the cakephp documentation (and obviously being a n00b) the reason why my code was breaking is because I had a callback beforeSave in my model. I didn't know that in order to save data I had to disable the callback which was unrelated to the part of the code I presented to you. The solution in a case like this is to do as follows:

$this->Profile->User->save($this->request->data, array('callbacks' => false));

I don't know you guys but sometimes I feel the cakephp documentation is a little too simplistic, I discover this by looking at the API.

Upvotes: 0

Dave
Dave

Reputation: 29121

Assuming the primaryKey of your users table is 'id', just remove all of the $primaryKey lines, and try again.

The only reason to set the primary key is if it doesn't follow the default that CakePHP has in place. I would GUESS (can't see your tables) that the primaryKey field in your 'users' table isn't 'user_id' - more likely it's just 'id', and in the other tables, it's 'user_id'. If that's the case, there's no need to specify the $primaryKey, since that's the default of CakePHP.

Upvotes: 1

Related Questions