[Dbix-class] Question on expressing relationship to one or other table depending on ENUM value

Jason Gottshall jgottshall at capwiz.com
Thu Mar 26 14:05:00 GMT 2009


John Ramsden wrote:
> [Please excuse resend, but first try was munged in daily digest, 
> possibly due to RTF characters]
>  
> Hi DBIx::Class hackers
> Let's say we have two unrelated tables, "tag" and "asset", each with 
> their own sequence of auto-incrementing IDs (so of course the same ID 
> may occur in both, for records unrelated to each other).
> Now suppose there is a third table, "edits", set up with a record to 
> allow users to log edits to records in either of the above tables. So we 
> define this table by:
> CREATE TABLE `edit_log`
> (
> `id` int(11) NOT NULL auto_increment,
> object_type ENUM('tag', 'asset'),
> object_id int(11) NOT NULL,
> `comment` text,
> :::
> );
> My question is how does one represent the extra condition on object type 
> to define this "one or other" relationship in the DBIx::Class module for 
> the edit_log table, and for that matter in the "tag" and "asset" tables?
> Presumably in EditLog.pm it needs to be something along the lines of:
> __PACKAGE__->has_one (asset => 'Ethel::Schema::Asset', { 
> 'self.object_id' => 'foreign.id' }, { -is => { 'self.object_type' => 
> 'asset' } );
> __PACKAGE__->has_one (tag => 'Ethel::Schema::Tag', { 'self.object_id' => 
> 'foreign.id'
> { -is => { 'self.object_type' => 'tag' } );
> I'm fairly sure the "-is" syntax is wrong, but that is basically my 
> question. How should this condition be expressed?
> I did check the documentation, but found no examples of how to express 
> extra conditions.
> Many thanks in anticipation for any replies.

Hardcoded (literal) values in join conditions are not (yet) supported by 
DBIC. Take a look at this previous post for ideas on a work-around that 
emulates the appropriate behavior:

http://lists.scsys.co.uk/pipermail/dbix-class/2009-February/007311.html

HTH,
Jason

-- 
Jason Gottshall
jgottshall at capwiz.com




More information about the DBIx-Class mailing list