Validate unique two tables


column: id
column: site_id


column: id
column: navigation_id
column: language_id
column: slug

Navigations table column: site_id allow NULL, 1, 2

I need to do a unique validation for two table. When I save, my records are saved in both tables at the same time.

Unique insert: (site_id = table Navigations), (language_id, slug = table NavigationsTexts)

Is it possible to validate between two tables?

you will need to do a custom validation. See

Please would not be an example, it is not entirely clear to me from the documentation.

Before sample code would be presented, it would be best to understand what you’re trying to do.

How are you currently posting 2 records into 2 tables at the same time? Wouldn’t Navigations be posted first? Otherwise how could you get the value for navigation_id in the NavigationsTexts table?

Would it be more correct to assume the Navigations record already exists, and its the validation of the navigation_id field in the NavigationsTexts table which requires the validation?

Please explain the situation more clearly, what does at the same time means to you.

Form helper plus patching does this just fine with field names like navigations_texts[0].language_id.


site_id = 1
navigations_texts[0][language_id] = 1
navigations_texts[0][slug] = '/some-url'

after click save…

$Content = $this->Navigations->newEmptyEntity();
$Content = $this->Navigations->patchEntity($Content, $this->request->getData());

A new autoincrement ID is created above, in the second table, the new id is copied to navigation_id.
Validation is performed during patchentity and here I would need to verify that the same string no longer exists in the slug column if site_id = 1 is in table 1.


Table: Navigations

id, site_id
1, 1

Table: NavigationsTexts

id, navigation_id, language_id, slug
1,  1, 1, '/some-url'

If I enter the same URL with site_id = 1, it will throw me an error, because the record already exists. If I choose site_id = 2, the record can be saved.