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

David Ihnen davidi at norchemlab.com
Wed Mar 25 18:02:32 GMT 2009


This is the 'mutiple join conditions' feature that is wanted by just =

about everybody but isn't in the system yet, to my understanding.

This pattern may work better given the current state of the code - AND =

forces database constraint checking so you don't accidentally end up =

putting in a object_id that is neither tag_id or asset_id.

CREATE TABLE `edit_log`
(
`id` int(11) NOT NULL auto_increment,
`tag_id` int(11) NULL,
FOREIGN KEY (tag_id) references tags (id),
`asset_id` int(11) NULL,
FOREIGN KEY (asset_id) references assets (id),
`comment` text,
...
) engine=3Dinnodb;

So you might have either relationship

__PACKAGE__->might_have('asset', 'schema::assets', { 'foreign.id' =3D> =

'self.asset_id' } );
__PACKAGE__->might_have('tag', 'schema::tags', { 'foreign.id' =3D> =

'self.tag_id' } );

Which you can then join both in your code, leftwards because they're =

mights...

$row =3D $resultset->search( { ... }, { prefetch =3D> [ asset, tag ] } =

)->single;

$object =3D $row->asset or $row->tag;

The only real ambiguity left is if you managed to create an edit_log =

entry with both tag_id AND asset_id at the same time.  Which seems unlikely.

David


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 =3D> 'Ethel::Schema::Asset', { =

> 'self.object_id' =3D> 'foreign.id' }, { -is =3D> { 'self.object_type' =3D=
> =

> 'asset' } );
> __PACKAGE__->has_one (tag =3D> 'Ethel::Schema::Tag', { 'self.object_id' =

> =3D> 'foreign.id'
> { -is =3D> { 'self.object_type' =3D> '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.
>  =

> Regards
> John Ramsden
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.scsys.=
co.uk

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20090325/9fd=
9e991/attachment.htm


More information about the DBIx-Class mailing list