Skip to content

Commit

Permalink
Strip schema from table for HasMany::their_name guess (#1002)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsouthardjr committed May 31, 2022
1 parent 07b1c72 commit 0401814
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Reference/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getTheirFieldName(Model $theirModel = null): string
// this is pure guess, verify if such field exist, otherwise throw
// TODO probably remove completely in the future
$ourModel = $this->getOurModel(null);
$theirFieldName = $this->getModelTableString($ourModel) . '_' . $ourModel->id_field;
$theirFieldName = preg_replace('~^.+?\.~', '', $this->getModelTableString($ourModel)) . '_' . $ourModel->id_field;
if (!$this->createTheirModel()->hasField($theirFieldName)) {
throw (new Exception('Their model does not contain fallback field'))
->addMoreInfo('their_fallback_field', $theirFieldName);
Expand Down
10 changes: 10 additions & 0 deletions tests/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@ public function testCustomRef(): void

$this->assertSame('user_archive', $m->ref('archive')->table);
}

public function testOtherDbFieldRef(): void
{
$user = new Model($this->db, ['table' => 'thisdb.user']);
$order = new Model($this->db, ['table' => 'otherdb.orders']);
$order->addField('user_id');

$user->hasMany('Orders', ['model' => $order, 'caption' => 'My Orders']);
$this->assertSame($user->getRef('Orders')->getTheirFieldName(), 'user_id');
}
}

0 comments on commit 0401814

Please sign in to comment.