PHP Conference Japan 2024

CollectionAdd::execute

(無版本資訊可用,可能僅存在於 Git 中)

CollectionAdd::execute執行陳述式

描述

public mysql_xdevapi\CollectionAdd::execute(): mysql_xdevapi\Result

需要 execute 方法將 CRUD 操作請求傳送至 MySQL 伺服器。

參數

此函數沒有任何參數。

傳回值

一個 Result 物件,可用於驗證操作狀態,例如受影響的行數。

範例

範例 #1 mysql_xdevapi\CollectionAdd::execute() 範例

<?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");

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

// 新增兩個文件
$collection
->add('{"name": "Fred", "age": 21, "job": "Construction"}')
->
execute();

$collection
->add('{"name": "Wilma", "age": 23, "job": "Teacher"}')
->
execute();

// 使用單一 JSON 物件新增兩個文件
$result = $collection
->add(
'{"name": "Bernie",
"jobs": [{"title":"Cat Herder","Salary":42000}, {"title":"Father","Salary":0}],
"hobbies": ["Sports","Making cupcakes"]}'
,
'{"name": "Jane",
"jobs": [{"title":"Scientist","Salary":18000}, {"title":"Mother","Salary":0}],
"hobbies": ["Walking","Making pies"]}'
)
->
execute();

// 從最後一個 add() 取得產生的 ID 清單
$ids = $result->getGeneratedIds();
print_r($ids);
?>

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

Array
(
    [0] => 00005b6b53610000000000000056
    [1] => 00005b6b53610000000000000057
)
新增註記

使用者貢獻的註記

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