1
votes

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.'); } 
    } 

  }
2
Could you give a clearer description of the relationships? The current one sounds like a riddle. :) - L. Sanna
just updated them to read easier - user1393064

2 Answers

0
votes

In fact there is no what you called account_template model. One account has one template and then one template has many accounts.

So, for this task you should have two models: Account and Template

Account Model

var $hasOne = array(
    'Template' =>
        array(
            'className'=>'Template',

            )
        );

Template Model

public $hasMany = array(
    'Account' => array(
        'classname' => 'Account',
        'foreignKey' =>'Template_id'
        )
    );
0
votes

I can't see your database, but judging from your question there may be several errors:

user_accounts

is wrong as the name of join table in Cakephp. It should be:

accounts_users

plurals and in alphabetical order.

If you create a accounts_templates join table, it means that account HABTM templates. You said it was a HasMany. Either it is HasMany, then no need for a join table, or it's a HABTM and you should say so in the models.

Edit: Ok. IN your question you write:

accounts_templates which contains id, accounts_id, templates_id

it should be:

id, account_id, template_id

singular.

cakephp docs on relationships