DbNoRecordExists on PATCH/PUT request

Hello!

I’m using Api-tools to create my API Rest with DBConnected. I like to use it because it adds filter and validation based on my table. But I have problem when I’m using PATCH/PUT to update my record on db when there is a “unique” field value and it is in the same record, then I cannot update it because DbNoRecordExists.

I read there is a “exclude” param to avoid this issue, but how can I send this value without use a constant one?

Thank you

Can I send this exclude > value to Validation? What I need to implement for it?

Hey Julian,

you have a few approaches you can take to solve your problem.

Using the right SQL statement for partial updates or updates

the simplest solution to your problem would be to adjust your update statement in your table class. If there 's just a single unqiue constraint defined, you can use a ON DUPLICATE KEY UPDATE statement.

Overriding resource methods

If the above does not work for you, you can easily override the patch and update method in your respective db connected resource to unset the unique value if it 's given in the patch / put data.

Method specific input filters

The Laminas API Tools allows you to use different input filters for specifiv http methods. You can define a single input filter for patch and put methods. Define your input filter as precise as possible and get only the filtered parameters.

Excluding values

As far as I know excluding values is a thing of Laminas hydrators. With the respective filters used with a hydrator you can archive, that some properties of an object are not extracted. There you can exclude values. Since validators are the last thing that would happen in the described scenario, you can not use them to remove parameters from the post request. The above described approaches are the ways to go.

1 Like

Hello ezkimo!
Thank you so much for your time.

I’m feeling more comfortable to use “Overriding resource methods”, but I understand that validation occurs before, so I would need to delete DbNoRecordExists and implement this ‘validation’ on resource class, right?

Keep in mind, that the DbNoRecordExists validator would validate your data as invalid, when you exlude a single rowset with a specific value. The validator checks all rowsets in your table except the one with, ex. unique_value => bla. As a result of that the rowset, which you want to update, will not be found and the api tools will respond with a 404. The DBNoRecordExists validator is no solution for your issue.

I might be wrong. For a deeper understanding of what you want to do a detailed description and an example of your code would help.