与同一模型CakePHP的多重关系

时间:2012-08-07 11:29:44

标签: mysql cakephp associations relationships

嘿,我们的数据库中有三个表,这两个表通过帐户和发票这两种关系连接。

帐户(id ....) 发票(id,sender_id,receiver_id) 关系(id,sender_id,receiver_id)

发件人和收件人都是引用帐户表的外键,所以在cakePHP中是一个account_id。关系表指定可以发送或接收发票的关系,发票表显示已发送的发票。

我们如何将这些外键与CakePHP中的帐户相关联?

蛋糕发现存在关系并列出可用于发送发票的接收器。目前它将正确的sender_id发送到数据库,但将relationship_id作为invoice_id发送到数据库,作为发票表中的receiver_id。

已经完成了但不起作用:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

以下是我们对两个模型的看法:

帐户模型:

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',      
            )

        );



    }

发票模型:

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';

发票控制器:

 $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); 

添加视图:

<?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 个答案:

答案 0 :(得分:2)

我认为您不需要发票,发件人和收件人的联接表。您可以将这些外键存储在发票表中。那么你的关系就是:

<?php
class Invoice extends AppModel {

    public $belongsTo = array(
        'Sender' => array(
            'className' => 'Account',
            'foreignKey' => 'sender_id'
        ),
        'Receiver' => array(
            'className' => 'Account',
            'foreignKey' => 'receiver_id'
        )
    );
}

如果您需要区分已发送或未发送的发票,您还可以添加名为status_id或类似的列,并将另一个外键存储到具有ID的新statuses表中列和名称列以及以下示例数据:

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

您可能需要的任何其他状态。

相关问题