2014-10-27 17 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);