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

John Ramsden jhnrmsdn at yahoo.co.uk
Wed Mar 25 09:55:41 GMT 2009


Hi DBIx::Class hackers
=A0
Let's say=A0we have two unrelated tables, "tag" and "asset", each with thei=
r own
sequence of auto-incrementing IDs (so of course the same ID may occur in
both, for records unrelated to each other).
=A0
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 ta=
ble by:
=A0
CREATE TABLE `edit_log`
(
=A0 `id`=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 int(11) NOT NULL auto_incre=
ment,
=A0=A0 object_type=A0=A0=A0=A0=A0 ENUM('tag', 'asset'),
=A0=A0 object_id=A0=A0=A0=A0=A0=A0=A0 int(11) NOT NULL,
=A0 `comment`=A0=A0=A0=A0=A0=A0=A0=A0 text,
=A0=A0 :::
);
=A0
My question is how does one represent the extra condition on object type
to define this "one or other"=A0relationship in the DBIx::Class module for
the edit_log table, and for that matter in the "tag" and "asset" tables?
=A0
Presumably in EditLog.pm it needs to be something along the lines of:
=A0
=A0 __PACKAGE__->has_one=A0=A0=A0 (asset=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 =
=3D> 'Ethel::Schema::Asset',
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 { =
'self.object_id' =3D> 'foreign.id' },
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 { -is =3D> { 'self.object_type' =
=3D> 'asset' } );

=A0 __PACKAGE__->has_one=A0=A0=A0 (tag=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0 =3D> 'Ethel::Schema::Tag',
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
 { 'self.object_id' =3D> 'foreign.id' =

=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 =A0=A0=A0 { -is =3D> { 'self.object_type' =
=3D> 'tag' } );
=A0
I'm fairly sure the "-is" syntax is wrong, but that is basically
my question. How should this condition be expressed?
=A0
I did check the documentation, but found no examples of how
to express extra conditions.
=A0
Many thanks in anticipation for any replies.
=A0
=A0
Regards
=A0
John Ramsden
=A0


      =

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20090325/8d4=
cc506/attachment-0001.htm


More information about the DBIx-Class mailing list