function isUnique($data, $fields) { // check if the param contains multiple columns or a single one if (!is_array($fields)) { $fields = array($fields); } // go trough all columns and get their values from the parameters foreach ($fields as $key) { $unique[$key] = $this->data[$this->name][$key]; } // primary key value must be different from the posted value if (isset($this->data[$this->name][$this->primaryKey])) { $unique[$this->primaryKey] = "<>" . $this->data[$this->name][$this->primaryKey]; } // use the model's isUnique function to check the unique rule return parent::isUnique($unique, false); }
/** * Checks the uniqueness of multiple fields * * Loops through the field that should be unique. * If a field is not present in the data array and * the validation is for an existing record, * (if an ID is present) it is tried to load the value of the field. * Otherwise it's considered to be empty (e.g. when adding). * * @param Model $model Model using this behavior * @param array $data Unused * @param array $fields The fields to be checked * @return bool True if valid, else false * @see Model::isUnique() Makes use of */ public function multiColumnUniqueness(Model $model, $data, $fields) { if (!is_array($fields)) { $fields = array($fields); } $check = array(); foreach ($fields as $key) { if (isset($model->data[$model->name][$key])) { $value = $model->data[$model->name][$key]; } elseif (!empty($model->id)) { $value = $model->field($key, array($model->primaryKey => $model->id)); if ($value === false) { $value = null; } } else { $value = null; } $check[$key] = $value; } return $model->isUnique($check, false); }