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
16 changes: 8 additions & 8 deletions src/Database/Models/DatabaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,17 @@ public function save(TransactionManager|null $transactionManager = null): bool
$result = $queryBuilder
->insertGetId($data, $this->getModelKeyColumnName());

if ($result) {
if (! $this->keyIsInitialized()) {
$this->{$this->getModelKeyName()} = $result;
}

$this->setAsExisting();
if ($result === false) {
return false;
}

return true;
if (! $this->keyIsInitialized() && $result !== null) {
$this->{$this->getModelKeyName()} = $result;
}

return false;
$this->setAsExisting();

return true;
}

public function delete(TransactionManager|null $transactionManager = null): bool
Expand Down
8 changes: 4 additions & 4 deletions src/Database/Models/QueryBuilders/DatabaseQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ public function create(array $attributes): DatabaseModel

$queryBuilder = clone $this;
$queryBuilder->setModel($model);
$modelKeyName = $model->getModelKeyName();
$keyWasInitialized = isset($model->{$modelKeyName});
$result = $queryBuilder->insertGetId($data, $model->getModelKeyColumnName());

if ($result) {
$modelKeyName = $model->getModelKeyName();

if (! isset($model->{$modelKeyName})) {
if ($result !== false && ($keyWasInitialized || $result !== null)) {
if (! $keyWasInitialized) {
$model->{$modelKeyName} = $result;
}

Expand Down
55 changes: 55 additions & 0 deletions tests/Feature/Database/DatabaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,34 @@
expect($model->createdAt)->toBeInstanceOf(Date::class);
});

it('creates model with manually assigned string ID when insertGetId returns zero', function () {
$insertResult = new Result([['Query OK']]);
$insertResult->setLastInsertedId(0);

$connection = $this->getMockBuilder(MysqlConnectionPool::class)->getMock();

$connection->expects($this->exactly(1))
->method('prepare')
->willReturnOnConsecutiveCalls(
new Statement($insertResult),
);

$this->app->swap(Connection::default(), $connection);

$uuid = Str::uuid()->toString();

$model = UserWithUuid::create([
'id' => $uuid,
'name' => 'John Doe',
'email' => faker()->email(),
'created_at' => Date::now(),
]);

expect($model->isExisting())->toBeTrue();
expect($model->id)->toBe($uuid);
expect($model->createdAt)->toBeInstanceOf(Date::class);
});

it('throws an exception when column in invalid on create instance', function () {
expect(function () {
$connection = $this->getMockBuilder(MysqlConnectionPool::class)->getMock();
Expand Down Expand Up @@ -895,6 +923,33 @@
expect($model->createdAt)->toBeInstanceOf(Date::class);
});

it('saves a new model with manually assigned string ID when insertGetId returns zero', function () {
$insertResult = new Result([['Query OK']]);
$insertResult->setLastInsertedId(0);

$connection = $this->getMockBuilder(MysqlConnectionPool::class)->getMock();

$connection->expects($this->exactly(1))
->method('prepare')
->willReturnOnConsecutiveCalls(
new Statement($insertResult),
);

$this->app->swap(Connection::default(), $connection);

$uuid = Str::uuid()->toString();
$model = new UserWithUuid();
$model->id = $uuid;
$model->name = 'John Doe';
$model->email = faker()->email();

expect($model->isExisting())->toBeFalse();
expect($model->save())->toBeTrue();
expect($model->isExisting())->toBeTrue();
expect($model->id)->toBe($uuid);
expect($model->createdAt)->toBeInstanceOf(Date::class);
});

it('updates an existing model with string ID correctly', function () {
$uuid = Str::uuid()->toString();
$data = [
Expand Down
Loading