繁体   English   中英

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

[英]Yii comparing two attributes of different models

在我的Web应用程序中,我需要比较两个不同模型的属性。 我有两个名为“ ProducerOffer”和“ BookVegetable”的模型。 我需要比较两个单独的属性。 “ BookVegetable”表的“ booked_quantity”和“ ProducerOffer”表的“ offered_qty”。 条件应检查“ booked_quantity”是否小于“ 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');
}

暂无
暂无

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

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