为什么cakephp会抛出数据库错误?

时间:2012-05-29 13:43:23

标签: database cakephp model

当我尝试将新发票插入数据库时​​,cakephp会抛出一条错误,指出它无法在where子句中找到列关系.partytwo。我正在努力实现的是确保发送发票的用户在发送发票之前在relationship_users数据库中与接收发票的人之间存在活动关系,遗憾的是我得到的只是数据库错误。

这是我表格的结构

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

    }

这是我的addinvoice函数

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

1 个答案:

答案 0 :(得分:0)

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

似乎您想要建模的是belongsTo,其中Relationship 属于 Invoice,这意味着外键位于{{} 1}} model / table。