簡體   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