Callback event

For the overall situation onQuery

Set callback events for the global as follows:

// When registering ORM, call the callback function
public static function mainServerCreate(EventRegister $register)
{
    ...

    DbManager::getInstance()->addConnection(new Connection($config));
    DbManager::getInstance()->onQuery(function ($res, $builder, $start) {
        // Print parameter or write log
    });
}

The onquery callback will inject three parameters

  • resQuery result object, class name isEasySwoole\ORM\Db\Result

Can refer to results of enforcement Document for more results

  • builderQuery statement object, class name isEasySwoole\Mysqli\QueryBuilder

  • startStart query timestamp, unit iss, type is float

If the withTotalCount () method is invoked during the query, there will be second callback results.

It should be noted that this callback party legal must be called when registering ORM, otherwise no result will be generated

Model specific onQuery

If we don't want to use the global onquery, we can call the onquery method when we perform the operation, so as to realize the callback for a specific model

$res = User::create()->onQuery(function ($res, $builder, $start) {
    // Print parameter or write log
})->get(1);

The three parameters of callback injection are the same as the global onquery

Log slow

We can manually determine the execution time to achieve a slow log recording function

public static function mainServerCreate(EventRegister $register)
{
    ...

    DbManager::getInstance()->addConnection(new Connection($config));
    DbManager::getInstance()->onQuery(function ($res, $builder, $start) {
        $queryTime = Query time threshold;
        if (bcsub(time(), $start, 3) > $queryTime) {
            // Write log
        }
    });
}