當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Query::batch方法代碼示例

本文整理匯總了PHP中yii\db\Query::batch方法的典型用法代碼示例。如果您正苦於以下問題:PHP Query::batch方法的具體用法?PHP Query::batch怎麽用?PHP Query::batch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在yii\db\Query的用法示例。


在下文中一共展示了Query::batch方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: performAsCSV

 /**
  * Perform a Database Query and show the result as CSV
  *
  * By default, write to the output buffer.
  * But, if you set the filename, the file will be downloadable.
  *
  * @param string $filename Name of the file to download
  */
 public function performAsCSV($filename = null)
 {
     // Create the CSV into memory
     $csv = Writer::createFromFileObject(new SplTempFileObject());
     // Insert fields names as the CSV header
     $csv->insertOne($this->getCSVHeader());
     // Print to the output stream
     foreach ($this->query->batch() as $items) {
         // $items is an array of 100 or fewer rows from the db table
         $csv->insertAll($items);
     }
     $csv->output($filename);
 }
開發者ID:ramialcheikh,項目名稱:quickforms,代碼行數:21,代碼來源:Report.php

示例2: testQuery

 public function testQuery()
 {
     $db = $this->getConnection();
     // initialize property test
     $query = new Query();
     $query->from('customer')->orderBy('id');
     $result = $query->batch(2, $db);
     $this->assertTrue($result instanceof BatchQueryResult);
     $this->assertEquals(2, $result->batchSize);
     $this->assertTrue($result->query === $query);
     // normal query
     $query = new Query();
     $query->from('customer')->orderBy('id');
     $allRows = [];
     $batch = $query->batch(2, $db);
     foreach ($batch as $rows) {
         $allRows = array_merge($allRows, $rows);
     }
     $this->assertEquals(3, count($allRows));
     $this->assertEquals('user1', $allRows[0]['name']);
     $this->assertEquals('user2', $allRows[1]['name']);
     $this->assertEquals('user3', $allRows[2]['name']);
     // rewind
     $allRows = [];
     foreach ($batch as $rows) {
         $allRows = array_merge($allRows, $rows);
     }
     $this->assertEquals(3, count($allRows));
     // reset
     $batch->reset();
     // empty query
     $query = new Query();
     $query->from('customer')->where(['id' => 100]);
     $allRows = [];
     $batch = $query->batch(2, $db);
     foreach ($batch as $rows) {
         $allRows = array_merge($allRows, $rows);
     }
     $this->assertEquals(0, count($allRows));
     // query with index
     $query = new Query();
     $query->from('customer')->indexBy('name');
     $allRows = [];
     foreach ($query->batch(2, $db) as $rows) {
         $allRows = array_merge($allRows, $rows);
     }
     $this->assertEquals(3, count($allRows));
     $this->assertEquals('address1', $allRows['user1']['address']);
     $this->assertEquals('address2', $allRows['user2']['address']);
     $this->assertEquals('address3', $allRows['user3']['address']);
     // each
     $query = new Query();
     $query->from('customer')->orderBy('id');
     $allRows = [];
     foreach ($query->each(100, $db) as $rows) {
         $allRows[] = $rows;
     }
     $this->assertEquals(3, count($allRows));
     $this->assertEquals('user1', $allRows[0]['name']);
     $this->assertEquals('user2', $allRows[1]['name']);
     $this->assertEquals('user3', $allRows[2]['name']);
     // each with key
     $query = new Query();
     $query->from('customer')->orderBy('id')->indexBy('name');
     $allRows = [];
     foreach ($query->each(100, $db) as $key => $row) {
         $allRows[$key] = $row;
     }
     $this->assertEquals(3, count($allRows));
     $this->assertEquals('address1', $allRows['user1']['address']);
     $this->assertEquals('address2', $allRows['user2']['address']);
     $this->assertEquals('address3', $allRows['user3']['address']);
 }
開發者ID:rajanishtimes,項目名稱:basicyii,代碼行數:73,代碼來源:BatchQueryResultTest.php


注:本文中的yii\db\Query::batch方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。