I made a model Validator that checks the uniqness of a model by multiple fields
namespace AutoPoster\Backend\Validators;
use Phalcon\Mvc\Model\Validator;
use Phalcon\Mvc\Model\ValidatorInterface;
class UniqueFields extends Validator implements ValidatorInterface{
public function validate($model){
$fields = $this->getOption('fileds');
if(!is_array($fields)){
return false;
}
$conditions = "";
$binds = array();
$i=0;
foreach($fields as $field){
$i++;
if($i>1)
$conditions.=' AND ';
$conditions .= $field.' = '.$i.'?';
$binds[] = $model->$field;
}
if($model::findFirst(array(
'conditions' => $conditions,
'bind' => $binds
))){
$this->appendMessage(
$this->getOption('message'),
'Fileds',
'UniquePlanificationValidator'
);
return false;
}
return true;
}
}
in the validation function of a model I did:
$this->validate(new UniqueFields(array(
'fields' => array('datePublication', 'Articles_id'),
'message' => 'Planification already exists',
)
));
I got this error:
Fatal error: The argument is not iterable() in <MY DIR>\app\backend\models\Planifications.php on line 60