[英]Yii comparing attribute values of two different models
在我的Web应用程序中,我需要比较两个不同模型的属性值
我想比较属性命名的值booked_quantity
一个名为模型BookVegetable
对指定的属性的值offered_qty
型号名称的ProducerOffer
。 我想要的是booked_quantity
的值应该booked_quantity
或等于offer_qty,但不大于。 我尝试搜索它,但是没有找到用于比较两个不同模型属性值的地方。 我应该如何进行? 还是应该在控制器本身中进行比较? 并生成Flash错误消息? 我尝试过的代码是徒劳的
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'),
);
}
在我的表单中,我具有两个模型的值。 我应该如何比较属性值? 请帮助ProducerOffer模型中的关系函数。
public function relations()
{
return array( 'producerOfferBookVegetableRelation'=>array(self::BELONGS_TO, 'BookVegetable','id'),
);
}
首先,如果您想获得booked_quantity,它将看起来
$count = BookVegetable::model()->findByAttributes(array('booked_quantity'=>$booked_quantity))->booked_quantity;
第二,
公共函数compareBookedQuantity($ booked_quantity,$ params)
这里$ booked_quantity =='offered_qty'。 它是属性的名称。 但是我不完全理解你的问题。 哪个模型包含compareBookedQuantity函数?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.