The great Class::DBI clean up continues.
Today I pulled about 150 lines out of the main module into a support module. This was pretty hairy as it was the code that handled all the 'relationships' between classes - which is pretty important stuff for a module that handles database mappings!
I originally attempted to do this via mixins - basically just exporting all the methods I needed. But some of the methods over in Class::DBI proper needed to play with all the relationship data too, so I ended up exporting almost every method, including supposedly 'private' ones, which is always a worrying sign.
Thankfully the hooks I'd added when I was writing the Class::DBI::mysql::FullTextSearch module proved their worth. So I was able to take code, such as that for cascading delete, which needed to follow the relationship path, and implemented it trivially in the helper module just registering a delete hook.
Unfortunately, the other major point in Class::DBI where standard behaviour changes if there are relationships is create(). If you try to create with a field set to a related object rather than just its id
CD->create({
title => $title, artist => $artist })
instead of
CD->create({
title => $title, artist => $artist->id })
then it needs to DWIM. And we have no pre_create hook as there is no object at that point we can play with :(
For now, I've just implemented a _tidy_creation_data method which does nothing in Class::DBI proper, but which gets overridden when you set up a relationship to one that downgrades embedded objects to their ids.
I don't like this though, and I need to find a better way ...