Multiple relations to the same model CakePHP

前端 未结 1 2022
时光说笑
时光说笑 2021-01-03 10:07

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, rec

相关标签:
1条回答
  • 2021-01-03 10:44

    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 讨论(0)
提交回复
热议问题