<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
    <title></title>
  </head>
  <body bgcolor="#ffffff" text="#000000">
    Le 09/12/2010 09:37, linuxsupport a &eacute;crit&nbsp;:
    <blockquote
      cite="mid:AANLkTi=opDSwh+ueL71HLN2-9z8umbdSZOHBn=CrLKhk@mail.gmail.com"
      type="cite">I have 3 tables, users, user_cat, and cat, table
      structure and relationship are setup as follows.<br>
      User.pm<br>
      __PACKAGE__-&gt;add_columns(<br>
      &nbsp; "id", { data_type =&gt; "integer", is_nullable =&gt; 0 },<br>
      &nbsp; "username",{ data_type =&gt; "text", is_nullable =&gt; 1 },<br>
      &nbsp; "password",&nbsp; { data_type =&gt; "text", is_nullable =&gt; 1 },<br>
      &nbsp; "email_address",&nbsp; { data_type =&gt; "text", is_nullable =&gt; 1
      },<br>
      &nbsp; "first_name",&nbsp; { data_type =&gt; "text", is_nullable =&gt; 1 },<br>
      &nbsp; "last_name",&nbsp; { data_type =&gt; "text", is_nullable =&gt; 1 },<br>
      &nbsp; "active",&nbsp; { data_type =&gt; "integer", is_nullable =&gt; 1 },<br>
      );<br>
      __PACKAGE__-&gt;set_primary_key("id");<br>
      <div id=":u0">__PACKAGE__-&gt;has_many("usercats",
        "Example::Schema::Result::UserCat",{ "foreign.user_id" =&gt; "<a
          moz-do-not-send="true" href="http://self.id/" target="_blank">self.id</a>"
        },);<br>
        __PACKAGE__-&gt;many_to_many(cats =&gt; 'usercats', 'cat');<br>
        <br>
        UserCat.pm<br>
        __PACKAGE__-&gt;add_columns(<br>
        &nbsp; "user_id",&nbsp; { data_type =&gt; "integer", is_nullable =&gt; 0
        },<br>
        &nbsp; "cat_id",&nbsp; { data_type =&gt; "integer", default_value =&gt; 0,
        is_nullable =&gt; 0 },<br>
        );<br>
        __PACKAGE__-&gt;set_primary_key("user_id", "cat_id");<br>
        __PACKAGE__-&gt;belongs_to("user",
        "Example::Schema::Result::User", { id =&gt; "user_id" },{
        join_type =&gt; "LEFT" },);<br>
        __PACKAGE__-&gt;belongs_to("cat",
        "Example::Schema::Result::Cat", { id =&gt; "cat_id" },{
        join_type =&gt; "LEFT" },);<br>
        <br>
        Cat.pm<br>
        __PACKAGE__-&gt;add_columns(<br>
        &nbsp; "id",&nbsp; { data_type =&gt; "integer", is_nullable =&gt; 0 },<br>
        &nbsp; "cat_name",&nbsp; { data_type =&gt; "text", is_nullable =&gt; 0 },<br>
        &nbsp;"parent_id",&nbsp; { data_type =&gt; "integer", is_nullable =&gt; 0
        },<br>
        );<br>
        __PACKAGE__-&gt;set_primary_key("id","parent_id");<br>
        __PACKAGE__-&gt;has_many("usercat","Example::Schema::Result::UserCat",{
        "foreign.cat_id" =&gt; "<a moz-do-not-send="true"
          href="http://self.id/" target="_blank">self.id</a>" },);<br>
        __PACKAGE__&gt;many_to_many("allcat", "usercat', "cat");<br>
        <br>
        I am able to retrieve all users in any particular category using
        "allcat" many_to_many relationship.<br>
      </div>
    </blockquote>
    Are you, really ?<br>
    The navigation path you indicate in your "allcat" many_to_many
    relationship does not seem to lead from Cat to Users but from Cat to
    Cat itself, which seems of no practical use.<br>
    <br>
    I rather would have written:<br>
    __PACKAGE__-&gt;many_to_many("users","usercat","user");<br>
    <br>
    So that I can get the users in any category this way:<br>
    my $cat = $schema-&gt;resultset("Cat)-&gt;find($my_cat_id);<br>
    my @users_belonging_directly_to_that_category = $cat-&gt;users;<br>
    <br>
    <blockquote
      cite="mid:AANLkTi=opDSwh+ueL71HLN2-9z8umbdSZOHBn=CrLKhk@mail.gmail.com"
      type="cite">
      <div id=":u0">In cat table I have both category and sub category,
        if a row have parent_id &gt; 0 then it is sub category.<br>
        How can I get all users belong to one category and its sub
        categories? </div>
    </blockquote>
    Now to get recursively all users belonging to this category AND all
    sub-categories is another problem.<br>
    <br>
    The procedural/Perlish/DBIx::Classish solution would be:<br>
    # first, add to Cat.pm an accessor to the sub-categories<br>
    __PACKAGE__-&gt;has_many("subcats",""Example::Schema::Result::Cat",{
    "foreign.parent_id" =&gt; "<a moz-do-not-send="true"
      href="http://self.id/" target="_blank">self.id</a>" });<br>
    <br>
    # then, in your program:<br>
    my $cat = $schema-&gt;resultset("Cat)-&gt;find($my_cat_id);<br>
    my @users_belonging_to_that_category_and_its_subcats =
    $cat-&gt;users;<br>
    <br>
    my @subcats = $cat-&gt;subcats;<br>
    for my $subcat (@subcats)<br>
    &nbsp;&nbsp;&nbsp; push @users_belonging_to_that_category_and_its_subcats ,
    $cat-&gt;users;<br>
    }<br>
    # assuming the reflexive relationship is one level only<br>
    # i.e. no sub-sub-..etc..-categories<br>
    <br>
    <br>
    A (better in my -humble- opinion) solution would be more
    SQL-oriented (after all, isn't the declarative SQL way the reason
    why we all use RDBMSes ?)<br>
    <br>
    First of all, create a view giving what you want:<br>
    <br>
    create view users_by_cat_and_subcats as<br>
    select cat.id cat_id, user.*<br>
    &nbsp; from cat join usercat on usercat.cat_id = cat.id<br>
    &nbsp; join user on user.id = usercat.user_id<br>
    union select parent.cat.id cat_id , user.*<br>
    &nbsp; from cat parent_cat join cat child_cat on child_cat.parent_id =
    parent_cat.cat_id<br>
    &nbsp; join usercat on usercat.cat_id = child_cat.id<br>
    &nbsp; join user on user.id = usercat.user_id ;<br>
    <br>
    Then create (automatically, by make_schema_at) a
    UsersByCatAndSubcat.pm to map this table to a DBIx::Class and just
    use it:<br>
    my @users_belonging_to_that_category_and_its_subcats = <br>
    &nbsp;&nbsp; $schema-&gt;resultset('UsersByCatAndSubcat')-&gt;search( { cat_id
    =&gt; $my_category } )-&gt;all;<br>
    <br>
    It's always difficult to use the right tool, and I've seen ugly
    things like simple left joins replaced by 15 lines of while, if
    defined, push,etc...<br>
    RDBMSes have dominated our domain for nearly forty years for a
    reason, which is the expressive power of SQL, itself based on strong
    theoretical mathematical background.<br>
    My advice is: don't forget you have a RDBMS, and thus the full power
    of SQL, at your disposal.<br>
    <br>
    Sorry for this answer to a slightly off-catalyst-topic question; I
    will subscribe to the DBIx::Class list right away.<br>
    <pre class="moz-signature" cols="72">-- 
Emmanuel Otton - Ecole des mines d'Albi-Carmaux - d&eacute;partement informatique - T&eacute;l: 05 63 49 30 86</pre>
  </body>
</html>