Skip to content

Commit 9d5195e

Browse files
committed
attach by child namespace
Signed-off-by: Derek Smart <derek@grindaga.com>
1 parent 85a7c39 commit 9d5195e

File tree

6 files changed

+15
-13
lines changed

6 files changed

+15
-13
lines changed

src/Models/Model.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ protected function attachByMethod(string $method)
9797
{
9898
if ($this->$method) {
9999
foreach ($this->$method as $attachModel) {
100-
$attachModel = __NAMESPACE__ . '\\' . $attachModel;
100+
$class = new \ReflectionClass($this);
101+
$attachModel = $class->getNamespaceName() . '\\' . $attachModel;
101102
$attach = new $attachModel($this->mysql);
102103
foreach ($this->data as $key => $model) {
103104
switch ($method) {
@@ -136,7 +137,8 @@ protected function attachHABTM()
136137
{
137138
if ($this->hasAndBelongsToMany) {
138139
foreach ($this->hasAndBelongsToMany as $habtmModel) {
139-
$habtmModel = __NAMESPACE__ . '\\' . $habtmModel;
140+
$class = new \ReflectionClass($this);
141+
$habtmModel = $class->getNamespaceName() . '\\' . $habtmModel;
140142
$habtm = new $habtmModel($this->mysql);
141143
foreach ($this->data as $key => $model) {
142144
$alias = $habtm->table . ' ' . $habtm->model;

tests/mocks/Pipe.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
namespace PHPRed\Models;
2+
namespace Custom;
33

4-
class Pipe extends Model
4+
class Pipe extends \PHPRed\Models\Model
55
{
66
public function __construct(\MysqliDb $mysql)
77
{

tests/mocks/PipeUser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
namespace PHPRed\Models;
2+
namespace Custom;
33

4-
class PipeUser extends Model
4+
class PipeUser extends \PHPRed\Models\Model
55
{
66
public function __construct(\MysqliDb $mysql)
77
{

tests/mocks/Service.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
namespace PHPRed\Models;
2+
namespace Custom;
33

4-
class Service extends Model
4+
class Service extends \PHPRed\Models\Model
55
{
66
public function __construct(\MysqliDb $mysql)
77
{

tests/mocks/User.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
2-
namespace PHPRed\Models;
2+
namespace Custom;
33

4-
class User extends Model
4+
class User extends \PHPRed\Models\Model
55
{
66
public function __construct(\MysqliDb $mysql)
77
{

tests/unit/PHPRed/ModelTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public function setup()
5757
]];
5858
$this->prophet = new Prophecy\Prophet;
5959
$this->mysql = $this->prophet->prophesize("\MysqliDb");
60-
$this->pipe = new Pipe($this->mysql->reveal());
61-
$this->pipeUser = new PipeUser($this->mysql->reveal());
60+
$this->pipe = new \Custom\Pipe($this->mysql->reveal());
61+
$this->pipeUser = new \Custom\PipeUser($this->mysql->reveal());
6262
}
6363

6464
public function testCanInstantiate()
6565
{
66-
$this->assertInstanceOf(Pipe::class, $this->pipe);
66+
$this->assertInstanceOf(\Custom\Pipe::class, $this->pipe);
6767
}
6868

6969
public function testCanGetAll()

0 commit comments

Comments
 (0)