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=innodb;
So you might have either relationship
__PACKAGE__->might_have('asset', 'schema::assets', { 'foreign.id' =>
'self.asset_id' } );
__PACKAGE__->might_have('tag', 'schema::tags', { 'foreign.id' =>
'self.tag_id' } );
Which you can then join both in your code, leftwards because they're
mights...
$row = $resultset->search( { ... }, { prefetch => [ asset, tag ] }
)->single;
$object = $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 => '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.
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/[email protected]
_______________________________________________
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/[email protected]