在我的Web应用程序中,我需要比较两个不同模型的属性。我有两个名为" ProducerOffer"和#34; BookVegetable" 。我需要比较两个单独的属性。 " booked_quantity" " BookVegetable"表和" provided_qty" " ProducerOffer"桌子。条件应该检查" reservations_quantity"小于" provided_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'),
);
}
但验证根本没有发生。我该如何纠正这个错误?
答案 0 :(得分:0)
$ 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');
}