Skip to main content

Bind a Company During Import

This example demonstrates how to bind the Owner Company model field using a company name from the import file. If the company does not already exist in Deepser, the script automatically creates it during the import process.

The code below sets the Owner Company model field with the ID of a company specified by name in the import file. If no matching company is found, a new one is created.


/* $value contains the value of the current column in the import file, calling trim()
method (after casting as string type) to eliminate any space at the beginning or
at the end of the string */
$value = trim((string)$value);
/* if the company name field specified in the import file is not empty */
if($value) {
/* if the company name (transformed into uppercase) doesn't exist in DeepDesk.
Here we are checking the keys of $companyArray created in the 'Before Run' area */
if(!array_key_exists(strtoupper($value), $companyArray)){
/* a new Company object is created through the static getModel() method of the Deep class (
via the abstract factory pattern)*/
$companyModel = Deep::getModel('deep_company/company');
$companyModel->setName($value);
$companyModel->setStatus(1);
/* saving the company to Database after setting the name and the status (1 = Enabled) */
$companyModel->save();
/* adding the company just created to the $companyArray */
$companyArray[strtoupper($value)] = $companyModel->getId();
}
/* the field owner_company_id of the CI is set here.
The id is calculated starting from the name of the company specified in the import file */
$model->setOwnerCompanyId($companyArray[strtoupper($value)]);
}

note

The $companyArray used in this code must be initialized in the Before Run scripting area. Newly created companies are added to the array so that subsequent rows referencing the same company do not create duplicates.