示例

假如你的项目的tp的风格,可以继承EasySwoole\Mysqli\TpORM来自定义

  1. <?php
  2. namespace App;
  3. use EasySwoole\Mysqli\DbObject;
  4. use App\Pool\MysqlPool;
  5. use App\Pool\MysqlObject;
  6. use EasySwoole\Mysqli\TpORM;
  7. use EasySwoole\Component\Pool\PoolManager;
  8. use EasySwoole\EasySwoole\Config;
  9. /**
  10. * Class Model
  11. * @package ezswoole
  12. * @method mixed|static where($whereProps, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
  13. * @method mixed|static field($field)
  14. *
  15. */
  16. class Model extends TpORM
  17. {
  18. protected $prefix;
  19. protected $modelPath = '\\App\\Model';
  20. protected $fields = [];
  21. protected $limit;
  22. protected $throwable;
  23. protected $createTime = false;
  24. protected $createTimeName = 'create_time';
  25. /**
  26. * Model constructor.
  27. * @param null $data
  28. */
  29. public function __construct( $data = null )
  30. {
  31. $this->prefix = Config::getInstance()->getConf( 'MYSQL.prefix' );
  32. $db = PoolManager::getInstance()->getPool( MysqlPool::class )->getObj( Config::getInstance()->getConf( 'MYSQL.POOL_TIME_OUT' ) );
  33. if( $db instanceof MysqlObject ){
  34. parent::__construct( $data );
  35. $this->setDb( $db );
  36. } else{
  37. // todo log
  38. return null;
  39. }
  40. }
  41. public function __destruct()
  42. {
  43. $db = $this->getDb();
  44. if( $db instanceof MysqlObject ){
  45. $db->gc();
  46. PoolManager::getInstance()->getPool( MysqlPool::class )->recycleObj( $db );
  47. $this->setDb( null );
  48. }
  49. }
  50. /**
  51. * @param null $data
  52. * @return bool|int
  53. */
  54. protected function add( $data = null )
  55. {
  56. try{
  57. if( $this->createTime === true ){
  58. $data[$this->createTimeName] = time();
  59. }
  60. return parent::insert( $data );
  61. } catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
  62. $this->throwable = $e;
  63. return false;
  64. } catch( \EasySwoole\Mysqli\Exceptions\PrepareQueryFail $e ){
  65. $this->throwable = $e;
  66. return false;
  67. } catch( \Throwable $t ){
  68. $this->throwable = $t;
  69. return false;
  70. }
  71. }
  72. /**
  73. * @param null $data
  74. * @return bool|mixed
  75. */
  76. protected function edit( $data = null )
  77. {
  78. try{
  79. return $this->update( $data );
  80. } catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
  81. $this->throwable = $e;
  82. return false;
  83. } catch( \EasySwoole\Mysqli\Exceptions\PrepareQueryFail $e ){
  84. $this->throwable = $e;
  85. return false;
  86. } catch( \Throwable $t ){
  87. $this->throwable = $t;
  88. return false;
  89. }
  90. }
  91. /**
  92. * @return bool|null
  93. */
  94. public function del()
  95. {
  96. try{
  97. if( $this->softDelete === true ){
  98. $data[$this->softDeleteTimeName] = time();
  99. return $this->update( $data );
  100. } else{
  101. return parent::delete();
  102. }
  103. } catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
  104. $this->throwable = $e;
  105. return false;
  106. } catch( \EasySwoole\Mysqli\Exceptions\PrepareQueryFail $e ){
  107. $this->throwable = $e;
  108. return false;
  109. } catch( \Throwable $t ){
  110. $this->throwable = $t;
  111. return false;
  112. }
  113. }
  114. /**
  115. * @return array|bool|false|null
  116. */
  117. public function select()
  118. {
  119. try{
  120. return parent::select();
  121. } catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
  122. $this->throwable = $e;
  123. return false;
  124. } catch( \EasySwoole\Mysqli\Exceptions\PrepareQueryFail $e ){
  125. $this->throwable = $e;
  126. return false;
  127. } catch( \Throwable $t ){
  128. $this->throwable = $t;
  129. return false;
  130. }
  131. }
  132. /**
  133. * @param string $name
  134. * @return array|bool
  135. */
  136. public function column( string $name )
  137. {
  138. try{
  139. return parent::column();
  140. } catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
  141. $this->throwable = $e;
  142. return false;
  143. } catch( \EasySwoole\Mysqli\Exceptions\PrepareQueryFail $e ){
  144. $this->throwable = $e;
  145. return false;
  146. } catch( \Throwable $t ){
  147. $this->throwable = $t;
  148. return false;
  149. }
  150. }
  151. /**
  152. * @param string $name
  153. * @return array|bool|null
  154. */
  155. public function value( string $name )
  156. {
  157. try{
  158. return parent::value();
  159. } catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
  160. $this->throwable = $e;
  161. return false;
  162. } catch( \EasySwoole\Mysqli\Exceptions\PrepareQueryFail $e ){
  163. $this->throwable = $e;
  164. return false;
  165. } catch( \Throwable $t ){
  166. $this->throwable = $t;
  167. return false;
  168. }
  169. }
  170. /**
  171. * @return array|bool
  172. */
  173. protected function find( $id = null )
  174. {
  175. try{
  176. if( $id ){
  177. return $this->byId( $id );
  178. } else{
  179. return parent::find();
  180. }
  181. } catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
  182. $this->throwable = $e;
  183. return false;
  184. } catch( \EasySwoole\Mysqli\Exceptions\PrepareQueryFail $e ){
  185. $this->throwable = $e;
  186. return false;
  187. } catch( \Throwable $t ){
  188. $this->throwable = $t;
  189. return false;
  190. }
  191. }
  192. }

示例

WikiModel.php

  1. <?php
  2. namespace App\Model;
  3. use yourPath\Model;
  4. class Wiki extends Model
  5. {
  6. protected $softDelete = true;
  7. protected $softDeleteTimeName = 'delete_time';
  8. protected $createTime = true;
  9. protected $createTimeName = 'create_time';
  10. protected $dbFields
  11. = [
  12. 'content' => ['text', 'required'],
  13. 'id' => ['int'],
  14. 'name' => ['text'],
  15. 'create_time' => ['int'],
  16. 'delete_time' => ['int'],
  17. ];
  18. }
  19. ?>

查询多条

  1. WikiModel::where( ['id' => ['>', 5]] )->field( ['id'] )->select();
  2. WikiModel::where( ['id' => ['>', 5]] )->column( 'id' );
  3. WikiModel::where( ['id' => ['>', 5]] )->value( 'id' );
  4. WikiModel::where( ['id' => ['>', 5]] )->field( ['id'] )->select();

单条

  1. WikiModel::where( ['id' => ['>', 5]] )->find();

添加

  1. WikiModel::add( ['content' => "1111"] );

修改

注意:对象的主键。’id’是默认值。protected $primaryKey = ‘id’;

如果想改为其他,请继承并替换

  1. // 方式1,逻辑是先查询有没有,如果有就实例化出来一个对象,然后调用edit方法
  2. $wiki = WikiModel::byId( 4 );
  3. $wiki->edit( ['content' => 'xxxxxxxxxxx'] );
  4. // 方式2,不管有没有直接就修改
  5. $wiki->edit( ['id'=>'4','content' => 'xxxxxxxxxxx'] );
  6. // 还可以这么写
  7. WikiModel::where( ['id' => ['>' => 64]] )->edit( ['content' => '我是64'] );

删除

  1. // 方式1
  2. WikiModel::byId( 64 )->del();
  3. // 方式2
  4. WikiModel::where( ['id' => ['>=' => 63]] )->del();