多重关系到相同型号的CakePHP [英] Multiple relations to the same model CakePHP

查看:105
本文介绍了多重关系到相同型号的CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在我们的数据库中3个表内通过两个关系连接它们的帐号和发票。

帐户(ID ....)
发票(ID,SENDER_ID,receiver_id)
关系(ID,SENDER_ID,receiver_id)

发送器和接收器都引用该帐户表,以便在CakePHP的ACCOUNT_ID两个外键。关系表规定的发票可发送或接收和发票表显示已发送发票的关系。

我们如何链接都与账户CakePHP中,这些外键?

蛋糕是找到有关系,并列出了可用来发送发票给接收方。此刻其右侧SENDER_ID发送到数据库,但在该relationship_id作为发票表receiver_id发送到数据库中。

已通过这一点,但不工作了:<一href=\"http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm\" rel=\"nofollow\">http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

下面是我们为我们的两种型号:

Account模型:

 类帐户扩展AppModel {    变量$名称='账户';
    公共$ useTable =帐户;
    公共$的PrimaryKey ='ID';
    变量$ hasAndBelongsToMany =阵列(
        '用户'=&GT;
            阵列(
                '的className =&GT;用户,
                )
            );
    变量$ =的hasMany阵列(
        '模板'= GT;
            阵列(
                '的className =&GT;'模板',
                FOREIGNKEY'=&GT;'ACCOUNT_ID',
            )
            InvoiceRecieved'=&GT;阵列(
                '的className =&GT;发票,
                FOREIGNKEY'=&GT;'receiver_id',
            )
            InvoiceSent'=&GT;阵列(
                '的className =&GT;发票,
                FOREIGNKEY'=&GT;'SENDER_ID',
            )        );    }

发票模型:

 类发票扩展AppModel {
    变量$名称='发票';
    变量$ hasAndBelongsToMany =阵列(
        '现场'=&GT;
            阵列(
                '的className =&GT;'场',
                joinTable'=&GT;'fields_invoices                )
            );
    变量$ =的belongsTo阵列(
            '发件人'=&GT;阵列(
            '的className =&GT; '帐户',
            FOREIGNKEY'=&GT;'ACCOUNT_ID',
            )
            接收器=&GT;阵列(
            '的className =&GT; '帐户',
            FOREIGNKEY'=&GT;'receiver_id',
            )
        );    公共$ useTable ='发票';
    公共$的PrimaryKey ='ID';

发票控制器:

  $ accounts2 = $这个 - &GT;用户 - &GT; AccountsUser-&GT;找到('名单',阵列(
            '域'=&GT;阵列('ACCOUNT_ID'),'​​条件'=&GT;阵列(
            'USER_ID =&GT; $这个 - &GT; Auth-&gt;用户('身份证'))));    $占= $这个 - &GT;用户 - &GT;关系 - &GT;找到('名单',阵列('场'=&GT;阵列('receiver_id'),'条件'=&GT;阵列('SENDER_ID'=&GT ; $ accounts2)));如果($这个 - &GT;请求 - '是('后')){
        ($这个 - &GT; Invoice-&GT;集($这个 - &GT;请求 - &GT;数据));
        //if($this->Invoice->validates(array('fieldList'=>array('receiver_id','Invoice.relationshipExists')))){            $这个 - &GT; Invoice-&GT;保存($这个 - &GT;请求 - &GT;数据);
            $这个 - &GT;会话级&GT; setFlash(发票已保存');
      }其他{
                $这个 - &GT;会话级&GT; setFlash('发票无法保存请重试。');
             }
    //}
     $这个 - &GT;设置('账户',$账户);
     $这个 - &GT;设置('accounts2',$ accounts2);

添加视图:

 &LT; PHP
回声$这个 - &GT;&形式 - GT;创建(发票,阵列('行动'=&GT;'addinvoice'));
回声$这个 - &GT;形式 - &GT;输入('SENDER_ID',阵列('标签'=&GT;'发件人:','型'=&GT;'选择','选择'=&GT; $ accounts3));
回声$这个 - &GT;形式 - &GT;输入('receiver_id',阵列('标签'=&GT;'接收器:','型'=&GT;'选择','选择'=&GT; $账户));
这 - 回声$&GT;&形式 - GT;结束('点击这里提交发票);?&GT;


解决方案

我不认为你需要一个连接表,发票和发送者和接收者。您可以在您的发票,这些表的外键存储。那么你的关系是:

 &LT; PHP
类发票扩展AppModel {    公共$ =的belongsTo阵列(
        '发件人'=&GT;阵列(
            '的className =&GT; '帐户',
            FOREIGNKEY'=&GT; '发件人ID'
        )
        接收器=&GT;阵列(
            '的className =&GT; '帐户',
            FOREIGNKEY'=&GT; receiver_id
        )
    );
}

如果您再需要区分已发送或没有发票,你也可以添加一个名为 STATUS_ID 列或相似,且存储另一个外键到一个新的状态表中,有一个ID列和名称列,下面的示例数据:

  ID名称
== ====
1草案
2发

和任何其他状态可能需要。

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

?>

解决方案

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.

这篇关于多重关系到相同型号的CakePHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆