In Yii2, the SQL generated after the validation rule of the existent (present) is written and does not meet the expectations
1. In Yii2, the SQL generated after the validation rule of the existentiality is written is not as expected. as shown in Figure 1
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['survey_from_id'], 'exist', 'targetAttribute' => 'id', 'targetRelation' => 'survey_form', 'filter' => ['user_id' => Yii::$app->user->id], 'on' => self::SCENARIO_CREATE],
];
}
public function getSurvey_form()
{
return $this->hasOne(SurveyForm::class, ['id' => 'survey_form_id']);
}
SELECT EXISTS(SELECT * FROM `survey_forms` WHERE (`user_id`='1825203750689422') AND (0=1))
2. Give up the use of TargetRelation, and use TargetClass and TargetAttribute instead. Generating SQL is as expected.
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['survey_from_id'], 'exist', 'targetClass' => SurveyForm::class, 'targetAttribute' => ['survey_from_id' => 'id'], 'filter' => ['user_id' => Yii::$app->user->id], 'on' => self::SCENARIO_CREATE],
];
}
public function getSurvey_form()
{
return $this->hasOne(SurveyForm::class, ['id' => 'survey_form_id']);
}
SELECT EXISTS(SELECT * FROM `survey_forms` WHERE (`survey_forms`.`id`='1825203750689574') AND (`user_id`='1825203750689422'))
