Bind Type Values During Import
Import files typically contain human-readable type names rather than numeric database IDs. This example shows how to resolve a type name from the import file to its corresponding ID in the Deepser database.
The code below binds the TYPE field by looking up the name in the $subtypesTree array (built in the Before Run hook) and assigning the matching ID to the model.
/* $value contains the value of the current column in the import file, calling trim()
method (after casting as string type) to eliminate any space char at the beginning or
at the end of the string */
$value = trim($value);
/* if $value isn't null and isn't empty */
if($value){
/* if the type name exists in Deepser. This is checked by testing if the name in the Excel file is a
key in $subtypesTree array, created in the 'Before Run' scripting area */
if (array_key_exists(strtoupper($value), $subtypesTree)) {
/* the type id is retrieved from the subtypesTree array and saved in the field
type_id of the current model */
$model->setTypeId($subtypesTree[strtoupper($value)]['id']);
}
else {
/* if the type name specified in the file doesn't exist in Deepser, then type_id
is set to null */
$model->setTypeId(null);
}
}
else {
/* if no type name has been specified in the import file, then type_id
is set to null */
$model->setTypeId(null);
}
note
The $subtypesTree array must be initialized in the Before Run scripting area. If a type name from the import file does not match any existing type, the type_id is set to null.