Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Illuminate/Database/Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use Illuminate\Support\Traits\ForwardsCalls;
use Illuminate\Support\Traits\Macroable;
use Throwable;
use UnitEnum;

use function Illuminate\Support\enum_value;

/**
* @template TModel of \Illuminate\Database\Eloquent\Model
Expand Down Expand Up @@ -101,7 +104,7 @@ abstract class Factory
/**
* The name of the database connection that will be used to create the models.
*
* @var string|null
* @var \UnitEnum|string|null
*/
protected $connection;

Expand Down Expand Up @@ -156,7 +159,7 @@ abstract class Factory
* @param \Illuminate\Support\Collection|null $for
* @param \Illuminate\Support\Collection|null $afterMaking
* @param \Illuminate\Support\Collection|null $afterCreating
* @param string|null $connection
* @param \UnitEnum|string|null $connection
* @param \Illuminate\Support\Collection|null $recycle
* @param bool|null $expandRelationships
* @param array $excludeRelationships
Expand Down Expand Up @@ -802,16 +805,16 @@ public function withoutParents($parents = [])
*/
public function getConnectionName()
{
return $this->connection;
return enum_value($this->connection);
}

/**
* Specify the database connection that should be used to generate models.
*
* @param string $connection
* @param \UnitEnum|string $connection
* @return static
*/
public function connection(string $connection)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is backwards incompatible change for classes extending this class and overriding this method with a typed signature, as they would have to update theirs. I'm not sure if such cases are considered to be breaking changes; if so I can port just this change to master if preferred.

public function connection(UnitEnum|string $connection)
{
return $this->newInstance(['connection' => $connection]);
}
Expand Down
16 changes: 9 additions & 7 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
use ReflectionMethod;
use Stringable;

use function Illuminate\Support\enum_value;

abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToString, HasBroadcastChannel, Jsonable, JsonSerializable, QueueableEntity, Stringable, UrlRoutable
{
use Concerns\HasAttributes,
Expand All @@ -52,7 +54,7 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
/**
* The connection name for the model.
*
* @var string|null
* @var \UnitEnum|string|null
*/
protected $connection;

Expand Down Expand Up @@ -717,7 +719,7 @@ public function newInstance($attributes = [], $exists = false)
* Create a new model instance that is existing.
*
* @param array<string, mixed> $attributes
* @param string|null $connection
* @param \UnitEnum|string|null $connection
* @return static
*/
public function newFromBuilder($attributes = [], $connection = null)
Expand All @@ -726,7 +728,7 @@ public function newFromBuilder($attributes = [], $connection = null)

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

$model->setConnection($connection ?: $this->getConnectionName());
$model->setConnection($connection ?? $this->getConnectionName());

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

Expand All @@ -736,7 +738,7 @@ public function newFromBuilder($attributes = [], $connection = null)
/**
* Begin querying the model on a given connection.
*
* @param string|null $connection
* @param \UnitEnum|string|null $connection
* @return \Illuminate\Database\Eloquent\Builder<static>
*/
public static function on($connection = null)
Expand Down Expand Up @@ -1951,13 +1953,13 @@ public function getConnection()
*/
public function getConnectionName()
{
return $this->connection;
return enum_value($this->connection);
}

/**
* Set the connection associated with the model.
*
* @param string|null $name
* @param \UnitEnum|string|null $name
* @return $this
*/
public function setConnection($name)
Expand All @@ -1970,7 +1972,7 @@ public function setConnection($name)
/**
* Resolve a connection instance.
*
* @param string|null $connection
* @param \UnitEnum|string|null $connection
* @return \Illuminate\Database\Connection
*/
public static function resolveConnection($connection = null)
Expand Down
30 changes: 30 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use InvalidArgumentException;
use LogicException;
use Mockery as m;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use stdClass;
Expand Down Expand Up @@ -1317,6 +1318,23 @@ public function testConnectionManagement()
$this->assertSame('bar', $model->getConnection());
}

#[TestWith(['Foo'])]
#[TestWith([ConnectionName::Foo])]
#[TestWith([ConnectionNameBacked::Foo])]
public function testConnectionEnums(string|\UnitEnum $connectionName)
{
EloquentModelStub::setConnectionResolver($resolver = m::mock(ConnectionResolverInterface::class));
$model = new EloquentModelStub;

$retval = $model->setConnection($connectionName);
$this->assertEquals($retval, $model);
$this->assertSame('Foo', $model->getConnectionName());

$resolver->shouldReceive('connection')->once()->with('Foo')->andReturn('bar');

$this->assertSame('bar', $model->getConnection());
}

public function testToArray()
{
$model = new EloquentModelStub;
Expand Down Expand Up @@ -4432,3 +4450,15 @@ public function __toString()
return $this->cast;
}
}

enum ConnectionName
{
case Foo;
case Bar;
}

enum ConnectionNameBacked: string
{
case Foo = 'Foo';
case Bar = 'Bar';
}