I've come across an interesting scenario with Class::DBI. Let's say you have a list of authors, but those authors can also have any number of aliases.
You might think to set things up like so: (Note: if I want author 2 to be an alias for author 1 and vice versa i need to have two rows in the alias table)
package My::Author;
use base qw( My::DBI );
__PACKAGE__->table( 'my_authors' );
__PACKAGE__->columns( Primary => qw( author_id ) );
__PACKAGE__->columns( Essential => qw( first_name last_name ) );
__PACKAGE__->has_many( aliases => [ 'My::AuthorAlias' => 'alias' ] );
package My::AuthorAlias;
use base qw( My::DBI );
__PACKAGE__->table( 'my_author_alias' );
__PACKAGE__->columns( Primary => qw( alias_id ) );
__PACKAGE__->columns( Essential => qw( author alias ) );
__PACKAGE__->has_a( author => 'My::Author' );
__PACKAGE__->has_a( alias => 'My::Author' );
1;
But, it seems that Class::DBI gets confused with two has_a relationships pointing to the same table. So, the aliases relationship doesn't work.
My current work-around is to use might_have on the non-primary column:
package My::AuthorAlias;
use base qw( My::DBI );
__PACKAGE__->table( 'my_author_alias' );
__PACKAGE__->columns( Primary => qw( alias_id ) );
__PACKAGE__->columns( Essential => qw( author ) );
__PACKAGE__->has_a( author => 'My::Author' );
__PACKAGE__->might_have( alias => 'My::Author' );
1;
From a simple test, it seems okay. Am I missing an easier alternative?
What about table_alias? (Score:1)
Had you tried using the table_alias() [cpan.org] (or also the second param to table()) on the AuthorAlias class?
I would summize that using table_alis on the second package would un-confuse CDBI.
Re:What about table_alias? (Score:1)
I'm failing to see how
table_alias()will help in this situation, but, perhaps I'm being dense. Could you explain it further?Thanks!
Re:What about table_alias? (Score:1)
Could you post your schema for those two tables as well as a couple rows of sample data? I'd like to tinker.
Cross-memories (Score:1)
So I went to see the logs...
It was you :-)
thanks (Score:1)
Bill
# I had a sig when sigs were cool
use Sig;