简体   繁体   English

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

[英]Yii comparing attribute values of two different models

In my web application I need to compare the attribute values of two different models 在我的Web应用程序中,我需要比较两个不同模型的属性值

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 . 我想比较属性命名的值booked_quantity一个名为模型BookVegetable对指定的属性的值offered_qty型号名称的ProducerOffer What I want is the values of booked_quantity should be lesser or equal to offered_qty but not greater. 我想要的是booked_quantity的值应该booked_quantity或等于offer_qty,但不大于。 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? 并生成Flash错误消息? 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. 请帮助ProducerOffer模型中的关系函数。

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

        );
    }

First, if you want get booked_quantity, it will looks 首先,如果您想获得booked_quantity,它将看起来

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

Second, 第二,

public function compareBookedQuantity($booked_quantity,$params) 公共函数compareBookedQuantity($ booked_quantity,$ params)

Here $booked_quantity == 'offered_qty'. 这里$ booked_quantity =='offered_qty'。 It is the name of an attribute. 它是属性的名称。 But i didn't fully understand your question. 但是我不完全理解你的问题。 What model contains compareBookedQuantity function? 哪个模型包含compareBookedQuantity函数?

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

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