2017-06-14 1 views
0

J'ai un modèle TblPayroll avec les attributs ci-dessus, je veux vérifier l'unicité de 'fk_int_emp_id', 'fk_int_payroll_month', 'fk_int_payroll_year'. Si ce trois champs déjà dans la base de données, il ne doit pas insérer. Au moins un est différent qu'il devrait insérer. Comment puis-je vérifier l'unicité?Comment définir 3 attribut est unique de modèle

public function attributeLabels() 
    { 
     return [ 
      'pk_int_payroll_id' => 'Payroll Id', 
      'fk_int_emp_id' => 'Employee', 
      'vchr_worked_hours' => 'Worked Hours', 
      'vchr_actual_hours' => 'Actual Hours', 
      'fk_int_payroll_month' => 'Month', 
      'fk_int_payroll_year' => 'Year', 
     ]; 
    } 

public function rules() 
    { 
     return [ 
      [[ 'vchr_worked_hours', 'vchr_actual_hours', 'fk_int_payroll_month', 'fk_int_payroll_year'], 'required'], 

      //[['fk_int_emp_id', 'fk_int_payroll_year','vchr_worked_hours','vchr_actual_hours'], 'integer'], 
      //[['fk_int_emp_id','fk_int_payroll_month','fk_int_payroll_year'], 'string', 'max' => 50], 
      [['fk_int_emp_id'], 'exist', 'skipOnError' => true, 'targetClass' => TblEmployee::className(), 'targetAttribute' => ['fk_int_emp_id' => 'pk_int_emp_id']], 
      [['fk_int_payroll_month'], 'exist', 'skipOnError' => true, 'targetClass' => TblPayrollMonth::className(), 'targetAttribute' => ['fk_int_payroll_month' => 'pk_int_payroll_month_id']], 
      [['fk_int_payroll_year'], 'exist', 'skipOnError' => true, 'targetClass' => TblPayrollYear::className(), 'targetAttribute' => ['fk_int_payroll_year' => 'pk_int_payroll_year_id']], 
     ]; 
    } 
+0

@scaiseEdge vérifier ce que vous avez défini –

+0

attribut unique dans les règles non étiquettes, montrent la section règles nous. –

+0

s'il vous plaît vérifier les édités, j'ai besoin d'insérer ce modèle, mais tout en insérant 'fk_int_emp_id', 'fk_int_payroll_month', 'fk_int_payroll_year' il devrait être unique à sametime. si quelqu'un de différent dans ces trois domaines, il devrait insérer ... –

Répondre

0

Essayez cette

[['fk_int_emp_id', 'fk_int_payroll_month', 'fk_int_payroll_year'], 'unique', 'targetAttribute' => ['fk_int_emp_id', 'fk_int_payroll_month', 'fk_int_payroll_year'], 'message' => 'The combination of .... has already been taken.'], 
+0

grand son fonctionnement correctement, merci somuch –