保存belongsTo的Cakephp问题

时间:2011-07-27 00:16:25

标签: cakephp

保存属于

时遇到问题
class Script extends AppModel {
    var $name = 'Script';
    var $belongsTo = array(
    'ScriptCatagories' => array(
        'className' => 'ScriptCatagories',
        'foreignKey' => 'script_catagories_id',

 function add_script() {
   pr($this->data);
    if (!empty($this->data)) {
        $this->Script->create();
        $this->data['Script']['script_catagories_id'] = $this->Script->scriptCategory->id;
        $this->data['Script']['user_id'] = $this->Auth->user('id');
        if ($this->Script->save($this->data)) {
            $this->Session->setFlash(__('The script has been saved', true));
             $this->redirect(array('action' => 'script_index'));
        } else {
            $this->Session->setFlash(__('The script could not be saved. Please, try again.', true));
        }
    }

     pr($scriptCatagories = $this->Script->ScriptCatagories->find('list')); 
     $this->set(compact('scriptCatagories'));
}

点击保存

Array
(
[Script] => Array
    (
        [name] => fdfvvds
        [description] => dfvdfvdfvdf
        [tags] => 
     )
   )
    Array
   (
    [0] => performance
    [1] => cleanup
    )

中的问题

        $this->data['Script']['script_catagories_id'] = $this->Script->scriptCategory->id;

我需要将categoryid保存到脚本表中,但我不知道得到它   谢谢

1 个答案:

答案 0 :(得分:0)

var $belongsTo = array(
'ScriptCatagories' => array(
    'className' => 'ScriptCatagories',
    'foreignKey' => 'script_catagories_id',

首先,它没有紧密的括号。它应该是单一的ScriptCategory,也就是外键script_category_id(只是为了遵循惯例)。你拼写错误的类别(你在代码后面有$this->Script->scriptCategory->id;