简体   繁体   English

Yii比较不同模型的两个属性

[英]Yii comparing two attributes of different models

In my web application I need to compare attributes of two different models. 在我的Web应用程序中,我需要比较两个不同模型的属性。 I have two models named "ProducerOffer" and "BookVegetable" . 我有两个名为“ ProducerOffer”和“ BookVegetable”的模型。 I need to compare the two individual attributes . 我需要比较两个单独的属性。 "booked_quantity" of "BookVegetable" table and "offered_qty" of "ProducerOffer" table . “ BookVegetable”表的“ booked_quantity”和“ ProducerOffer”表的“ offered_qty”。 The condition should check if the "booked_quantity" is less than the "offered_quantity". 条件应检查“ booked_quantity”是否小于“ offered_quantity”。 The code I wrote for my check condition 我为检查条件编写的代码

public function compareBookedQuantity($booked_quantity,$params){
    if(BookVegetable::model()->findByAttributes(array('booked_quantity'=>$booked_quantity ))> $this->offered_qty){

        $this->addError($attribute,'Please enter a quantity lesser than offered quantity');
      }
    }

public function rules()
{

array('offered_qty','compareBookedQuantity'),


 array(' vegetable_id, offered_qty, unit_cost, unit_delivery_cost', 'required'),
        array(' offered_qty, unit_cost, unit_delivery_cost, booking_status, booked_by, available_days', 'numerical'),
        array('user_id', 'length', 'max'=>11),
array('offered_qty','compareBookedQuantity'),


array('id,userName,user_id, vegetable_id, unit_cost,book_vegetable_id, unit_delivery_cost, offered_date,offered_quantity,available_quantity,booking_status, booked_by, available_days', 'safe', 'on'=>'search'),
    );
}

But the validation is not happening at all. 但是验证根本没有发生。 How should I correct this error? 我应该如何纠正这个错误?

$booked_quantity - is an Attribute - not attribute value. $ booked_quantity-是属性-不是属性值。 Your function must be: 您的功能必须是:

public function compareBookedQuantity($booked_quantity,$params){
    $count = BookVegetable::model()->findByAttributes(array('booked_quantity'=>$this->$booked_quantity))->#field#;
    if ($count > $this->offered_qty)
        $this->addError($booked_quantity,'Please enter a quantity lesser than offered quantity');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM