Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8340237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:07:28+00:00 2026-06-09T05:07:28+00:00

Hey we have three tables in our database which are connected through two relationships

  • 0

Hey we have three tables in our database which are connected through two relationships which are Account and Invoices.

Accounts (id….)
Invoices (id, sender_id, receiver_id)
Relationships (id, sender_id, receiver_id)

Sender and receiver are both foreign keys which reference the account table so in cakePHP an account_id. The relationship table specifies relationships where invoices can be sent or received and the invoice table displays the invoices that have been sent.

How do we link both these foreign keys with Accounts in CakePHP?

Cake is finding there is a relationship and listing the receivers available to send an invoice to. At the moment its sending the right sender_id to the database but the sending the relationship_id to the database as the receiver_id in the invoice table.

Already gone through this but doesnt work: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

Here is what we have for our two models:

Account model:

class Account extends AppModel{

    var $name='Account';
    public $useTable = 'accounts';
    public $primaryKey = 'id';
    var $hasAndBelongsToMany = array(
        'User' =>
            array(
                'className'=>'User',
                )
            );
    var $hasMany = array(
        'Template' =>
            array(
                'className'=>'Template',
                'foreignKey'=>'account_id',     
            ),  
            'InvoiceRecieved' => array(
                'className'=>'Invoice',
                'foreignKey'=>'receiver_id',        
            ),
            'InvoiceSent' => array(
                'className'=>'Invoice',
                'foreignKey'=>'sender_id',      
            )

        );



    }

Invoice model:

class Invoice extends AppModel{ 
    var $name='Invoice'; 
    var $hasAndBelongsToMany = array(
        'Field' =>
            array(
                'className'=>'Field',
                'joinTable'=>'fields_invoices'

                )
            );
    var $belongsTo = array(
            'Sender' => array(
            'className' => 'Account',
            'foreignKey' =>'account_id',
            ),
            'Receiver' => array(
            'className' => 'Account',
            'foreignKey' =>'receiver_id',
            )
        ); 

    public $useTable='invoices';
    public $primaryKey='id';

Invoice Controller:

 $accounts2=$this->User->AccountsUser->find('list', array(
            'fields'=>array('account_id'),'conditions' => array(
            'user_id' => $this->Auth->user('id'))));

    $accounts=$this->User->Relationship->find('list', array('fields'=>array('receiver_id'),'conditions' =>  array('sender_id' => $accounts2)));

if($this->request->is('post')){
        ($this->Invoice->set($this->request->data));
        //if($this->Invoice->validates(array('fieldList'=>array('receiver_id','Invoice.relationshipExists')))){

            $this->Invoice->save($this->request->data); 
            $this->Session->setFlash('The invoice has been saved');  
      }else { 
                $this->Session->setFlash('The invoice could not be saved. Please, try again.');
             }
    //}
     $this->set('accounts', $accounts); 
     $this->set('accounts2', $accounts2); 

Add view:

<?php
echo $this->Form->create('Invoice', array('action'=>'addinvoice'));
echo $this->Form->input('sender_id',array('label'=>'Sender: ', 'type' => 'select', 'options' => $accounts3));
echo $this->Form->input('receiver_id',array('label'=>'Receiver: ', 'type' => 'select', 'options' => $accounts));
echo $this->Form->end('Click here to submit Invoice');

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T05:07:30+00:00Added an answer on June 9, 2026 at 5:07 am

    I don’t think you need a join table for invoices, and senders and receivers. You can store these foreign keys in your invoices table. Your relationships would then be:

    <?php
    class Invoice extends AppModel {
    
        public $belongsTo = array(
            'Sender' => array(
                'className' => 'Account',
                'foreignKey' => 'sender_id'
            ),
            'Receiver' => array(
                'className' => 'Account',
                'foreignKey' => 'receiver_id'
            )
        );
    }
    

    If you then need to distinguish invoices that have been sent or not, you could also add a column called status_id or similar, and store another foreign key to a new statuses table, with an ID column and name column, and the following sample data:

    id name
    == ====
    1  Draft
    2  Sent
    

    And any other statuses you may need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey I have a problem comparing the value of a CGPoint (struct with two
I have a server with a vendor application which is heavily database-reliant. I need
Hey i need to make a count on a few tables where i have
Hey I have 3 tables called STUDENT, COURSE and ENROLLMENT. STUDENT has a PK
Hey there I have 2 tables with each multiple columns and i want to
Hey guys, I have a client who wants me to update a site, which
hey all I have tables with millions of rows in them and some of
Hey i have an EmployeeStore which i have used a hashmap for this. The
Hey I have the following situation: a WinService which is processing some stuff from
Hey all, have been working on designing a new database for work. They have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.