同一模型的多重关系 CakePHP

Multiple relations to the same model CakePHP(同一模型的多重关系 CakePHP)
本文介绍了同一模型的多重关系 CakePHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

发送者和接收者都是引用帐户表的外键,因此在 cakePHP 中是 account_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.

我们如何将这两个外键与 CakePHP 中的帐户联系起来?

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

Cake 发现存在关系并列出可向其发送发票的接收者.目前它向数据库发送了正确的sender_id,但将relationship_id 作为invoice 表中的receiver_id 发送到数据库.

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.

已经通过这个但不起作用:http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasandbelongstomany-habtm

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:

帐户模型:

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.');
             }
    /
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)