もくじ
try catchとtransactionの位置
どこに記述したら良いのか?
- tryの直前でtransaction()開始
- tryの中でcommit()
- catchの中でrollback()
実装例
<?php namespace App\Services\Master; use App\Repositories\Shop\shopDesignInterface; use App\Repositories\Master\ModuleInterface; use App\Repositories\Customer\CustomerInterface; class ShopDesignService extends ManagementService implements CreateShopDesignServiceInterface { public function __construct( ShopDesignInterface $i_shop_design, ModuleInterface $i_module, CustomerInterface $i_customer ) { $this->i_shop_design = $i_shop_design; $this->i_module = $i_module; $this->i_customer = $i_customer; } ・・・ /** * 商品削除 * * @param $detail_id * @return int */ public function delete(int $product_id): int { $this->i_shop_design->begin(); // ●トランザクション開始 try { // shop_content削除 $delete_product_count = $this->i_shop_design->deleteByIds([$product_id]); // customer_content削除 $shop_module = $this->i_module->findWhere(['code' => "shop"]); if (!isset($shop_module->id)) { throw new NotFoundHttpException("ブースのmodule_idがありません"); } $this->i_customer->deleteWhere([ 'sale_id' => $this->getSaleId(), 'product_id' => $product_id ]); $this->i_shop_design->commit(); // ●コミット } catch (\Exception $e) { $this->i_shop_design->rollback(); // ●ロールバック throw $e; } return $delete_product_count; } ・・・ }
リポジトリパターンで抽象化
begin(), commit(), rollback()などLaravelでよく使う処理はTraitでまとめておくと便利です。
$this->i_c->begin(); $this->i_c->commit(); $this->i_c->rollback();
App\Repositories\Shop\ShopDesignRepository.php
<?php namespace App\Repositories\Shop; use App\Entities\ShopDesign; use App\Repositories\Traits\ResourceTrait; class ShopDesignRepository implements ShopDesignInterface { use ResourceConstructTrait; /** * @param \App\Entities\ShopDesign $resource */ public function __construct(ShopDesign $resource) { $this->resource = $resource; } ・・・ }
App\Repositories\Shop\ShopDesignInterface
<?php namespace App\Repositories\Shop; use App\Repositories\Traits\ResourceInterface; interface ShopDesignInterface extends ResourceInterface { }
App\Repositories\Traits\ResourceTrait.php
<?php namespace App\Repositories\Traits; trait ResourceTrait { /** * @var Illuminate\Database\Eloquent\Model */ protected $resource; ・・・ public function begin($connection = null) { $this->resource->getConnection($connection)->beginTransaction(); } public function rollback($connection = null) { $this->resource->getConnection($connection)->rollback(); } public function commit($connection = null) { $this->resource->getConnection($connection)->commit(); } ・・・ }
<?php namespace App\Entities; use Illuminate\Database\Eloquent\Model; use Prettus\Repository\Contracts\Transformable; use Prettus\Repository\Traits\TransformableTrait; /** * Class ShopDesign. * * @package namespace App\Entities; */ class ShopDesign extends Model implements Transformable { use TransformableTrait; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ "id", "is_sall", "type", "json", ]; }
App\Providers\RepositoryServiceProvider.php
<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; class RepositoryServiceProvider extends ServiceProvider { /** * Bootstrap services. * * @return void */ public function boot() { // } /** * Register services. * * @return void */ public function register() { ・・・ $this->app->bind( \App\Repositories\Shop\shopDesignInterface::class, \App\Repositories\Shop\shopDesignRepository::class ); $this->app->bind( \App\Repositories\Master\ModuleInterface::class, \App\Repositories\Master\ModuleRepository::class ); $this->app->bind( \App\Repositories\Customer\CustomerInterface::class, \App\Repositories\Customer\CustomerRepository::class ); ・・・ } }
$ composer dumpa