Hi,
It seems to me that this is a bug, but not sure if it’s because I’m doing something wrong.
The bug is: after deleting one of the “has many” relationships, that relationship becomes NULL entirely. Eg, an entry has 5 tags, deleting one of tags makes $entry->tag NULL while it should remain a valid array with 4 tags in it
SITUATION:
- Entry and Tag are 2 models with many-to-many relationships
- An entry currently has 5 tags (eg. tag1, tag2, tag3, tag4, tag5)
WHAT I DID:
- I open that entry in the edit form, remove one tag (eg. tag1) and click save with the following code:
// I compare what is submitted with what exists in the DB to find out
// what will be deleted and what will stay. I dont want to blindly remove
// everything and just add in whatever new, just to keep my Id column small :blush:
// Plus, by doing this comparison, in case user just updates the entry details
// but not changing the tags, I dont want to remove and add back in the same list
// of tags as they are redundant.
// After running through all the comparisons, I got a few arrays like this:
// What will be deleted
$delete = array (
'tag' => array(
TAG_1_ID => TAG_1_OBJECT
)
);
// What will be added (nothing in this example)
$added = array (
'tag' => array()
);
// What will stay (unchanged)
$keep = array (
'tag' => array(
TAG_2_ID => TAG_2_OBJECT,
TAG_3_ID => TAG_3_OBJECT,
TAG_4_ID => TAG_4_OBJECT,
TAG_5_ID => TAG_5_OBJECT,
)
);
// Now I delete those that have been marked for deletion
$entry->delete($delete);
// ERROR HERE: Trying to access the tag again raises error
foreach ($entry->tag->get()->all as $k => $v)
{
// ...
}
WHAT I GET:
The foreach loop above will throw the following error because $entry->tag is now NULL:
Fatal error: Call to a member function get() on a non-object
WHAT I EXPECT:
- It should remove 1 tag and keep the remaining 4 tags
- $entry->tag should still remain a valid object of class/model and calling $entry->tag->get()->all should give me an array with 4 elements
I also tried to call $entry->refresh_all() directly after the delete(), but the same result.
Whta did I do wrong?
Thanks,
Khoa