Multiple relations to the same model CakePHP

空扰寡人 提交于 2019-11-30 15:49:28

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!