简体   繁体   中英

Yii comparing two attributes of different models

In my web application I need to compare attributes of two different models. I have two models named "ProducerOffer" and "BookVegetable" . I need to compare the two individual attributes . "booked_quantity" of "BookVegetable" table and "offered_qty" of "ProducerOffer" table . The condition should check if the "booked_quantity" is less than the "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. 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');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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