limingxinleo / swoft-db-model
基于Swoft Db模型封装
1.0.1
2018-06-30 07:51 UTC
Requires
- php: >=7.0
- swoft/db: ^1.0
- swoft/framework: ^1.0
Requires (Dev)
- phpunit/phpunit: ^5.7
- swoft/swoole-ide-helper: dev-master
This package is auto-updated.
Last update: 2024-11-17 19:50:43 UTC
README
基于Swoft的Model封装
Mysql Connection 事件的使用
- 增加事件类
<?php namespace SwoftTest\Db\Testing\Listener; use Swoft\App; use Swoft\Bean\Annotation\Listener; use Swoft\Event\EventHandlerInterface; use Swoft\Event\EventInterface; use SwoftTest\Db\Testing\Entity\User; use Xin\Swoft\Db\Driver\Mysql\MysqlConnection; use Xin\Swoft\Db\Event\MysqlConnectionEvent; /** * Model before save handler * * @Listener(MysqlConnectionEvent::AFTER_EXECUTE) */ class AfterExecuteListener implements EventHandlerInterface { /** * @param \Swoft\Event\EventInterface $event */ public function handle(EventInterface $event) { /** @var MysqlConnection $model */ $model = $event->getConnection(); $runtime = App::getAlias('@runtime'); $file = $runtime . '/sql.log'; file_put_contents($file, $model->getSql() . PHP_EOL, FILE_APPEND); } }
- 重写默认DB连接池
主库
<?php namespace App\Pool; use Swoft\Bean\Annotation\Pool; use Swoft\Db\Pool\DbPool as SwoftDbPool; use Xin\Swoft\Db\Pool\CreateConnectionTrait; /** * OtherDbPool * * @Pool("default.master") */ class DbPool extends SwoftDbPool { use CreateConnectionTrait; }
从库
<?php namespace App\Pool; use Swoft\Bean\Annotation\Pool; use Swoft\Db\Pool\DbSlavePool as SwoftDbSlavePool; use Xin\Swoft\Db\Pool\CreateConnectionTrait; /** * OtherDbSlavePool * * @Pool("default.slave") */ class DbSlavePool extends SwoftDbSlavePool { use CreateConnectionTrait; }
- 实体类增加DB连接池的Instance
此实体重写了Mysql和同步Mysql链接,但是连接池配置与default一致,可以放心使用。
注解如下
@Entity(instance="dbModel")
模型事件的使用
增加事件类
<?php namespace SwoftTest\Db\Testing\Listener; use Swoft\Bean\Annotation\Listener; use Swoft\Event\EventHandlerInterface; use Swoft\Event\EventInterface; use SwoftTest\Db\Testing\Entity\User; use Xin\Swoft\Db\Event\ModelEvent; /** * Model before save handler * * @Listener(ModelEvent::BEFORE_SAVE) */ class BeforeSaveListener implements EventHandlerInterface { /** * @param \Swoft\Event\EventInterface $event */ public function handle(EventInterface $event) { /** @var User $model */ $model = $event->getModel(); if (method_exists($model, 'setCreatedAt') && method_exists($model, 'setUpdatedAt')) { $date = date('Y-m-d H:i:s'); $model->setCreatedAt($date); $model->setUpdatedAt($date); } } }
将事件类的位置放到 beanScan 中
<?php // config/properties return [ 'version' => '1.0', 'autoInitBean' => true, 'beanScan' => [ 'SwoftTest\\Db\\Testing' => BASE_PATH . '/Testing', 'Xin\\Swoft\\Db' => BASE_PATH . '/../src', ], 'I18n' => [ 'sourceLanguage' => '@root/resources/messages/', ], 'env' => 'Base', 'user.stelin.steln' => 'fafafa', 'Service' => [ 'user' => [ 'timeout' => 3000 ] ], 'db' => require dirname(__FILE__) . DS . 'db.php', ];