(無版本資訊可用,可能僅存在於 Git 中)
CollectionAdd::execute — 執行陳述式
需要 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 )