# 最终都是获取到 Hyperf\Database\Model\Bulder 的实例 /** * Get a new query builder for the model's table. * * @return \Hyperf\Database\Model\Builder */ publicfunctionnewQuery() { return$this->registerGlobalScopes($this->newQueryWithoutScopes()); }
/** * Get a new query builder that doesn't have any global scopes or eager loading. * * @return \Hyperf\Database\Model\Builder|static */ publicfunctionnewModelQuery() { return$this->newModelBuilder($this->newBaseQueryBuilder())->setModel($this); }
/** * Get a new query builder instance for the connection. * * @return \Hyperf\Database\Query\Builder */ protectedfunctionnewBaseQueryBuilder() { $connection = $this->getConnection();
/** * Get the database connection for the model. * You can write it by yourself. */ publicfunctiongetConnection(): ConnectionInterface { returnRegister::resolveConnection($this->getConnectionName()); }
if (! $connectioninstanceof ConnectionInterface) { $pool = $this->factory->getPool($name); $connection = $pool->get(); try { // PDO is initialized as an anonymous function, so there is no IO exception, // but if other exceptions are thrown, the connection will not return to the connection pool properly. $connection = $connection->getConnection(); Context::set($id, $connection); } finally { if (Coroutine::inCoroutine()) { defer(function () use ($connection, $id) { Context::set($id, null); $connection->release(); }); } } }