Skip to content

Commit

Permalink
Merge pull request #12 from hanwenbo/master
Browse files Browse the repository at this point in the history
feat: 文档和TPORM的RAW()
  • Loading branch information
kiss291323003 authored Apr 27, 2019
2 parents 14b3b64 + 877aed2 commit 0a4e839
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 24 deletions.
167 changes: 145 additions & 22 deletions TpORM.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
```php
<?php

namespace App;
namespace ezswoole;

use EasySwoole\Mysqli\DbObject;
use App\Pool\MysqlPool;
use App\Pool\MysqlObject;
use EasySwoole\Mysqli\TpORM;
use EasySwoole\Component\Pool\PoolManager;
use EasySwoole\EasySwoole\Config;
use your\pool\MysqlObject;
use your\context\MysqlContext;

/**
* Class Model
* @package ezswoole
* @package your
* @method mixed|static where($whereProps, $whereValue = 'DBNULL', $operator = '=', $cond = 'AND')
* @method mixed|static group(string $groupByField)
* @method mixed|static order(string $orderByField, string $orderByDirection = "DESC", $customFieldsOrRegExp = null)
* @method mixed|static field($field)
*
*/
Expand All @@ -29,34 +29,129 @@ class Model extends TpORM
protected $throwable;
protected $createTime = false;
protected $createTimeName = 'create_time';
protected $softDelete = false;
protected $softDeleteTimeName = 'delete_time';

/**
* Model constructor.
* @param null $data
*/
public function __construct( $data = null )
{
$this->prefix = Config::getInstance()->getConf( 'MYSQL.prefix' );
$db = PoolManager::getInstance()->getPool( MysqlPool::class )->getObj( Config::getInstance()->getConf( 'MYSQL.POOL_TIME_OUT' ) );
$db = \EasySwoole\Component\Context\ContextManager::getInstance()->get(MysqlContext::KEY);
if( $db instanceof MysqlObject ){
parent::__construct( $data );
$this->setDb( $db );
} else{
// todo log
return null;
}
}

public function __destruct()
/**
* 批量添加
* @param array $datas
* @param bool $autoConvertData 自动转换model所需要的类型
* @return bool|mixed
*/
public function addMulti( array $datas = [], bool $convertData = false)
{
$db = $this->getDb();
if( $db instanceof MysqlObject ){
$db->gc();
PoolManager::getInstance()->getPool( MysqlPool::class )->recycleObj( $db );
$this->setDb( null );
try{
if( !empty( $datas ) ){
if($convertData === true ){
foreach($datas as $k => $d){
$datas[$k] = $this->convertData($d);
}
}
if( !is_array( $datas[0] ) ){
return false;
}
$fields = array_keys( $datas[0] );
$db = $this->getDb();
$tableName = $this->getDbTable();
$values = [];
foreach( $datas as $data ){
$value = [];
foreach( $data as $key => $val ){
if( is_string( $val ) ){
$val = '"'.addslashes( $val ).'"';
} elseif( is_bool( $val ) ){
$val = $val ? '1' : '0';
} elseif( is_null( $val ) ){
$val = 'null';
}
if( is_scalar( $val ) ){
$value[] = $val;
}
}
$values[] = '('.implode( ',', $value ).')';
}
$sql = 'INSERT INTO '.$tableName.' ('.implode( ',', $fields ).') VALUES '.implode( ',', $values );
return $db->rawQuery( $sql );
} else{
return false;
}
}catch(\Exception $e){
$this->throwable = $e;
var_dump($e->getTraceAsString());
return false;
}

}

/**
* 批量修改
* @param array $multipleData
* @return bool
*/
public function editMulti( array $multipleData = [] )
{
try{
if( !empty( $multipleData ) ){
$db = $this->getDb();
$pk = $this->getPrimaryKey();
$tableName = $this->getDbTable();
$updateColumn = array_keys( $multipleData[0] );
unset( $updateColumn[0] );
$sql = "UPDATE ".$tableName." SET ";
$pks = array_column( $multipleData, $pk );

foreach( $updateColumn as $uColumn ){
$sql .= "`{$uColumn}` = CASE ";
foreach( $multipleData as $data ){
$val = $data[$pk];
// 判断是不是字符串
if( is_string( $val ) ){
$val = '"'.addslashes( $val ).'"';
} elseif( is_null( $val ) ){
$val = 'NULL';
}

$_val = $data[$uColumn];
if( is_string( $val ) ){
$_val = '"'.addslashes( $_val ).'"';
} elseif( is_null( $_val ) ){
$_val = 'NULL';
}

$sql .= "WHEN `".$pk."` = {$val} THEN {$_val} ";
}
$sql .= "ELSE `".$uColumn."` END, ";
}

$joinStr = join(",",$pks);
$inStr = "'".str_replace(",","','",$joinStr)."'";

$sql = rtrim( $sql, ", " )." WHERE `".$pk."` IN (".$inStr.")";
return $db->rawQuery( $sql ) ?? false;
} else{
return false;
}
}catch(\Exception $e){
$this->throwable = $e;
var_dump($e->getTraceAsString());
return false;
}

}
/**
* @param null $data
* @return bool|int
Expand Down Expand Up @@ -103,7 +198,7 @@ class Model extends TpORM
/**
* @return bool|null
*/
public function del()
protected function del()
{
try{
if( $this->softDelete === true ){
Expand All @@ -127,7 +222,7 @@ class Model extends TpORM
/**
* @return array|bool|false|null
*/
public function select()
protected function select()
{
try{
return parent::select();
Expand All @@ -147,10 +242,10 @@ class Model extends TpORM
* @param string $name
* @return array|bool
*/
public function column( string $name )
protected function column( string $name )
{
try{
return parent::column();
return parent::column( $name );
} catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
$this->throwable = $e;
return false;
Expand All @@ -167,10 +262,30 @@ class Model extends TpORM
* @param string $name
* @return array|bool|null
*/
public function value( string $name )
protected function value( string $name )
{
try{
return parent::value( $name );
} catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
$this->throwable = $e;
return false;
} catch( \EasySwoole\Mysqli\Exceptions\PrepareQueryFail $e ){
$this->throwable = $e;
return false;
} catch( \Throwable $t ){
$this->throwable = $t;
return false;
}
}

/**
* @param string $column
* @return array|bool|int|null
*/
protected function count( string $column = '*')
{
try{
return parent::value();
return parent::count($column);
} catch( \EasySwoole\Mysqli\Exceptions\ConnectFail $e ){
$this->throwable = $e;
return false;
Expand Down Expand Up @@ -205,6 +320,14 @@ class Model extends TpORM
return false;
}
}

/**
* @return Model
*/
static function init()
{
return new static();
}
}
```
## 示例
Expand Down
4 changes: 2 additions & 2 deletions src/TpORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected function order( string $orderByField, string $orderByDirection = "DESC
// 替换多个空格为单个空格
$orderByField = preg_replace( '#\s+#', ' ', trim( $orderByField ) );
// 如果是 "create_time desc,time asc"
if( strstr( $orderByField, ',' ) ){
if( strstr( $orderByField, ',' ) && !strstr( $orderByField, 'RAW(' ) ){
$orders = explode( ',', $orderByField );
foreach( $orders as $order ){
// 如果是存在空格,执行orderBy("create_time","DESC")
Expand All @@ -172,7 +172,7 @@ protected function order( string $orderByField, string $orderByDirection = "DESC
}
} else{
// 如果是存在空格,执行orderBy("create_time","DESC")
if( strstr( $orderByField, ' ' ) ){
if( strstr( $orderByField, ' ' ) && !strstr( $orderByField, 'RAW(' ) ){
$split = explode( ' ', $orderByField );
$this->getDb()->orderBy( $split[0], $split[1] );
} else{
Expand Down

0 comments on commit 0a4e839

Please sign in to comment.