Skip to content

Commit 37d5774

Browse files
committed
Adjust builder() logic
1 parent 5e24c9f commit 37d5774

2 files changed

Lines changed: 52 additions & 22 deletions

File tree

system/Model.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,12 +1394,19 @@ public function protect(bool $protect = true)
13941394
* @param string $table
13951395
*
13961396
* @return BaseBuilder
1397-
* @throws ModelException ;
1397+
* @throws ModelException
13981398
*/
1399-
protected function builder(string $table = null)
1399+
public function builder(string $table = null)
14001400
{
1401+
// Check for an existing Builder
14011402
if ($this->builder instanceof BaseBuilder)
14021403
{
1404+
// Make sure the requested table matches the builder
1405+
if ($table && $this->builder->getTable() !== $table)
1406+
{
1407+
return $this->db->table($table);
1408+
}
1409+
14031410
return $this->builder;
14041411
}
14051412

@@ -1419,9 +1426,15 @@ protected function builder(string $table = null)
14191426
$this->db = Database::connect($this->DBGroup);
14201427
}
14211428

1422-
$this->builder = $this->db->table($table);
1429+
$builder = $this->db->table($table);
1430+
1431+
// Only consider it "shared" if the table is correct
1432+
if ($table === $this->table)
1433+
{
1434+
$this->builder = $builder;
1435+
}
14231436

1424-
return $this->builder;
1437+
return $builder;
14251438
}
14261439

14271440
/**
@@ -1973,10 +1986,7 @@ public function __call(string $name, array $params)
19731986
$result = $builder->{$name}(...$params);
19741987
}
19751988

1976-
// Don't return the builder object unless specifically requested
1977-
//, since that will interrupt the usability flow
1978-
// and break intermingling of model and builder methods.
1979-
if ($name !== 'builder' && empty($result))
1989+
if (empty($result))
19801990
{
19811991
if (! method_exists($this->builder(), $name))
19821992
{
@@ -1988,7 +1998,7 @@ public function __call(string $name, array $params)
19881998
return $result;
19891999
}
19902000

1991-
if ($name !== 'builder' && ! $result instanceof BaseBuilder)
2001+
if (! $result instanceof BaseBuilder)
19922002
{
19932003
return $result;
19942004
}

tests/system/Database/Live/ModelTest.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,19 +2487,6 @@ public function testUndefinedModelMethod()
24872487
$model->undefinedMethodCall();
24882488
}
24892489

2490-
public function testUndefinedMethodInBuilder()
2491-
{
2492-
$model = new JobModel($this->db);
2493-
2494-
$model->find(1);
2495-
2496-
$this->expectException(BadMethodCallException::class);
2497-
$this->expectExceptionMessage('Call to undefined method Tests\Support\Models\JobModel::getBindings');
2498-
2499-
$binds = $model->builder()
2500-
->getBindings();
2501-
}
2502-
25032490
/**
25042491
* @dataProvider provideAggregateAndGroupBy
25052492
*/
@@ -2643,4 +2630,37 @@ public function testSetAllowedFields()
26432630
$model->setAllowedFields($allowed2);
26442631
$this->assertSame($allowed2, $this->getPrivateProperty($model, 'allowedFields'));
26452632
}
2633+
2634+
//--------------------------------------------------------------------
2635+
2636+
public function testBuilderUsesModelTable()
2637+
{
2638+
$model = new UserModel($this->db);
2639+
$builder = $model->builder();
2640+
2641+
$this->assertEquals('user', $builder->getTable());
2642+
}
2643+
2644+
public function testBuilderRespectsTableParameter()
2645+
{
2646+
$model = new UserModel($this->db);
2647+
$builder1 = $model->builder('jobs');
2648+
$builder2 = $model->builder();
2649+
2650+
$this->assertEquals('jobs', $builder1->getTable());
2651+
$this->assertEquals('user', $builder2->getTable());
2652+
}
2653+
2654+
public function testBuilderWithParameterIgnoresShared()
2655+
{
2656+
$model = new UserModel($this->db);
2657+
2658+
$builder1 = $model->builder();
2659+
$builder2 = $model->builder('jobs');
2660+
$builder3 = $model->builder();
2661+
2662+
$this->assertEquals('user', $builder1->getTable());
2663+
$this->assertEquals('jobs', $builder2->getTable());
2664+
$this->assertEquals('user', $builder3->getTable());
2665+
}
26462666
}

0 commit comments

Comments
 (0)