douganggu4392 2014-04-16 10:49
浏览 50

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

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'),  

        );
    }
  • 写回答

1条回答 默认 最新

  • duancai7568 2014-04-16 12:24
    关注

    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?

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入