简体   繁体   中英

Yii comparing attribute values of two different models

In my web application I need to compare the attribute values of two different models

I want to compare the value of attribute named booked_quantity of a model named BookVegetable to the value of a attribute named offered_qty of a model name ProducerOffer . What I want is the values of booked_quantity should be lesser or equal to offered_qty but not greater. I tried searching it but no where I found for comparing two different model attribute values. How should I proceed ? or should I compare it in the controller itself? and generate flash error message? The code I tried but in vain

public function compareBookedQuantity($booked_quantity,$params){
    $count = BookVegetable::model()->findByAttributes(array('booked_quantity'=>$booked_quantity));
    if ($count > $this->offered_qty){
        $this->addError($booked_quantity,'Please enter a quantity lesser than offered quantity');
    }
}

public function rules()
    {

        return array(

array('offered_qty','compareBookedQuantity'),
);
}

In my form I have values of both models. How should I compare the attribute values ? please help The relations function in ProducerOffer model.

public function relations()
    {
        return array( 'producerOfferBookVegetableRelation'=>array(self::BELONGS_TO, 'BookVegetable','id'),  

        );
    }

First, if you want get booked_quantity, it will looks

$count = BookVegetable::model()->findByAttributes(array('booked_quantity'=>$booked_quantity))->booked_quantity;

Second,

public function compareBookedQuantity($booked_quantity,$params)

Here $booked_quantity == 'offered_qty'. It is the name of an attribute. But i didn't fully understand your question. What model contains compareBookedQuantity function?

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