Skip to content

Commit

Permalink
use relationship name as alias name in belongs_to relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
und3f committed Aug 31, 2011
1 parent 08e18df commit e4d1039
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
5 changes: 4 additions & 1 deletion lib/ObjectDB/Finder.pm
Expand Up @@ -517,10 +517,13 @@ sub _resolve_with {

# Add source before resolving children to get correct order
# Add where constraint as join args
$source = $rel->to_source($args->{where}, $alias_prefix);
$source =
$rel->to_source($args->{where}, $alias_prefix,
$args->{parent_name});
$sql->add_source($source);

if (my $subwith = $args->{nested}) {
$subwith->[1]->{parent_name} = $source->{as} if @$subwith;
_execute_code_ref($code_ref, $rel->foreign_class,
$subwith, $rel_chain, $table_chain, $args);
}
Expand Down
11 changes: 2 additions & 9 deletions lib/ObjectDB/Relationship/BelongsTo.pm
Expand Up @@ -32,18 +32,11 @@ sub to_source {
my $self = shift;
my $passed_join_args = shift;
my $alias_prefix = shift || '';
my $table = shift || $self->table;

my $table = $self->table;
my $foreign_table = $self->foreign_table;


my $as;
if ($foreign_table eq $table) {
$as = $alias_prefix . $self->name;
}
else {
$as = $alias_prefix . $foreign_table;
}
my $as = $alias_prefix . $self->name;

my $constraint;

Expand Down
4 changes: 2 additions & 2 deletions t/relationship/belongs-to-one.t
Expand Up @@ -29,9 +29,9 @@ is($rel->foreign_table, 'authors');
is_deeply(
$rel->to_source,
{ name => 'authors',
as => 'authors',
as => 'author',
join => 'left',
constraint => ['authors.id' => \'`author_admins`.`author_id`']
constraint => ['author.id' => \'`author_admins`.`author_id`']
}
);

6 changes: 3 additions & 3 deletions t/relationship/belongs-to.t
Expand Up @@ -30,11 +30,11 @@ is($rel->foreign_table, 'authors');
is_deeply(
$rel->to_source,
{ name => 'authors',
as => 'authors',
as => 'author',
join => 'left',
constraint => [
'authors.id' => \'`articles`.`author_id`',
'authors.title' => \'foo'
'author.id' => \'`articles`.`author_id`',
'author.title' => \'foo'
]
}
);

0 comments on commit e4d1039

Please sign in to comment.