Import Before Run Tutorial
Estimated reading: 3 minutes
In the following example we are retrieving devices already inside Deepser’s DataBase, to avoid duplicates when importing new records from an Excel.
We’re also retrieving Companies, Types, Subtypes and Status List Values to convert their names to corresponding IDs.
/* through the static method in Deep class, a collection of CI is retrived. The collection is filtered by class_id = 2 that represents the 'Device' class*/
$deviceCollection = Deep::getResourceModel('deep_cmdb/ci_collection')->addFieldToFilter('class_id',['eq' => 2]);
/* declaring array containing device instances */
$deviceArray = [];
/* each element of the collection is saved in an associative array, having as key the CODE field (first column of the import file) and as value the instance of the Device. The instance is a PHP object with all the data inside the Database for the record */
foreach($deviceCollection as $device){
$deviceArray[$device->getCustCode()] = $device;
}
/* declaring an array containing type-subtype tree */
$subtypesTree = [];
/* declaring array used for id->name type traslation */
$typeIdentity = [];
/* retrieving CMDB Type collection through static method getResourceModel in Deep class */
$typeCollection = Deep::getResourceModel('deep_cmdb/type_collection');
/* retrieving CMDB Subtype collection through static method getResourceModel in Deep class */
$subtypeCollection = Deep::getResourceModel('deep_cmdb/subtype_collection');
foreach ($typeCollection as $type){
/* for each type, the array tree is populated assigning the name of the type converted into uppercase
and a child array as a value, whose key 'id' corresponds to the type id
es: arrayTree [typeName] ['id'] = typeId */
$subtypesTree [strtoupper($type->getName())] = ['id' => $type->getId()];
/* for each type, the identity array is configured by assigning the id as key and name
converted uppercase as value the name attribute transformed into uppercase */
$typeIdentity[$type->getId()] = strtoupper($type->getName());
}
/* for each subtype, the types in the array tree are updated by updating child array, adding the element
that represents a subtype having as key the name of the subtype transformed into uppercase, and as
value the id of the subtype. eg: arrayTree [typeName] [subtypeName] = subtypeId */
foreach ($subtypeCollection as $subtype){
$subtypesTree[$typeIdentity[$subtype->getTypeId()]] = array_merge($subtypesTree[$typeIdentity[$subtype->getTypeId()]], [strtoupper($subtype->getName()) => $subtype->getId()]);
}
/* retrieving Company collection through static method getResourceModel in Deep class */
$companyCollection = Deep::getResourceModel('deep_company/company_collection');
$companyArray = [];
foreach($companyCollection as $company){
/* for each company, the array is configured by assigning as key the company name
transformed into uppercase and company id as value */
$companyArray [strtoupper($company->getName())] = $company->getId();
}
/* Retrieving values and keys of cmdb_ci_status list
Deep::helper('deep_list') retieving list helper which is an utility class
->loadListValues('cmdb_ci_status') retieving list elements
->toOptionHash() the return collection is converted in a key = label array
array_flip wrapping keys with vakues
array_change_key_case keys case converted to uppercase */
$statusArray = array_change_key_case(array_flip(Deep::helper('deep_list')
->loadListValues('cmdb_ci_status')->toOptionHash()),CASE_UPPE