0

I've got some code like this:

$this->validate(new \Phalcon\Mvc\Model\Validator\Uniqueness(['field' => $field]));
if (true == $this->validationHasFailed()) {
    throw new SpecialInternalUniqueException();
}

This works for all columns except for natural Primary Keys. That is, Primary Keys that are not surrogate keys (auto-incrementing integers). For example, in the job_titles table, the natural key column is "job_title" - which, in our case, refers to the name of the job title. This name should be unique, and I want to be able to check for that in the code prior to saving. However, Phalcon happily ignores it, somehow.

I'm actually setting up a unit test for this right now and doing something similar to the following:

$job_title = new JobTitles();
$job_title->job_title = 'Unique Test';
$job_title->description = 'Desc A';
$job_title->save();

$job_title2 = new JobTitles();
$job_title2->job_title = 'Unique Test';
$job_title->description = 'Desc B';
$job_title->save();

The exception never gets thrown. What ends up in the database is a single column for the first Unique test with Desc A, and no record for the second one. But I don't get a thrown exception.

Any thoughts?

EDIT:

Also, I've tried with the ->create() function in place of the save() function.

Mr Mikkél
  • 2,577
  • 4
  • 34
  • 52

1 Answers1

1

First you should be aware that in the default behavior those validations are created from the actual database schema right after the model class is initialized; you're not supposed to add them manually in that case.

In other words, the default meta-data strategy for models is the Database Introspection Strategy

So a exception will only be raised if the job_title field is already indexed for uniqueness checking in the database scheme. If you aren't able to actually create this PK in the database, you may change the default meta-data strategy for your models and them set the metadata manually (sigh).

Community
  • 1
  • 1
cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
  • Even with the Database Introspection Strategy, nothing happens when I attempt to add a second record with the same natural key. Somehow Phalcon doesn't recognize it. – Mr Mikkél Nov 18 '14 at 18:26
  • Also - I'd be way more than happy to let Phalcon manage all of that for me. Is there a way, though, to define and throw my own exceptions when validation fails based on what it is that fails? – Mr Mikkél Nov 18 '14 at 19:11
  • Hmm.... what's more, if I don't handle my own validation for things like max length, Phalcon doesn't do anything about it. I just tried to put something into the DB that goes past the length of the field. Both Phalcon and the DB happily complied and shoved my data (truncated) into the column... – Mr Mikkél Nov 18 '14 at 19:35
  • 1
    @MrA Maybe it's fired only from the database?! Not sure if this is the correct behavior... file an issue to Phalcon so we can find out more – cvsguimaraes Nov 19 '14 at 12:02
  • ...by database I mean the native PDO adapter used by the Phalcon ORM – cvsguimaraes Nov 19 '14 at 19:04