PHP code example of guiqibusixin / hyper-database-ext
1. Go to this page and download the library: Download guiqibusixin/hyper-database-ext library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
guiqibusixin / hyper-database-ext example snippets
declare(strict_types=1);
namespace App\Model;
use Guiqibusixin\Hyperf\Database\Model\Traits\ModelBuilderExtensionTrait;
use Hyperf\DbConnection\Model\Model as BaseModel;
abstract class Model extends BaseModel
{
use ModelBuilderExtensionTrait;
}
declare(strict_types=1);
namespace App\Service;
use App\Model\User;
use Guiqibusixin\Hyperf\Database\Annotation\Transactional;
class UserService
{
#[Transactional(modelClass: User::class)] // 通过模型类
// #[Transactional(connection: 'test')] // 通过数据库连接名
// #[Transactional] //默认连接,连接名为default
public function create()
{
}
}
declare (strict_types=1);
namespace App\Model;
use Guiqibusixin\Hyperf\Database\Annotation\Transactional;
class User extends Model
{
protected $table = 'user';
protected $connection = 'common';
#[Transactional] //此时开启事务连接是本模型的连接(common),而不是default
public static function createUser(array $user)
{
//创建用户逻辑
}
}