简体   繁体   English

对 Laravel 中的 cursor 语句的原始查询

[英]Raw query to cursor statement in Laravel

I have a set of raw queries that I want to transform into Illuminate\Database\Query\Builder and then fetch their data through the cursor() method.我有一组原始查询,我想将它们转换为Illuminate\Database\Query\Builder ,然后通过 cursor() 方法获取它们的数据。 For example:例如:

$queries = [];
$queries[] = 'SELECT * from users';
$queries[] = 'SELECT * from toys INNER JOIN users ON toys.owner_id = user.id';
$queries[] = 'SELECT * from toys';

$connection = \DB::connection();

foreach ($queries as $query) {
    $builder = $connection->query();
    // Pass some how the raw query to the builder
    $builder->raw($query);
    // Execute the statement with a cursor function
    foreach($builder->cursor() as $data) {
         // Do something
    }
}

The problem that I have is on how to pass the raw query to the Builder and then fetch the data trought the cursor method.我遇到的问题是如何将原始查询传递给 Builder,然后通过 cursor 方法获取数据。

Any ideas?有任何想法吗?

According to the official API documentation you should be able to use cursor() just like select() .根据官方 API 文档,您应该能够像select()一样使用cursor() ) 。

$generator = DB::cursor('SELECT * from users'); 

foreach ($generator as $data) { ... }
$queries = [];
$queries[] = 'SELECT * from users';
$queries[] = 'SELECT * from toys INNER JOIN users ON toys.owner_id = user.id';
$queries[] = 'SELECT * from toys';

foreach ($queries as $query) {
    foreach(\DB::cursor($query) as $data) {
         // Do something
    }
}

\DB::connection()->cursor($query) is the same as \DB::cursor($query) . \DB::connection()->cursor($query)\DB::cursor($query)相同。 Through the magic method __call , the DatabaseManager ( DB Facade) pipes the cursor call to the default connection.通过魔术方法__callDatabaseManagerDB Facade)将 cursor 调用通过管道传递到默认连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM