Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Laravel中,SQL连接和查询在哪里实现?_Laravel - Fatal编程技术网

在Laravel中,SQL连接和查询在哪里实现?

在Laravel中,SQL连接和查询在哪里实现?,laravel,Laravel,我想知道所有提供数据库连接、数据制作和其他功能的SQL查询都位于何处。我试图越来越深入地研究Laravel源代码,但我发现的只是Laravel方法。我知道这很没用,但也很有趣。SQL连接位于项目根目录中的.env文件中。如果要覆盖.env中的连接,可以在项目根目录中设置config>database.php 如果您想查看查询是如何生成的,只需查找vendor\laravel\framework\src\light\Database\Query文件夹中的文件Builder.php 然后,您可以检查

我想知道所有提供数据库连接、数据制作和其他功能的SQL查询都位于何处。我试图越来越深入地研究Laravel源代码,但我发现的只是Laravel方法。我知道这很没用,但也很有趣。

SQL连接位于项目根目录中的.env文件中。如果要覆盖.env中的连接,可以在项目根目录中设置
config>database.php

如果您想查看查询是如何生成的,只需查找
vendor\laravel\framework\src\light\Database\Query
文件夹中的文件
Builder.php

然后,您可以检查
Query
文件夹中的所有其他文件,以查看雄辩的查询发生了什么

您将永远不必触摸这些文件,只需使用 了解如何使用查询生成器

连接在哪里? 您不应该直接处理SQL。这是拉威尔的一个特点。如果您对thoug感到好奇,数据库连接位于(如果您尚未发布您的供应商文件)
/vendor/laravel/framework/src/illusted/DataBaseManger.php
中的make connection,从第101行开始,我们在该行调用:

return $this->factory->make($config, $name);
这要求:

return $this->createSingleConnection($config);
其中包括:

    $pdo = $this->createPdoResolver($config);

    return $this->createConnection(
        $config['driver'], $pdo, $config['database'], $config['prefix'], $config
    );
return new MySqlConnector;
因此,我们有一个$pdo对象被传递到createConnection,它执行以下操作:

    $pdo = $this->createPdoResolver($config);

    return $this->createConnection(
        $config['driver'], $pdo, $config['database'], $config['prefix'], $config
    );
return new MySqlConnector;
哪一个连接:

   public function connect(array $config)
{
    $dsn = $this->getDsn($config);

    $options = $this->getOptions($config);

    // We need to grab the PDO options that should be used while making the brand
    // new connection instance. The PDO options control various aspects of the
    // connection's behavior, and some might be specified by the developers.
    $connection = $this->createConnection($dsn, $config, $options);

    if (! empty($config['database'])) {
        $connection->exec("use `{$config['database']}`;");
    }

    $this->configureEncoding($connection, $config);

    // Next, we will check to see if a timezone has been specified in this config
    // and if it has we will issue a statement to modify the timezone with the
    // database. Setting this DB timezone is an optional configuration item.
    $this->configureTimezone($connection, $config);

    $this->setModes($connection, $config);

    return $connection;
}
但为什么这是如此复杂和复杂?因为,您可以做一些复杂的事情,比如建立一个“连接”,使用一个数据库进行读取,另一个数据库进行写入,或者切换到不同类型的数据库(可能是非关系数据库),而不必担心更改所有代码

并实现了查询
有两个地方。Illumb有数据库查询生成器,Elounce有ORM(我相信它在引擎盖下使用QueryBuilder。但是执行发生在
Illumb/Query/Builder
get()调用受保护的方法runSelect(),该方法调用
$this->connection->select(…)
执行构建的PDO语句(位于
illighted/Database/Connection

的第330行)。有些地方有完整的查询。它们是使用雄辩的或查询生成器方法按需构建的。感谢您的解释。我终于找到了所有这些对PHPStorm Stubs-PDO.php文件的引用。但是,这些方法都在这个文件中主体为空。例如,
公共函数execute($input\u parameters=null){}
现在的主要问题是它是如何工作的。PHPStorm的存根不是实际的实现。它们只是为了让PHPStorm向您显示参数等。该函数的实际实现是PHP本身的一部分:存根是正常的、语法正确的PHP文件,包含函数和类签名、常量定义等。对于所有内置PHP内容和大多数标准扩展。存根需要包括完整的PHPDOC,特别是适当的@return注释。IDE需要它们来完成、代码检查、类型推断、文档弹出窗口等。这些服务的质量大部分取决于存根的质量(基本上是它们的PHPDOC@annotation).我只想了解底层流程。正如我在上面的评论中所写,所有这些都是对存根文件-PDO.php的引用