Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5680b61

Browse files
[12.x] Allow enums on model connection property and methods (#56896)
* Allow setting connection by enum value in Model * Add basic tests for connection enums in models * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 42076ce commit 5680b61

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/Illuminate/Database/Eloquent/Factories/Factory.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Illuminate\Support\Traits\ForwardsCalls;
1717
use Illuminate\Support\Traits\Macroable;
1818
use Throwable;
19+
use UnitEnum;
20+
21+
use function Illuminate\Support\enum_value;
1922

2023
/**
2124
* @template TModel of \Illuminate\Database\Eloquent\Model
@@ -101,7 +104,7 @@ abstract class Factory
101104
/**
102105
* The name of the database connection that will be used to create the models.
103106
*
104-
* @var string|null
107+
* @var \UnitEnum|string|null
105108
*/
106109
protected $connection;
107110

@@ -156,7 +159,7 @@ abstract class Factory
156159
* @param \Illuminate\Support\Collection|null $for
157160
* @param \Illuminate\Support\Collection|null $afterMaking
158161
* @param \Illuminate\Support\Collection|null $afterCreating
159-
* @param string|null $connection
162+
* @param \UnitEnum|string|null $connection
160163
* @param \Illuminate\Support\Collection|null $recycle
161164
* @param bool|null $expandRelationships
162165
* @param array $excludeRelationships
@@ -802,16 +805,16 @@ public function withoutParents($parents = [])
802805
*/
803806
public function getConnectionName()
804807
{
805-
return $this->connection;
808+
return enum_value($this->connection);
806809
}
807810

808811
/**
809812
* Specify the database connection that should be used to generate models.
810813
*
811-
* @param string $connection
814+
* @param \UnitEnum|string $connection
812815
* @return static
813816
*/
814-
public function connection(string $connection)
817+
public function connection(UnitEnum|string $connection)
815818
{
816819
return $this->newInstance(['connection' => $connection]);
817820
}

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
use ReflectionMethod;
3434
use Stringable;
3535

36+
use function Illuminate\Support\enum_value;
37+
3638
abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToString, HasBroadcastChannel, Jsonable, JsonSerializable, QueueableEntity, Stringable, UrlRoutable
3739
{
3840
use Concerns\HasAttributes,
@@ -52,7 +54,7 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
5254
/**
5355
* The connection name for the model.
5456
*
55-
* @var string|null
57+
* @var \UnitEnum|string|null
5658
*/
5759
protected $connection;
5860

@@ -717,7 +719,7 @@ public function newInstance($attributes = [], $exists = false)
717719
* Create a new model instance that is existing.
718720
*
719721
* @param array<string, mixed> $attributes
720-
* @param string|null $connection
722+
* @param \UnitEnum|string|null $connection
721723
* @return static
722724
*/
723725
public function newFromBuilder($attributes = [], $connection = null)
@@ -726,7 +728,7 @@ public function newFromBuilder($attributes = [], $connection = null)
726728

727729
$model->setRawAttributes((array) $attributes, true);
728730

729-
$model->setConnection($connection ?: $this->getConnectionName());
731+
$model->setConnection($connection ?? $this->getConnectionName());
730732

731733
$model->fireModelEvent('retrieved', false);
732734

@@ -736,7 +738,7 @@ public function newFromBuilder($attributes = [], $connection = null)
736738
/**
737739
* Begin querying the model on a given connection.
738740
*
739-
* @param string|null $connection
741+
* @param \UnitEnum|string|null $connection
740742
* @return \Illuminate\Database\Eloquent\Builder<static>
741743
*/
742744
public static function on($connection = null)
@@ -1951,13 +1953,13 @@ public function getConnection()
19511953
*/
19521954
public function getConnectionName()
19531955
{
1954-
return $this->connection;
1956+
return enum_value($this->connection);
19551957
}
19561958

19571959
/**
19581960
* Set the connection associated with the model.
19591961
*
1960-
* @param string|null $name
1962+
* @param \UnitEnum|string|null $name
19611963
* @return $this
19621964
*/
19631965
public function setConnection($name)
@@ -1970,7 +1972,7 @@ public function setConnection($name)
19701972
/**
19711973
* Resolve a connection instance.
19721974
*
1973-
* @param string|null $connection
1975+
* @param \UnitEnum|string|null $connection
19741976
* @return \Illuminate\Database\Connection
19751977
*/
19761978
public static function resolveConnection($connection = null)

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
use InvalidArgumentException;
5757
use LogicException;
5858
use Mockery as m;
59+
use PHPUnit\Framework\Attributes\TestWith;
5960
use PHPUnit\Framework\TestCase;
6061
use ReflectionClass;
6162
use stdClass;
@@ -1317,6 +1318,23 @@ public function testConnectionManagement()
13171318
$this->assertSame('bar', $model->getConnection());
13181319
}
13191320

1321+
#[TestWith(['Foo'])]
1322+
#[TestWith([ConnectionName::Foo])]
1323+
#[TestWith([ConnectionNameBacked::Foo])]
1324+
public function testConnectionEnums(string|\UnitEnum $connectionName)
1325+
{
1326+
EloquentModelStub::setConnectionResolver($resolver = m::mock(ConnectionResolverInterface::class));
1327+
$model = new EloquentModelStub;
1328+
1329+
$retval = $model->setConnection($connectionName);
1330+
$this->assertEquals($retval, $model);
1331+
$this->assertSame('Foo', $model->getConnectionName());
1332+
1333+
$resolver->shouldReceive('connection')->once()->with('Foo')->andReturn('bar');
1334+
1335+
$this->assertSame('bar', $model->getConnection());
1336+
}
1337+
13201338
public function testToArray()
13211339
{
13221340
$model = new EloquentModelStub;
@@ -4432,3 +4450,15 @@ public function __toString()
44324450
return $this->cast;
44334451
}
44344452
}
4453+
4454+
enum ConnectionName
4455+
{
4456+
case Foo;
4457+
case Bar;
4458+
}
4459+
4460+
enum ConnectionNameBacked: string
4461+
{
4462+
case Foo = 'Foo';
4463+
case Bar = 'Bar';
4464+
}

0 commit comments

Comments
 (0)