简体   繁体   English

ArangoDB-PHP集合是否存在检查

[英]ArangoDB-PHP Collection exists check

I want to make a check if a Collection already exist with ArangoDB-PHP. 我想检查ArangoDB-PHP是否已经存在一个Collection。

$collectionHandler = new CollectionHandler($arango);
$userCollection = new Collection();
$userCollection->setName('_profiles');

Because I get the following error: 因为我收到以下错误:

Server error: 1207:cannot create collection: duplicate name cannot create collection: duplicate name

How can I check if a collection already exists with ArangoDB-PHP? 如何检查ArangoDB-PHP是否已经存在集合?

I should use try/catch statement 我应该使用try / catch语句

try { 
    $collectionHandler = new CollectionHandler($arango);
    $userCollection = new Collection();
    $userCollection->setName('_profiles');
    $collectionHandler->create($userCollection);
} catch (ServerException $e) {
    // do something
}

It is considered bad style to use exception handling to drive program flow -- it should be used for real exceptions. 使用异常处理来驱动程序流被认为是不好的样式-应该将其用于实际的异常。 In your case the prior existence of the collection containing user profiles is the rule, I assume, not an exception. 在您的情况下,我认为,包含用户配置文件的集合的先前存在不是例外。

The correct way to check for the existence of a collection is CollectionHandler::has($id) . 检查集合是否存在的正确方法是CollectionHandler::has($id) The correct way to create a collection is to use CollectionHandler::create($collection) . 创建集合的正确方法是使用CollectionHandler::create($collection) create accepts a string as parameter, the name of collection to create. create接受一个字符串作为参数,即要创建的集合的名称。

$userCollectionName = '_profiles';

$collectionHandler = new CollectionHandler($arango);
$userCollection = $collectionHandler->has($userCollectionName) ?
    $collectionHandler->get($userCollectionName) 
    : 
    $collectionHandler->create($userCollectionName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM