3
votes

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.

2
Are you saving questions in basics table?Arun Jain
Yes, I am. I updated my question with some more info that I just found out. Let me know if you see anything. Thanks!Evan Johnson
What it prints with pr($this->request->data);die; in your controller's method?Arun Jain
I've added the requested array of form data.Evan Johnson

2 Answers

3
votes

Please add relationship in User Model

var $hasOne = array(
'Basic' => array(
    'className' => 'Basic',
    'foreignKey' => 'user_id',
    'conditions' => '',
    'fields' => '',
    'order' => ''
   )
 );
2
votes

Your requested array should be made like:

Array
(
    [User] => Array
              (
                  //field list of User Model
               )

    [Basic] => Array
              (
                  //field list of Basic Model
               )
)

To achieve this either you can manipulate your requested array on server side or change the field name in your form like:

<?php echo $this->Form->input('User.first_name', array());?>
//and
<?php echo $this->Form->input('Basic.field_name', array());?>

Then you will be able to save associated records using:

$this->User->saveAll($this->request->data, array('deep' => true));

'deep' => true option will help you to save the associated records also. It will also save the user_id into your basics table automatically.

Don't forget to define the Model association-ships.