Added method \Hyperf\Database\Model\Model::updateOrFail (#6884)

Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
zds 2024-06-19 13:09:58 +08:00 committed by GitHub
parent abdbf7a1ee
commit 2685077627
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 1 deletions

View File

@ -8,6 +8,7 @@
- [#6876](https://github.com/hyperf/hyperf/pull/6876) Added method `Hyperf\Database\Concerns\Builder::eachById`.
- [#6878](https://github.com/hyperf/hyperf/pull/6878) Added methods `whereMorphRelation` and `orWhereMorphRelation` into `Hyperf\Database\Model\Concerns\QueriesRelationships`.
- [#6883](https://github.com/hyperf/hyperf/pull/6883) Added methods `getIndexes` `hasIndex` and `getIndexListing` into `Hyperf\Database\Schema\Builder`.
- [#6884](https://github.com/hyperf/hyperf/pull/6884) Added method `Hyperf\Database\Model\Model::updateOrFail`.
- [#6897](https://github.com/hyperf/hyperf/pull/6897) [#6899](https://github.com/hyperf/hyperf/pull/6899) Added events `BeforeLongLangConsumerCreated` and `AfterConsumerConfigCreated` into `Hyperf\Kafka\ConsumerManager`.
## Optimized

View File

@ -568,6 +568,19 @@ abstract class Model implements ArrayAccess, Arrayable, Jsonable, JsonSerializab
return $this->fill($attributes)->save($options);
}
/**
* Update the model in the database within a transaction.
* @throws Throwable
*/
public function updateOrFail(array $attributes = [], array $options = []): bool
{
if (! $this->exists) {
return false;
}
return $this->fill($attributes)->saveOrFail($options);
}
/**
* Save the model and all of its relationships.
*

View File

@ -13,6 +13,7 @@ declare(strict_types=1);
namespace HyperfTest\Database;
use Carbon\Carbon;
use Exception;
use Hyperf\Context\ApplicationContext;
use Hyperf\Context\Context;
use Hyperf\Contract\LengthAwarePaginatorInterface;
@ -24,9 +25,11 @@ use Hyperf\Database\ConnectionResolverInterface;
use Hyperf\Database\Connectors\ConnectionFactory;
use Hyperf\Database\Connectors\MySqlConnector;
use Hyperf\Database\Events\QueryExecuted;
use Hyperf\Database\Exception\QueryException;
use Hyperf\Database\Model\EnumCollector;
use Hyperf\Database\Model\Events\Saved;
use Hyperf\Database\Model\Model;
use Hyperf\Database\Model\Register;
use Hyperf\Database\MySqlBitConnection;
use Hyperf\Database\Query\Builder as QueryBuilder;
use Hyperf\Database\Query\Expression;
@ -1279,6 +1282,38 @@ class ModelRealBuilderTest extends TestCase
Schema::dropIfExists('lazy_users');
}
public function testUpdateOrFail(): void
{
$container = $this->getContainer();
Register::setConnectionResolver($container->get(ConnectionResolverInterface::class));
$container->shouldReceive('get')->with(Db::class)->andReturn(new Db($container));
Schema::create('update_or_fail', function (Blueprint $table) {
$table->id();
$table->string('name', 5);
$table->timestamps();
});
$model = UpdateOrFail::create([
'name' => Str::random(5),
]);
try {
$model->updateOrFail([
'name' => Str::random(6),
]);
} catch (Exception $e) {
$this->assertInstanceOf(QueryException::class, $e);
}
$this->assertFalse((new UpdateOrFail())->updateOrFail([]));
$name = Str::random(4);
$model->updateOrFail([
'name' => $name,
]);
$this->assertSame($name, $model->name);
Schema::drop('update_or_fail');
}
protected function getContainer()
{
$dispatcher = Mockery::mock(EventDispatcherInterface::class);
@ -1298,3 +1333,10 @@ class LazyUserModel extends Model
{
protected ?string $table = 'lazy_users';
}
class UpdateOrFail extends Model
{
protected ?string $table = 'update_or_fail';
protected array $guarded = [];
}

View File

@ -39,7 +39,9 @@ class ContainerStub
return $logger;
});
$container->shouldReceive('get')->with(EventDispatcherInterface::class)->andReturnUsing(function () {
return Mockery::mock(EventDispatcherInterface::class);
$dispatcher = Mockery::mock(EventDispatcherInterface::class);
$dispatcher->shouldReceive('dispatch')->andReturnNull();
return $dispatcher;
});
$container->shouldReceive('has')->andReturnUsing(function ($class) {
return true;