We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Don't fail when related models fail validation

I'm saving a model (Subscribers) that has a one-to-many relationship to Subscriptions. A single subscriber can have subscriptions to multiple mailing lists. When saving, I assign an array of Subscription objects to the Subscriber, so that when it's saved, its Subscriptions get saved as well. The Subscription class validates itself and fails validation if there is already a subscription record with that subscriber id and mailing list id. The problem I'm having is that if a single subscription fails validation, the whole save fails.

Is there a setting to allow saving to continue if a related record fails validation? Or a way to set validation to be non-fatal?



8.4k
Accepted
answer
edited Sep '20

i don't think so

Model::_preSaveRelatedRecords() returns false as soon as any related save fails

source



8.4k

the above was for pre save which is for belongs to relations

but anyway the same thing applies for Model::_postSaveRelatedRecords()

sorry about that

the above was for pre save which is for belongs to relations

but anyway the same thing applies for Model::_postSaveRelatedRecords()

sorry about that

No worries. A "No" is a "No" regardless of the reason. It was enough for me to decide to build a workaround. Thanks.