Cakephp problem with Save belongsTo
I got problem when save belongto
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'));
}
when click save
Array
(
[Script] => Array
(
[name] => fdfvvds
[description] => dfvdfvdfvdf
[tags] =>
)
)
Array
(
[0] => performance
[1] => cleanup
)
question in
$this->data['Script']['script_catagories_id'] = $this->Script->scriptCategory->id;
I need to s开发者_如何学Cave categoryid to script table but I don't know to get it
thank youvar $belongsTo = array( 'ScriptCatagories' => array( 'className' => 'ScriptCatagories', 'foreignKey' => 'script_catagories_id',
first, it doesn't have close parentheses. And it should be singular ScriptCategory, also the foreign key script_category_id (just to follow the convention). And you misspelled category (you have $this->Script->scriptCategory->id;
later in the code)
精彩评论