2024 年 PHP 日本會議

CollectionFind::fields

(沒有版本資訊,可能只在 Git 中)

CollectionFind::fields設定文件欄位篩選器

說明

public mysql_xdevapi\CollectionFind::fields(字串 $projection): mysql_xdevapi\CollectionFind

定義查詢要返回的欄位。如果未定義,則使用所有欄位。

參數

projection

可以是單個字串或字串陣列,用於識別要為每個符合搜尋條件的文件返回的欄位。

回傳值

一個 CollectionFind 物件,可用於進一步處理。

範例

範例 #1 mysql_xdevapi\CollectionFind::fields() 範例

<?php
$session
= mysql_xdevapi\getSession("mysqlx://user:password@localhost");

$session->sql("DROP DATABASE IF EXISTS addressbook")->execute();
$session->sql("CREATE DATABASE addressbook")->execute();

$schema = $session->getSchema("addressbook");
$create = $schema->createCollection("people");

$create
->add('{"name": "Alfred", "age": 18, "job": "Butler"}')
->
execute();

// ...

$collection = $schema->getCollection("people");

$result = $collection
->find('job like :job and age > :age')
->
bind(['job' => 'Butler', 'age' => 16])
->
fields('name')
->
execute();

var_dump($result->fetchAll());
?>

上述範例將輸出類似以下的內容

array(1) {
  [0]=>
  array(1) {
    ["name"]=>
    string(6) "Alfred"
  }
}
新增筆記

使用者貢獻的筆記

此頁面沒有使用者貢獻的筆記。
To Top