我有两个模型Session和SessionTicketType如下所示。我想在验证时在Session模型中显示SessionTicketType错误。
模型Session.php
public function relations() {
$relations['sessionTicketTypes'] = [
self::HAS_MANY,
'SessionTicketType',
'session_id',
'index' => 'ticket_type_id'
];
return $relations;
}
和
模型SessionTicketType.php
public function relations() {
$relations['session'] = [self::BELONGS_TO, 'Session', 'session_id'];
return $relations;
}
public function rules(){
$rules = parent::rules();
$rules[] = ['selected', 'safe'];
$rules[] = ['slots', 'validateSlots'];
return $rules;
}
public function validateSlots($attribute,$params) {
if($this->slots < $this->getQuantitySold()){
$this->addError('slots', 'Slots can not be less than total booking.');
}
public function getQuantitySold()
{
$sql = "SELECT SUM(i.quantity)
FROM booking b
INNER JOIN booking_item i ON b.id = i.booking_id
WHERE b.session_id = :session_id
AND i.ticket_type_id = :ticket_type_id
AND b.status IN (:status_completed)";
return Yii::app()->db->createCommand($sql)->queryScalar([
':session_id' => $this->session_id,
':ticket_type_id' => $this->ticket_type_id,
':status_completed' => BookingStatus::STATUS_CONFIRMED,
]);
}
我有Session表单,其中添加了多个sessionTicketype。 我想在会话验证上添加此错误(validateSlots())。 sessionTicketType中的验证码validateSlots()未显示会话更新和保存数据的任何错误。 如何显示错误并限制保存。 或者还有其他方法可以在会话模型中添加此验证