Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello Dear Monks

I'm having some trouble setting up a has_many relationship between 2 entities - a page entity and a column entity. The relationship I'm tring to use in my code is 'a page has many columns'.

If I structure the tables in the following way, I'm facing no problems.

Table page
page_id page_title
1 Geography
2 History
Table column
column_id column_title page_id
1 Geog 1 1
2 Geog 2 1
3 Geog 3 1
4 Hist 1 2

I set up all the DBI modules, etc... and in Page.pm I add a line:

__PACKAGE__->has_many(cols => 'Column');

Now I can get all columns of a page with the following code:

$columns = Page->retrieve(1)->cols;

Now good database design, has taught me that when one has a 1:N relationship, one should construct a new table to hold that relationship.
So, the foll structure:
Table page
page_id page_title
1 Geography
2 History
Table page_column
page_id column_id
1 1
1 2
1 3
2 4
Table column
column_id column_title
1 Geog 1
2 Geog 2
3 Geog 3
4 Hist 1

So now how do I code the relationships in my Class::DBI modules which would enable me to get all the columns of a page in the same was as above.
i.e.

$columns = Page->retrieve(1)->cols;

Thankz in advance for all suggestions and help and compliments of the season to all of you!

update:
Changed the 'columns' method to 'cols' since Class::DBI already has a 'columns' method
- Thanks to PodMaster


In reply to Class::DBI has_many by arc_of_descent

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 07:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found