2014-10-27 63 views
1

我想检查一下,如果一个Collection已经存在与ArangoDB-PHP。ArangoDB-PHP Collection exists check

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

因为我得到以下错误:

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

我如何检查是否收集已经与ArangoDB-PHP存在?

回答

1

我应该使用try/catch语句

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

它被认为是不好的风格来使用异常处理来驱动程序流 - 它应该用于真正的例外。在你的情况下,我认为,包含用户配置文件的集合的先前存在是规则,并非例外。

检查集合是否存在的正确方法是CollectionHandler::has($id)。创建集合的正确方法是使用CollectionHandler::create($collection)create接受一个字符串作为参数,即要创建的集合的名称。

$userCollectionName = '_profiles'; 

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