Yii2 规则 compareAttribute 与另一个模型的属性进行比较 [英] Yii2 rules compareAttribute compare with attribute of another model
本文介绍了Yii2 规则 compareAttribute 与另一个模型的属性进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在验证期间,我想与另一个模型的属性进行比较.是否可以?如果是,如果您能指出我正确的方向,我将不胜感激.我想象以某种方式访问模型 A 中的模型 B,但也许我的逻辑不好,我不知道如何实现这一点.谢谢.
during validation I would like to compare with an attribute from another model. Is it possible? If yes, I would be grateful if you would point me to the right direction. I imagine it somehow to access model B in model A, but maybe my logic is not good, and I have no clue how can this be achieved. Thanks.
推荐答案
我是这样解决的:
public function getRelatedmodel() {
return $this->hasOne(\app\models\Relatedmodel::className(), ['id' => 'relatedId']);
}
public function getMotherRelatedAttribute() {
if ($mother = Model::findOne($this->mother)) {
return $mother->relatedmodel->attribute;
}
}
在规则中:
['attribute', 'compare', 'compareAttribute' => 'MotherRelatedAttribute', 'operator' => '<=', 'on' => self::SCENARIO_CREATE_RST],
这篇关于Yii2 规则 compareAttribute 与另一个模型的属性进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文