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


Yii comparing two attributes of different models

在我的web应用程序中,我需要比较两个不同模型的属性。我有两个型号,分别是"ProducerOffer"answers"BookVegetable"。我需要比较这两个单独的属性。"BookVegetable"表的"booked_quantity"answers"ProducerOffer"表的"offered_quantity"。条件应检查"预订数量"是否小于"报价数量"。我为我的检查条件写的代码

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

但验证根本没有发生。我应该如何更正此错误?

$booked_quantity-是一个属性,而不是属性值。您的功能必须是:

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