[Dbix-class] Searching by related field value
Sir Robert Burbridge
rburbrid at cisco.com
Wed May 12 17:26:19 GMT 2010
Hey all,
I'm having a (probably?) simple problem that I just don't *quite* grok:
How do I search for a row based on properties of an foreign key
property's sub-property?
That is, if I have two tables like these:
CREATE TABLE `vote` (
`id` int(64) NOT NULL auto_increment,
`document` int(64) NOT NULL default '0' COMMENT 'The unique id of
this document version.',
`created_by` int(64) NOT NULL default '0' COMMENT 'The unique id of
this version of this user.',
`created_on` timestamp NOT NULL default CURRENT_TIMESTAMP,
`rating` int(4) unsigned NOT NULL default '5' COMMENT 'The value of
this vote. 0-10',
PRIMARY KEY (`id`),
KEY `vote_document` (`document`),
KEY `vote_created_by` (`created_by`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Votes reflect user
feedback pertaining to documents. Each u' AUTO_INCREMENT=17 ;
CREATE TABLE `user` (
`id` int(64) NOT NULL auto_increment COMMENT 'The unique id of this
version of this user.',
`version` int(64) NOT NULL default '0' COMMENT 'The version number of
this user.',
`user_name` varchar(8) NOT NULL default '' COMMENT 'This value could
change (with marriage, for example)',
`first_name` varchar(32) default NULL COMMENT 'This value could change.',
`last_name` varchar(32) default NULL COMMENT 'This value could change
(with marriage, for example)',
`job_title` varchar(255) NOT NULL default '' COMMENT 'The name of the
job title of this user',
`created_on` timestamp NOT NULL default CURRENT_TIMESTAMP on update
CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `revision` (`user_name`,`version`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This is a temporal table
representing a user during a given ' AUTO_INCREMENT=3 ;
How can I find how the user with username "john_doe" voted on document 123?
I've been trying things like:
$schema->resultset('Vote')
->search(document => 123)
->search_related(user_name => 'john_doe');
But that gives me a resultset of *user* objects (I understand why and I
agree that it should). I just don't quite see how I can get "the vote
rows created by users with user_name 'john_doe'".
Help? =)
Thanks!
-Sir
More information about the DBIx-Class
mailing list