CakePHP salvando el problema de la clave externa

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.

preguntado el 24 de agosto de 12 a las 04:08

Are you saving questions in basics table? -

Yes, I am. I updated my question with some more info that I just found out. Let me know if you see anything. Thanks! -

con que imprime pr($this->request->data);die; in your controller's method? -

I've added the requested array of form data. -

2 Respuestas

Please add relationship in User Model

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

Respondido 24 ago 12, 05:08

Thanks - you are right, I needed to add the relationship to the User Model. I am still not able to enter the user id into my foreign key field though. I have two hidden inputs trying to enter data into a user_id field and my_id field. Both fields are the same data type. When I switch the foreignKey value to my_id, the problem switches from the user_id field to the my_id field. Do you think I'm associating my models incorrectly? - Evan Johnson

OP's first 3 lines are saying something else. One user can have multiple questions that would be saved into basics table. Isn't it? - arun jain

No - there are multiple questions in the basics table, but the user only saves one row. Sorry! - Evan Johnson

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.

Respondido 24 ago 12, 07:08

Awesome! Even though we were not saving any user data - we still put the id in that field? I placed it as 'id', where the primary id is stored, and it added it to the Basics table. Interesting - I would assume you would store the data in the Basics table like all other data, and just tell Cake the association for later queries. Thank you so much the help! - Evan Johnson

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.