debiandude has asked for the wisdom of the Perl Monks concerning the following question:
Hello everyone
I am trying to setup CDBI for my database and either I am not understanding how a might_have works or I am doing something silly. Let me first describe my table structure: (I can't figure out how to get these tables to look normal sorry)
So I have a one-to-one relationship between the Article table and the Author table via authorId (and a many-to-one relationship from the Author to the Article table). I wanted to implment a might_have relationship so that I could do something like this:|-------------------| |-------------------| | TBL NAME = Article | | TBL NAME = Author | |-------------------| |-------------------| | articleId INT PrimKey | | authorId INT PrimKey| | authorId INT | | articleId INT | | title varchar(255) | | author varchar(32) | |other fields | | other fields | --------------------- ---------------------
$article->author
This is what I wrote (that does not work). Does anybody see my mistake?
package MPDatabase::Article; use strict; use base 'MPDatabase::DBI'; __PACKAGE__->table('article'); __PACKAGE__->columns(Primary => qw/articleId/); __PACKAGE__->columns(All => qw/articleId topicId authorId title intro +body postDateTime/); __PACKAGE__->has_a(topicId => 'MPDatabase::Topic'); __PACKAGE__->has_a(authorId => 'MPDatabase::Author'); __PACKAGE__->might_have( topic_class => 'MPDatabase::Topic' => qw/top +ic relativeImagePath imagePath alt/); __PACKAGE__->might_have( author_class => 'MPDatabase::Author' => qw/au +thor email/); 1;
package MPDatabase::Author; use strict; use base 'MPDatabase::DBI'; __PACKAGE__->table('author'); __PACKAGE__->columns(Primary => qw/authorId/); __PACKAGE__->columns(All => qw/authorId author email/); __PACKAGE__->has_many(articles => 'MPDatabase::Article'); 1;
I also wrote a test program to see if it works:
!/usr/bin/perl -w # use strict; use MPDatabase::Article; use MPDatabase::Author; my $article = MPDatabase::Article->retrieve(articleId => 5); print $article->author, "\n"; # This program does not produce any output
I turned on tracing to see the SQL code being generated and saw this:
Which is not what I expected. I was expecting something like this:<--- parse_params mysql_st_internal_execute Binding parameters: SELECT authorid FROM author WHERE authorid = '5'
SELECT authorId FROM author A, article B WHERE A.authorID = B.authorId and B.articleId = 5
Thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Class::DBI and might_have problem
by jdtoronto (Prior) on Jun 07, 2006 at 19:10 UTC | |
by debiandude (Scribe) on Jun 07, 2006 at 19:36 UTC | |
|
Re: Class::DBI and might_have problem
by bpphillips (Friar) on Jun 07, 2006 at 22:25 UTC | |
|
Re: Class::DBI and might_have problem
by perrin (Chancellor) on Jun 07, 2006 at 19:35 UTC | |
by debiandude (Scribe) on Jun 07, 2006 at 19:40 UTC |