My website/database is set up so that users habtm accounts, so that when a person creates an account then a user the data is saved in the accounts_users table. A person logs in (using information from the users table) and create a template, the problem is the accounts_templates table is not pulling the accountid from the accounts_users table and it is throwing a database error.
the account hambt user,
user hambt account,
account hasmany templates,
templates belongto account
there is accounts_templates which contains id, account_id, template_id
there is accounts_users which contains id, account_id, user_id
there is templates which contains id, name, description, active
there is accounts which contains id, companyname, postcode, abn
there is users which contains id, username, password
template model
<?php
class Template extends AppModel{
var $name='Template';
public $useTable = 'templates';
public $primaryKey = 'id';
public $belongsTo = array(
'Account' => array(
'classname' => 'Account',
'foreignKey' =>'account_id'
)
);
}
account template
var $hasMany = array(
'Template' =>
array(
'className'=>'Template',
'foreignKey'=>'account_id',
)
);
add function from the template controller
function add(){
if($this->request->is('post')){
$this->Template->create();
if ($this->Template->save($this->request->data))
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'Fields','action' => 'add'));
}
else { $this->Session->setFlash('The template could not be saved. Please, try again.'); }
}
}