略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: Schema::createCollection

2024-12-20

Schema::createCollection

(No version information available, might only be in Git)

Schema::createCollectionAdd collection to schema

说明

public mysql_xdevapi\Schema::createCollection(string $name, string $validate = ?): mysql_xdevapi\Collection

Create a collection within the schema.

参数

name

Collection name.

validate

Validation definition, as a JSON object.

返回值

The Collection object.

更新日志

版本 说明
8.0.20 Added the optional validate parameter.

范例

示例 #1 Schema::createCollection example

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

$session->sql("DROP DATABASE IF EXISTS food")->execute();
$session->sql("CREATE DATABASE food")->execute();
$session->sql("CREATE TABLE food.fruit(name text, rating text)")->execute();

$schema $session->getSchema("food");
$schema->createCollection("trees");

print_r($schema->gettables());
print_r($schema->getcollections());

以上例程的输出类似于:

Array
(
    [fruit] => mysql_xdevapi\Table Object
        (
            [name] => fruit
        )
)
Array
(
    [trees] => mysql_xdevapi\Collection Object
        (
            [name] => trees
        )
)

示例 #2 Schema::createCollection validate parameter example

<?php
 $collection 
$schema->createCollection("mycollection"'{
    "validation": {
        "level": "strict",
        "schema": {
            "id": "http://json-schema.org/geo",
            "description": "A geographical coordinate",
            "type": "object",
            "properties": {
                "latitude": {
                    "type": "number"
                },
                "longitude": {
                    "type": "number"
                }
            },
            "required": ["latitude", "longitude"]
        }
    }
}'
);
// Succeeds
$collection->add('{"latitude": 10, "longitude": 20}')->execute();

// Fails, invalid types (not numbers)
$collection->add('{"latitude": "lat", "longitude": "long"}')->execute();
add a noteadd a note

User Contributed Notes

There are no user contributed notes for this page.

官方地址:https://www.php.net/manual/en/mysql-xdevapi-schema.createcollection.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3