Firstly, I have to say thank as this is one of the best libraries ever! Great job!
But I have an issue that I’ve been trying to find in this post but could not as it has about 90 pages :cheese: ! So I will post it again here, sorry if it has been asked.
I think it is something about saving relationships and object itself at the same time, and also a bit to do with transactions.
The situation is, for blog we need to save tags for entry, entry has many tags, tags have many entries and so on. I’ve done the saving new entry bit, it’s fine, but when it comes to edit an existing entry which already has a number of tags, it seems a bit tricky to me.
SITUATION:
1. Entry has a validation rule that tag is required
2. Entry 1 has been created with 4 tags: tag1, tag2, tag3 and tag4
3. User updates it and clears all the tag. This should fail and all tags stay there.
WHAT I DID:
$entry = new Entry();
$entry->get_by_id($id);
// (D) Delete all existing tags inside memory
$entry->tag->delete_all();
// (L) Then loop through new tags coming from post and add them in.
// Nothing in this case as user does not enter anything
// Finally save the changes of other details like title, etc.
$entry->save();
WHAT HAPPENED:
- It does not allow user to save as it does not pass validation (requires tag). This is GOOD.
- However, all tags are gone because they have been deleted before the validation happens! This is BAD.
WHAT I EXPECTED:
- Display a message saying that tag is required
- Nothing changes in the DB, no tag is deleted.
QUESTION:
- How could I update the entry->tag without changing the DB so that when validate() runs at the save() command, it fails and nothing changes?
I tried to use transactions like this but no luck:
- trans_begin()
- delete()
- trans_rollback()
Transaction does not recover records that have been deleted when rolling back.
Thanks.