Skip to content

Commit b16c977

Browse files
committed
update 通过提前存储对象在执行完成后再进行获取避免了自己获取PDO对象
1 parent 7d7cf79 commit b16c977

File tree

7 files changed

+90
-34
lines changed

7 files changed

+90
-34
lines changed

app/ApiJson/Method/GetMethod.php

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\ApiJson\Parse\Handle;
66
use App\Event\ApiJson\QueryExecuteAfter;
77
use App\Event\ApiJson\QueryExecuteBefore;
8+
use App\Event\ApiJson\QueryResult;
89
use Hyperf\Utils\ApplicationContext;
910
use Psr\EventDispatcher\EventDispatcherInterface;
1011

@@ -31,6 +32,11 @@ protected function process()
3132

3233
if(is_null($event->result)) {
3334
$result = $this->query->all();
35+
36+
$queryEvent = new QueryResult($result);
37+
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($queryEvent);
38+
39+
$result = $queryEvent->result;
3440
} else {
3541
$result = $event->result;
3642
}

app/ApiJson/Model/MysqlQuery.php

+1-15
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44

55
use App\ApiJson\Entity\ConditionEntity;
66
use App\ApiJson\Interface\QueryInterface;
7-
use App\Event\ApiJson\MysqlQueryAfter;
87
use Hyperf\Database\Query\Builder;
98
use Hyperf\DbConnection\Db;
10-
use Hyperf\Utils\ApplicationContext;
11-
use PDO;
12-
use Psr\EventDispatcher\EventDispatcherInterface;
139

1410
class MysqlQuery implements QueryInterface
1511
{
@@ -51,17 +47,7 @@ public function getPrimaryKey(): string
5147
public function all(): array
5248
{
5349
$this->buildQuery();
54-
55-
$pdo = $this->db->getConnection()->getReadPdo(); //为了实现自动解析Json 找不到Hyperf的能提供的能力 则手动拿PDO处理
56-
57-
$statement = $pdo->prepare($this->toSql());
58-
$statement->execute($this->getBindings());
59-
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
60-
61-
$event = new MysqlQueryAfter($result, $statement, $this->toSql(), $this->getBindings()); //这可能并不是很好的写法 待暂无其他思路去实现
62-
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
63-
64-
return $event->result;
50+
return $this->db->get()->all();
6551
}
6652

6753
public function count($columns = '*'): int

app/Constants/ConfigCode.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace App\Constants;
13+
14+
use Hyperf\Constants\AbstractConstants;
15+
use Hyperf\Constants\Annotation\Constants;
16+
17+
/**
18+
* @Constants
19+
*/
20+
class ConfigCode extends AbstractConstants
21+
{
22+
/**
23+
* @Message("查询对象")
24+
*/
25+
public const DB_QUERY_STATEMENT = "ApiJson_DbQueryStatement";
26+
}

app/Event/ApiJson/MysqlQueryAfter.php

-15
This file was deleted.

app/Event/ApiJson/QueryResult.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Event\ApiJson;
4+
5+
use PDOStatement;
6+
7+
/**
8+
* 查询完毕的处理
9+
*/
10+
class QueryResult
11+
{
12+
public function __construct(public array $result)
13+
{
14+
}
15+
}

app/Listener/DbPreparedListener.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Listener;
5+
6+
use App\Constants\ConfigCode;
7+
use Hyperf\Database\Events\StatementPrepared;
8+
use Hyperf\Event\Annotation\Listener;
9+
use Hyperf\Event\Contract\ListenerInterface;
10+
use Hyperf\Utils\Context;
11+
12+
/**
13+
* @Listener
14+
*/
15+
class DbPreparedListener implements ListenerInterface
16+
{
17+
public function listen(): array
18+
{
19+
return [
20+
StatementPrepared::class,
21+
];
22+
}
23+
24+
public function process(object $event)
25+
{
26+
if ($event instanceof StatementPrepared) {
27+
Context::set(ConfigCode::DB_QUERY_STATEMENT, $event->statement);
28+
}
29+
}
30+
}

app/Listener/QueryResultTryToJsonListener.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
namespace App\Listener;
55

6-
use App\Event\ApiJson\MysqlQueryAfter;
6+
use App\Constants\ConfigCode;
7+
use App\Event\ApiJson\QueryResult;
78
use Hyperf\Event\Annotation\Listener;
89
use Hyperf\Event\Contract\ListenerInterface;
10+
use Hyperf\Utils\Context;
911

1012
/**
1113
* @Listener
@@ -15,17 +17,23 @@ class QueryResultTryToJsonListener implements ListenerInterface
1517
public function listen(): array
1618
{
1719
return [
18-
MysqlQueryAfter::class,
20+
QueryResult::class,
1921
];
2022
}
2123

2224
public function process(object $event)
2325
{
24-
if (!$event instanceof MysqlQueryAfter) return;
26+
if (!$event instanceof QueryResult) return;
27+
28+
$statement = Context::get(ConfigCode::DB_QUERY_STATEMENT);
29+
if (empty($statement)) {
30+
return;
31+
}
32+
2533
$columnCount = count(array_keys(current($event->result)));
2634
$columnMeta = [];
2735
for ($i = 0; $i <= $columnCount; $i++) {
28-
$meta = $event->statement->getColumnMeta($i);
36+
$meta = $statement->getColumnMeta($i);
2937
if ($meta) {
3038
$columnMeta[$meta['name']] = $meta;
3139
}

0 commit comments

Comments
 (0)