0

当我尝试将新发票插入数据库时​​,cakephp 抛出错误,指出在 where 子句中找不到列 relationship.partytwo。我想要实现的是确保发送发票的用户与在发送发票之前接收发票的人在关系用户数据库中具有活动关系,遗憾的是我得到的只是数据库错误。

这是我的表格的结构

invoices - id, to, biller, subject, description, datecreated, duedate
relationships_users - id, partyone, partytwo, expirydate,active

这是关系模型

  <?php

class Relationship extends AppModel
{

    var $name = 'Relationship';
    public $useTable = 'relationships_users';
    public $primaryKey = 'id';
    public $belongsTo = array(
        'Invoice' =>
            array(
                'className'              => 'Invoice',
                'joinTable'              => 'invoice',
                'foreignKey'             => 'invoice_id'));
    public $belongsTo = array(
        'User' =>array(
            'className' => 'User',
            'foreignKey' =>'partyone','partytwo',
            'associationForeignKey'  => 'username',
            )); 

    var $validate = array(
        'date' => array(
            'rule' => array(
                'datevalidation',
                'systemDate'
            ),
            'message' => 'Current Date and System Date is mismatched'
        ),
        'partytwo' => array(
            'userExists' => array(
                'rule' => array(
                    'userExists',
                ),
                'message' => 'That username doesnt exist.'
            ),
        ),
    );

    function datevalidation($field = array(), $compare_field = null)
    {
        if ($field['date'] > $compare_field)
            return TRUE;
        else
            return FALSE;
    }

    function userExists($check)
    {   
        $userExists = $this->User->find('count', array('conditions' => array('User.username'=>$check)));
        if ($userExists == 1) {
           return TRUE;
        }
        else
            return FALSE;
    }

}

这是我的发票模型

   <?php
App::uses('AuthComponent', 'Controller/Component');

class Invoice extends AppModel{ 
        var $name='Invoice'; 
        public $useTable = 'invoices';
        public $primaryKey = 'id';
        public $hasMany = array(
        'Relationship' =>array(
            'className' => 'Relationship',
            'foreignKey' =>'relationship_id',
        )); 
        var $validate = array(
            'to' => array(
                'relationshipExists' => array(
                    'rule' => array(
                        'relationshipExists',
                        ),
                    'message' => 'sorry you dont have a relationship with that user.'
                    ),
                ),
            );


        public function relationshipExists($check){ 
            if($this->hasAny(array('Relationship.partytwo'=>current($check))))
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

    }

这是我的附加发票功能

public function addinvoice(){
    $this->set('title_for_layout', 'Create Invoice');
    $this->set('stylesheet_used', 'homestyle');
    $this->set('image_used', 'eBOXLogoHome.jpg');   
    $this->layout='home_layout';

    if($this->request->is('post')){
        pr($this->Invoice->set($this->request->data));
        if($this->Invoice->validates(array('fieldList'=>array('to','Invoice.relationshipExists')))){
        //  $this->Relationship->create();
            $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.');
             }
    }
4

1 回答 1

0

hasMany外键应该在另一个中。如果Relationship hasMany Invoices,则Invoices必须有外键relationship_id

似乎您要建模的是 a belongsTo,其中 a Relationship belongsTo an Invoice,这意味着外键在Relationship模型/表中。

于 2012-05-29T13:51:16.870 回答