CakePHP节省外键问题 [英] CakePHP saving Foreign Key Issue

查看:96
本文介绍了CakePHP节省外键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在另一个名为Basics的表中保存我的用户表的外键。我试图向用户提出一系列问题,每个系列完成后,问题和用户ID都应保存到相应的表中。

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.

在这里是什么保存我的数据后验证发生在模型中。验证成功,因为所有的数据被保存,但外键保存为0。

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' => ''
    )
);

这应该声明外键,对吧?我的外键的列名称为'user_id'。数据类型都是INT。在数据库端是否还有其他错误?提前感谢!

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!

更新:所以 - 我现在隐藏我的用户ID在一个隐藏的字段。该字段将在我创建的任何其他字段(即使具有与INT(11)相同的数据类型)中输入到数据库中,但不会保存到名为user_id的字段中。例如,我可以将用户ID保存到名为my_id的字段,但仍然没有运气与user_id。我认为这排除了数据库问题,并且与我如何关联模型有关。

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.

推荐答案

应该像:

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 选项将帮助您保存关联的记录。它还会将user_id自动保存到您的基本表中。

'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.

这篇关于CakePHP节省外键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆