1

大家好,我终于对我的发票模型进行了 100% 的验证,但现在它取消了我在关系模型中的验证(它正在工作),因为现在它引用了错误表中的所有内容。

关系模型应该确保用户在您向他们发送请求之前存在于用户表中。

发票模型应该确保一个用户在关系表中具有另一个用户的列。

如何更改它以使其正常工作?目前,代码完全抛弃了我在网站上的整个关系方面。

-关系模型

class Relationship extends AppModel
{

    var $name = 'Relationship';
    public $useTable = 'relationships_users';
    public $primaryKey = 'id';
    public $hasMany = 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 User extends AppModel{ 
    public $name = 'User';
    public $hasMany = array(
        'Relationship' =>
            array(
                'className'              => 'Relationship',
                'joinTable'              => 'relationships_users',
                'foreignKey'             => 'id',
                'unique'                 => false,));


    public $useTable = 'users';
    public $primaryKey = 'id';

    public $validate = array(
        'username'=>array(
            'The username must be between 5 and 15 characters.'=>array(
                'rule'=>array('between', 5, 15),
                'message'=>'The username must be between 5 and 15 characters.'
            ),
            'That username has already been taken'=>array(
                'rule'=>'isUnique',
                'message'=>'That username has already been taken.'
            )),
        'email'=>array(
            'Valid email'=>array(
                'rule'=>array('email'),
                'message'=>'Please enter a valid email address'
            )
        ),
        'password'=>array(
            'Not Empty'=>array(
                'rule'=>'notEmpty',
                'message'=>'Please enter your password'
                )
            ),
            'Match passwords'=>array(
            'rule'=>'matchPasswords',
            'message'=>'Your passwords do not match'
            )
            ,
            'password_confirmation'=>array(
            'Not Empty'=>array(
                'rule'=>'notEmpty',
                'message'=>'Please confirm your password'
            )));


    public function matchPasswords($data){
        if ($data['password']==$this->data['User']['password_confirmation']){
        return true;
        }
        $this->invalidate('password_confirmation','Your passwords do not match');
        return false;
    }

    public function beforeSave(){

    if(isset($this->data['User']['password'])){
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
        }
        return true;
    }


    }
    ?>

发票模型-

class Invoice extends AppModel{ 
        var $name='Invoice'; 
        //public $useTable = 'invoices';
        //public $primaryKey = 'id';
        public $belongsTo = 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.'
                    ),
                ),          
            );

            var $validateTwo = array(
            'datecreated'=>array(
                'dateHasntExpired' => array(
                    'rule'=>array(
                        'dateHasntExpired',
                        ),
                    'message'=> 'sorry but your relationship has expired.'
                    ),
                ),
            );

        public function relationshipExists($check){ 

        $relationshipExists=$this->Relationship->find('count', array(
            'conditions' => array(
            'Relationship.partyone <>' => current($check),
            'Relationship.partytwo' => current($check),
        //  'Relationship.active'==true,
            // get the value from the passed var
  )
));
                if ($relationshipExists == true) {
                    return TRUE;
                    }
                else
                    return FALSE;
                    }
        public function dateHasntExpired($check){   
            $dateHasntExpired=$this->Relationship->find('count', array(
            'conditions'=>array(
            'DATE(Relationship.expirydate) > DATE(Invoice.datecreated)',
                )));
                    if ($dateHasntExpired == true) {
                        return TRUE;
                    }
                    else
                        return FALSE;
                    }



    }

这是尝试查看当前关系请求时出现的错误

Database Error

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Invoice.invoice_id' in 'field list'

SQL Query: SELECT `Invoice`.`id`, `Invoice`.`to`, `Invoice`.`biller`, `Invoice`.`subject`, `Invoice`.`description`, `Invoice`.`amount`, `Invoice`.`datecreated`, `Invoice`.`duedate`, `Invoice`.`invoice_id` FROM `pra_cake`.`invoices` AS `Invoice` WHERE `Invoice`.`invoice_id` IN (97, 98, 99, 101, 104, 105)

Notice: If you want to customize this error message, create 

应用\查看\错误\pdo_error.ctp

这是我在尝试查看我发送的关系请求时遇到的错误

Database Error

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Invoice.invoice_id' in 'field list'

SQL Query: SELECT `Invoice`.`id`, `Invoice`.`to`, `Invoice`.`biller`, `Invoice`.`subject`, `Invoice`.`description`, `Invoice`.`amount`, `Invoice`.`datecreated`, `Invoice`.`duedate`, `Invoice`.`invoice_id` FROM `pra_cake`.`invoices` AS `Invoice` WHERE `Invoice`.`invoice_id` IN (97, 98, 99, 101, 104, 105)

Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp
4

1 回答 1

1

需要在我的控制器中使用 unbind 方法,这是我在整个关系控制器中使用的 unbind 方法的示例。

public function approve($id=null){
        $this->set('title_for_layout', 'Relationships');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.jpg');   
        $this->layout='home_layout';
        $this->Relationship->unbindModel(array('hasMany'=>array('Invoice')));
        if ($this->request->is('get')) {
        $this->request->data = $this->Relationship->read(NULL, $id);

        } else {
        //sets active to 1
         $this->Relationship->read(null, $id);
         $this->Relationship->set(array('active' => true,));
        if ($this->Relationship->save($this->request->data)) {

            $this->Session->setFlash('Your post has been updated.');
            $this->redirect(array('action' => 'request'));
        } else {
            $this->Session->setFlash('Unable to update your post.');
        }
    }

因为这在我的发票控制器中引发了错误,所以我在那里也使用了 unbind,这是我在 invoicescontroller 中使用的取消绑定的示例

 public function viewInvoice($id = NULL){
        $this->set('title_for_layout', 'View invoice');
        $this->set('stylesheet_used', 'homestyle');
        $this->set('image_used', 'eBOXLogoHome.jpg');
        $this->Invoice->unbindModel(array('belongsTo'=>array('Relationship')));     
        $this->set('invoice', $this->Invoice->read(NULL, $id));
}
于 2012-05-30T01:35:13.327 回答