[Dbix-class] Need help with DBIC join table queries from Catalyst app

Dennis Daupert ddaupert at gmail.com
Tue Apr 14 20:55:03 GMT 2009


I am trying to work out queries from my Catalyst app that involve join
tables.

These are no doubt pretty simple once you have worked out the formats.

Yes, I have read thru various docs:
DBIx::Class::Manual::Intro
DBIx::Class::ResultSet
DBIx::Class::Relationship
DBIx::Class::Relationship::Base

I find that in trying to modify documented examples to my situation,
something is just not matching up.

I have several sets of parent / child / join tables in which both the
"parent" (has many and many-to-many) and "child" (belongs to) tables are
managed separately, and only the join tables need to be updated; e.g. ,
user_roles; project_members; project_directories; etc.

Let me paste some bits from three tables, snipped for brevity:

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D
package hde::Schema::Result::Projects;
__PACKAGE__->table("projects");
__PACKAGE__->add_columns(
  "id",   { data_type =3D> "INTEGER", is_nullable =3D> 0, size =3D> undef },
  "rel_tag",   { data_type =3D> "TEXT", is_nullable =3D> 0, size =3D> undef=
 },

__PACKAGE__->has_many(map_project_directory =3D>
'hde::Schema::Result::ProjectDirectories', 'directory_id');
__PACKAGE__->many_to_many(directories =3D> 'map_project_directory',
'directory');

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D
package hde::Schema::Result::Directories;
__PACKAGE__->table("directories");
__PACKAGE__->add_columns(
  "id",   { data_type =3D> "INTEGER", is_nullable =3D> 0, size =3D> undef },
  "directory",   { data_type =3D> "TEXT", is_nullable =3D> 0, size =3D> und=
ef },

__PACKAGE__->has_many(map_project_directory =3D>
'hde::Schema::Result::ProjectDirectories', 'directory_id');

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D
package hde::Schema::Result::ProjectDirectories;
__PACKAGE__->table("project_directories");
__PACKAGE__->add_columns(
  "project_id",   { data_type =3D> "INTEGER", is_nullable =3D> 0, size =3D>=
 undef
},
  "directory_id",   { data_type =3D> "INTEGER", is_nullable =3D> 0, size =
=3D>
undef },
__PACKAGE__->belongs_to(project =3D> 'hde::Schema::Result::Projects',
'project_id');
__PACKAGE__->belongs_to(directory =3D> 'hde::Schema::Result::Directories',
'directory_id');

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D

In Catalyst, how can I get a list of directories assigned to projects?

Documentation says: call the project's many_to_many accessor on the project
object.
In my case, the many_to_many accessor would be 'directories'

An example I've seen says: $directories_rs =3D $projects->directories()

Yet, in Catalyst, nothing I've tried along those lines have paid off.

    # One of many failed attempts:
    my $projects_rs =3D
$c->model('HdeDB::Projects')->find($selected_project_id);
      my $dirs_rs =3D $projects_rs->directories();
      while (my $dir_obj =3D $dirs_rs->next) {
        my $id =3D $dir_obj->id;
        my $dir_name =3D $dir_obj->directory;
        $c->log->debug("ID: $id -- Dir Name: $dir_name");
      }

Thanks for reading this far. Any help greatly appreciated.

/dennis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20090414/0b5=
9edbf/attachment.htm


More information about the DBIx-Class mailing list