1. Go to this page and download the library: Download easyswoole/fast-db 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/ */
declare(strict_types=1);
namespace App\Model;
use EasySwoole\FastDb\AbstractInterface\AbstractEntity;
use EasySwoole\FastDb\Attributes\Property;
/**
* @property int $id increment id
* @property string|null $name name
* @property int|null $status status
* @property int|null $score score
* @property int|null $sex sex
* @property string|null $address address
* @property string|null $email email
*/
class EasySwooleUser extends AbstractEntity
{
#[Property(isPrimaryKey: true)]
public int $id;
#[Property]
public ?string $name;
#[Property]
public ?int $status;
#[Property]
public ?int $score;
#[Property]
public ?int $sex;
#[Property]
public ?string $address;
#[Property]
public ?string $email;
public function tableName(): string
{
return 'easyswoole_user';
}
}
CREATE TABLE `student_info` (
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
`studentId` int unsigned NOT NULL DEFAULT '0' COMMENT 'student id',
`address` json DEFAULT NULL COMMENT 'address',
`note` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT 'note',
`sex` tinyint unsigned NOT NULL DEFAULT '0' COMMENT 'sex:1=male 2=female 0=unknown',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
namespace EasySwoole\FastDb\Tests\Model;
use EasySwoole\FastDb\AbstractInterface\AbstractEntity;
use EasySwoole\FastDb\Attributes\Property;
use EasySwoole\FastDb\Tests\Model\Address;
use EasySwoole\FastDb\Tests\Model\SexEnum;
class StudentInfoModel extends AbstractEntity
{
#[Property(isPrimaryKey: true)]
public int $id;
#[Property()]
public int $studentId;
#[Property(
convertObject: Address::class
)]
public Address $address;
#[Property]
public ?string $note;
#[Property(
convertObject: SexEnum::class
)]
public SexEnum $sex;
function tableName(): string
{
return "student_info";
}
}
namespace EasySwoole\FastDb\Tests\Model;
use EasySwoole\FastDb\AbstractInterface\ConvertJson;
class Address extends ConvertJson
{
public $city;
public $province;
}
namespace EasySwoole\FastDb\Tests\Model;
use EasySwoole\FastDb\AbstractInterface\ConvertObjectInterface;
enum SexEnum implements ConvertObjectInterface
{
case UNKNUWN;
case MALE;
case FEMAILE;
public static function toObject(mixed $data): object
{
switch ($data){
case 1:{
return self::MALE;
}
case 2:{
return self::FEMAILE;
}
default:{
return self::UNKNUWN;
}
}
}
function toValue()
{
return match ($this){
self::MALE=>1,
self::FEMAILE=>2,
default=>0
};
}
}
$user = new User();
$user->count();
// SELECT COUNT(*) as count FROM `easyswoole_user` LIMIT 1
$user->count('id', 'name');
// SELECT COUNT(id) as count FROM `easyswoole_user` GROUP BY name LIMIT 1
$user->queryLimit()->fields(['id', 'name']);
$user->count(null, 'name');
// SELECT COUNT(`id`) as id, COUNT(`name`) as name FROM `easyswoole_user` GROUP BY name LIMIT 1
$user = new User();
$user->max('id');
// SELECT MAX(`id`) as id FROM `easyswoole_user` LIMIT 1
$user->max('id', 'name');
// SELECT MAX(`id`) as id FROM `easyswoole_user` GROUP BY name LIMIT 1
$user->max(['id', 'name'], 'name');
// SELECT MAX(`id`) as id , MAX(`name`) as name FROM `easyswoole_user` GROUP BY name LIMIT 1
$user = new User();
$user->min('id');
// SELECT MIN(`id`) as id FROM `easyswoole_user` LIMIT 1
$user->min('id', 'name');
// SELECT MIN(`id`) as id FROM `easyswoole_user` GROUP BY name LIMIT 1
$user->min(['id', 'name'], 'name');
// SELECT MIN(`id`) as id , MIN(`name`) as name FROM `easyswoole_user` GROUP BY name LIMIT 1
$user = new User();
$user->avg('id');
// SELECT AVG(`id`) as id FROM `easyswoole_user` LIMIT 1
$user->avg('id', 'name');
// SELECT AVG(`id`) as id FROM `easyswoole_user` GROUP BY name LIMIT 1
$user->avg(['id', 'name'], 'name');
// SELECT AVG(`id`) as id , AVG(`name`) as name FROM `easyswoole_user` GROUP BY name LIMIT 1
$user = new User();
$user->sum('id');
// SELECT SUM(`id`) as id FROM `easyswoole_user` LIMIT 1
$user->sum('id', 'name');
// SELECT SUM(`id`) as id FROM `easyswoole_user` GROUP BY name LIMIT 1
$user->sum(['id', 'name'], 'name');
// SELECT SUM(`id`) as id , SUM(`name`) as name FROM `easyswoole_user` GROUP BY name LIMIT 1
declare(strict_types=1);
namespace EasySwoole\FastDb\Tests\Model;
use EasySwoole\FastDb\AbstractInterface\AbstractEntity;
use EasySwoole\FastDb\Attributes\Hook\OnInitialize;
use EasySwoole\FastDb\Attributes\Hook\OnInsert;
use EasySwoole\FastDb\Attributes\Hook\OnDelete;
use EasySwoole\FastDb\Attributes\Hook\OnUpdate;
// ...
/**
* @property int $id
* @property string $name
* @property int $status
* @property int $score
* @property int $create_time
*/
#[OnInitialize('onInitialize')]
#[OnInsert('onInsert')]
#[OnDelete('onDelete')]
#[OnUpdate('onUpdate')]
class User extends AbstractEntity
{
// ...
public function onInitialize()
{
// todo::
}
public function onInsert()
{
if (empty($this->status)) {
return false;
}
if (empty($this->create_time)) {
$this->create_time = time();
}
}
public function onDelete()
{
// todo::
}
public function onUpdate()
{
// todo::
}
}
declare(strict_types=1);
namespace EasySwoole\FastDb\Tests\Model;
use EasySwoole\FastDb\AbstractInterface\AbstractEntity;
use EasySwoole\FastDb\Attributes\Property;
use EasySwoole\FastDb\Attributes\Relate;
use EasySwoole\FastDb\Tests\Model\UserProfile;
/**
* @property int $id
* @property string $name
* @property string $email
*/
class User extends AbstractEntity
{
#[Property(isPrimaryKey: true)]
public int $id;
#[Property]
public ?string $name;
#[Property]
public ?string $email;
public function tableName(): string
{
return 'easyswoole_user';
}
#[Relate(
targetEntity: UserProfile::class,
targetProperty: 'user_id' // 关联模型的数据表的主键
)]
public function profile()
{
return $this->relateOne();
}
}
declare(strict_types=1);
namespace EasySwoole\FastDb\Tests\Model;
use EasySwoole\FastDb\AbstractInterface\AbstractEntity;
use EasySwoole\FastDb\Attributes\Property;
use EasySwoole\FastDb\Attributes\Relate;
use EasySwoole\FastDb\Tests\Model\UserCar;
/**
* @property int $id
* @property string $name
* @property int $status
* @property int $score
* @property string $email
*/
class User extends AbstractEntity
{
#[Property(isPrimaryKey: true)]
public int $id;
#[Property]
public ?string $name;
#[Property]
public ?int $status;
#[Property]
public ?int $score;
#[Property]
public ?int $create_time;
#[Property]
public ?string $info;
#[Property]
public ?string $foo;
#[Property]
public ?string $bar;
#[Property]
public ?int $login_time;
#[Property]
public ?int $login_times;
#[Property]
public ?int $read;
#[Property]
public ?string $title;
#[Property]
public ?string $content;
#[Property]
public ?string $email;
public function tableName(): string
{
return 'easyswoole_user';
}
#[Relate(
targetEntity: UserCar::class,
targetProperty: 'user_id'
)]
public function cars()
{
return $this->relateMany();
}
}
$builder = new \EasySwoole\Mysqli\QueryBuilder();
$builder->raw('select * from user');
$result = FastDb::getInstance()->invoke(function (\EasySwoole\FastDb\Mysql\Connection $connection) use ($builder) {
$connection->query($builder);
return $connection->rawQuery("select * from user");
});
FastDb::getInstance()->begin();
FastDb::getInstance()->commit();
FastDb::getInstance()->rollback();
$builder = new \EasySwoole\Mysqli\QueryBuilder();
$builder->raw("select * from user where id = ?", [1]);
FastDb::getInstance()->query($builder);
FastDb::getInstance()->rawQuery('select * from user where id = 1');
FastDb::getInstance()->currentConnection();
FastDb::getInstance()->reset();
namespace EasySwoole\EasySwoole;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\FastDb\FastDb;
class EasySwooleEvent implements Event
{
public static function initialize()
{
date_default_timezone_set('Asia/Shanghai');
$mysqlArrayConfig = Config::getInstance()->getConf('MYSQL');
$config = new \EasySwoole\FastDb\Config($mysqlArrayConfig);
FastDb::getInstance()->addDb($config);
}
public static function mainServerCreate(EventRegister $register)
{
$register->add($register::onWorkerStart, function () {
// 连接预热
FastDb::getInstance()->preConnect();
});
}
}