I am trying to import users into the db using cakephp 3. It is working, but due to a rule I added, when an email address is being imported that already exists, it fails (rollback kicks in). This is the code for the rule:
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email'], 'Email ID Already Exists'));
return $rules;
}
Now what I want to know is, how can I skip a record that does already exists and continue to the next one.
Here is part of the code I use to save the array of users:
$usersTable = TableRegistry::getTableLocator()->get('users');
$useritems = $usersTable->newEntities($new_array, [
'validate' => 'get_users_from_google'
]);
if ($usersTable->saveMany($useritems)) {
echo json_encode('success', 1);
}else{
echo json_encode('failed', 1);
}
This code is part of a function which is called via ajax.