How do I use a DbValues Validator to check for database referential integrity errors

How do I use a DbValues Validator to check for database referential integrity errors

Dirk FincleyDirk Fincley Posts: 38Questions: 3Answers: 0
edited March 2016 in Free community support

I have a parent MySQL database table with a foreign key in a child table. When I delete a parent row which has associated child rows I get the MySQL error below:-

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (test.region, CONSTRAINT region_ibfk_country FOREIGN KEY (country_id) REFERENCES country (id))

Does anyone know how I should create a PHP Validator clause to check whether the delete would work and if not to be able to give a a user friendly error?

Please is there any example code. I could not find one?

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,783Questions: 1Answers: 10,511 Site admin
    edited March 2016

    Hi,

    The Editor libraries have a built in dbValues validator which you can use to perform this sort of validation.

    The join example uses it (in its simplest possible form).

    Allan

  • Dirk FincleyDirk Fincley Posts: 38Questions: 3Answers: 0

    As far as I can see there are no examples of dbValues being used for this, only to "enforce" referential integrity not to intercept a MySQL RI Violation.

  • allanallan Posts: 63,783Questions: 1Answers: 10,511 Site admin
    Answer ✓

    Oh sorry - I had misunderstood. At the moment, I'm afraid this would require a custom validation function to run before the Editor process runs - the PHP libraries do not currently provide a method for doing this.

    For example you might use:

    if ( Editor::action( $_POST ) === Editor::ACTION_DELETE ) {
      ... perform check against and return error if needed
    }
    

    This is an area where Editor's libraries are lacking at the moment I'm sorry to say. A new feature to address this is planned for 1.6.

    Allan

  • Dirk FincleyDirk Fincley Posts: 38Questions: 3Answers: 0
    edited March 2016

    Thanks so I execute the "select" myself and simply return either an error message or not as the case may be. If I can get it to work I will post it on the forum. I bet other people may want to do something similar.

    Where would I do the check? In my PHP or JS file?

  • Dirk FincleyDirk Fincley Posts: 38Questions: 3Answers: 0

    B the way, when is 16 version due?

  • allanallan Posts: 63,783Questions: 1Answers: 10,511 Site admin
    Answer ✓

    Thanks so I execute the "select" myself and simply return either an error message or not as the case may be.

    Exactly. This would be done in the PHP.

    B the way, when is 16 version due?

    1.6 will likely be some months before it is released. I don't have an exactly time frame yet.

    Regards,
    Allan

  • Dirk FincleyDirk Fincley Posts: 38Questions: 3Answers: 0

    Many thanks.

This discussion has been closed.