http://www.perlmonks.org?node_id=711057

uG has asked for the wisdom of the Perl Monks concerning the following question:

Hello again monks! I want to join 2 tables where id1 OR id2 = nation.id However, i'm getting a
Can't handle this yet
error when I call
my $resource_rs = $nation_rs->trades->search_related('nation')->search +_related('resources');
I am implementing this wrong, or is this something dbix really can't do yet? (Relevant Schema's below)
package Game::DB::Schema::Nation; __PACKAGE__->has_many( trades => 'Game::DB::Schema::Trades', [ 'foreign.id1' => 'self.id', 'foreign.id2' => 'self.id', ], ); package Game::DB::Schema:Trades; __PACKAGE__->belongs_to( nation => 'Game::DB::Schema::Nation', [ 'foreign.id' => 'self.id1', 'foreign.id' => 'self.id2', ], );

Replies are listed 'Best First'.
Re: dbix: Can't handle this yet error (joining using or?)
by juster (Friar) on Sep 13, 2008 at 10:17 UTC

    You are using the wrong backets for your last arguments to has_many and belongs_to. Using [ ] creates an array reference and the docs ask for a hash reference ({ }).

    You can also save typing by using the accessor methods instead of using empty searche_relateds like so:

    my $resource_rs = $nation_rs->trades->nation->resources;

    Assuming there is a "resources" relation inside your nation class that you are omitting.

    After posting the above and rereading your post I found what may be a problem with the schema. I'm not sure if a belongs_to relationship can be used to give multiple results. At least when I have used it, and in all examples I've found, belongs_to is used to return the owner instance (row) of the owner class (table).

    Told another way, the nation has many trades. Sure that's nice. But can the trade belong to many nations and have more than one owner? I would't think so, at least not in the context of DB relationships.

    I would imagine a trade has many nations making this a many to many relationship but I can't think of how to preserve who traded what in what direction. Maybe someone with more DB experience can help where I've fallen short. Assuming, of course, that this is even an error!

      Preserving the direction isn't important (yet). id1 will always be the one to have 'created' the trade, so if I check the nation's trade->id1 against user.id I can figure out the direction. Although after switching the brackets to curley brackets (how I missed this I dont know haha) I get this instead:
      "Can't locate object method "nation" via package "DBIx::Class::ResultS +et"
      'nation' is defined via a belongs_to in the Trades schema. This makes me think that you are correct about not being able to use belongs_to for more than one owner.

        After getting some sleep I realized the most obvious answer to the problem would be to have each Trade belong to two Nations.

        package Game::DB::Schema:Trades; __PACKAGE__->belongs_to( creator => 'Game::DB::Schema::Nation', { 'foreign.id' => 'self.id1', }, ); __PACKAGE__->belongs_to( acceptor => 'Game::DB::Schema::Nation', { 'foreign.id' => 'self.id2', }, );

        At the top of DBIx::Class::Relationship in the Synopsis there is even an example of this showing a many-to-many relationship which you could probably even implement if you wanted to.

Re: dbix: Can't handle this yet error (joining using or?)
by Anonymous Monk on Sep 13, 2008 at 10:22 UTC
    DBIx is not a module