I am having an issue saving the foreign key of my Users table in another table called Basics. I am trying to ask the user a series of questions, and each series that is completed, the questions and user ID are supposed to be saved into the corresponding table.
Here is what saves my data after validation occurs in the model. Validation occurs successfully, as all the data is saved, but the foreign key is saved as a 0.
public function pageone() {
if ($this->request->is('post')){
$this->request->data['Table1']['user_id'] = $this->Auth->user('id');
if ($this->User->saveAll($this->request->data)) {
$this->redirect(array('action'=>'pagetwo'));
} else {
$this->Session->setFlash('Your data have not been saved');
}
}
}
In my model, I declared the foreign key with the following code:
var $name = 'Table1';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
This should declare the foreign key, right? The column name for my foreign key is called 'user_id'. The data types are both INT. Could there be something else wrong on the database side? Thanks in advance!
UPDATE: So - I am hiding my user ID in a hidden field now. The field will get entered into the database in any other fields I create (even with the same data type as INT(11)), but it won't get saved into the field named 'user_id'. For example, I can save the user ID into a field named my_id, but still no luck with user_id. I think this rules out a database issue, and has to do with how I'm associating the models.
pr($this->request->data);die;
in your controller's method? – Arun Jain