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)

|-------------------| |-------------------| | 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 | --------------------- ---------------------
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:
$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:

<--- parse_params mysql_st_internal_execute Binding parameters: SELECT authorid FROM author WHERE authorid = '5'
Which is not what I expected. I was expecting something like this:
SELECT authorId FROM author A, article B WHERE A.authorID = B.authorId and B.articleId = 5

Thanks in advance.


In reply to Class::DBI and might_have problem by debiandude

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.