PHP 8.4.2 Released!

Schema::createCollection

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

Schema::createCollectionAjoute une collection au schéma

Description

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

Créer une collection dans le schéma.

Liste de paramètres

name

Le nom de la collection.

validate

La définition de validation, en tant qu'objet JSON.

Valeurs de retour

L'objet Collection.

Historique

Version Description
8.0.20 Ajout du paramètre facultatif validate.

Exemples

Exemple #1 Exemple de mysql_xdevapi\Schema::createCollection()

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

Résultat de l'exemple ci-dessus est similaire à :

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

Exemple #2 Exemple de mysql_xdevapi\Schema::createCollection()

<?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"]
}
}
}'
);
// Succès
$collection->add('{"latitude": 10, "longitude": 20}')->execute();

// Echec, types invalides (pas des nombres)
$collection->add('{"latitude": "lat", "longitude": "long"}')->execute();
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top