Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

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

Trying to get to grips with references and joins in DBIx

Let us assume I have the following three Schema classes set up:

A web-based service:

---- file 1 ---- package ORI::Schema::Result::Repo; extends 'DBIx::Class::Core'; __PACKAGE__->table("repo"); __PACKAGE__->add_columns( "id" => { data_type => "integer" }, "system_url" => { data_type => "text" }, "oaibaseurl" => { data_type => "text" }, "lat" => { data_type => "real" }, "long" => { data_type => "real" }, "mandate" => { data_type => "boolean" }, "fulltext" => { data_type => "boolean" }, "openaccess" => { data_type => "boolean" }, ); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( "contentlinks" => "ORI::Schema::Result::Contentlink", { "foreign.repo_id" => "self.id" }, ); __PACKAGE__->many_to_many("contents", "contentlinks", "content");

A list of types of content the service deals with (can be multiple data types):

---- file 2 ---- package ORI::Schema::Result::Contenttype; extends 'DBIx::Class::Core'; __PACKAGE__->table("contenttypes"); __PACKAGE__->add_columns( "id" => { data_type => "integer" }, "text" => { data_type => "text" }, ); __PACKAGE__->set_primary_key("id"); _PACKAGE__->has_many( "contentlinks" => "ORI::Schema::Result::Contentlink", { "foreign.content_id" => "self.id" }, ); __PACKAGE__->many_to_many("repos", "contentlinks", "repo");

Finally, a table to link web services to data types:

---- file 3 ---- package ORI::Schema::Result::Contentlink; extends 'DBIx::Class::Core'; _PACKAGE__->table("contentlinks"); __PACKAGE__->add_columns( "content_id" => { data_type => "integer" }, "repo_id" => { data_type => "integer" }, ); __PACKAGE__->set_primary_key("content_id", "repo_id"); __PACKAGE__->belongs_to( "content" => "ORI::Schema::Result::Contenttype", { id => "content_id" }, ); __PACKAGE__->belongs_to( "repo" => "ORI::Schema::Result::Repo", { id => "repo_id" }, );

So, in code (a Catalyst Controller, in my case), I can get the ORI::Schema::Result::Repo values using the Template Toolkit:

---- within controller ---- # Get the first 10 results (getting all crashes the system! ) $c->stash( repos => [ $c->model('ORIdatabase::Repo')->search( {} { rows => '10' } ) ] ); ---- In Template ---- [% FOREACH repo IN repos -%] <tr> <td>[% repo.oaibaseurl %]</td> <td>[% repo.system_url %]</td> <td>[% repo.lat %]</td> <td>[% repo.long %]</td> <td>[% repo.opstatus %]</td> <td>[% repo.fulltext %]</td> <td>[% repo.mandate %]</td> [% END -%]

.... but how do I get a list of all the data types the web-service is linked to?

---- within controller ---- # Get the first 10 results (getting all crashes the system! ) # Add in a join to get all the contents (ManyToMany relationship) $c->stash( repos => [ $c->model('ORIdatabase::Repo')->search( {} { join => 'contents', prefetch => 'contents', rows => '10' } ) ] ); ---- In Template ---- [% FOREACH repo IN repos -%] <tr> <td>[% repo.oaibaseurl %]</td> <td>[% repo.system_url %]</td> <td>[% repo.lat %]</td> <td>[% repo.long %]</td> <td>[% repo.opstatus %]</td> <td>[% repo.fulltext %]</td> <td>[% repo.mandate %]</td> <td>[% repo.mandate %]</td> <td> [% # How to I get the list of content types? -%] </td> [% END -%]

.... however this barfs! Using the 'content' or 'contents' relationships fail, and 'contentlinks' just goes down 1 level!!

How do I get the list of text fields from ORI::Schema::Result::Contenttype, where they are linked by ORI::Schema::Result::Contentlink?



-- Ian Stuart
A man depriving some poor village, somewhere, of a first-class idiot.

In reply to DBIx and ManyToMany Relationships by kiz

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 examining the Monastery: (3)
As of 2024-04-24 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found