Cybris has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

I'm in a bit of a conundrum. I'm writing a manager for user created objects (think CMS or file manager) which are stored in folders (related to a "folder" table in the database). If a user doesn't create subfolders, his objects are created in the main folder. There is no real main folder for each user; his objects are just stored with folder id 0.

I now have some methods that need to be called on a folder, e.g. a method that lists the objects in a folder. I handled the main folder by creating a DBIx::Class::Row object in-memory with new(), but can't seem to be able to call the object's methods, or access the relationship accessors which DBIx::Class usually creates automatically. DBIx::Class only seems to connect relationships if the object has been loaded from storage (or created in-storage).

Is it possible to connect the relationships to the in-memory object somehow? Remember that this object does not exist as a row in the database, but the relationships would still be valid (because of the column value 0 for the folder id). I only need the relationships one-way.

Alternatively, is it possible to assign methods to this object (package instance) without modifying the package? I can fake the one or two relationships I need with simple subs, but I can't attach those to the instance.

If it is not possible, I will have to create main folders for each user, move the objects to these folders and update a number of Controller actions that deal with folders. Your help would be very appreciated.

  • Comment on Adding a new method to a package instance / Connecting DBIx::Class relationships to in-memory objects