Skip to content
MNB PHPExcelDeveloper Guide
AI

Database library

Column Mapping and Validation

Preview headers, suggest mappings, normalize values, validate records, and preserve original source row numbers.

Level
Intermediate
Reading time
10 min
Package
mnb/mnb-phpexcel-database
Updated
ImportQualityAnalyzer::suggestColumnMap()DomainImporter::preview()ArrayValidator::validate()

Install the source reader you need#

DomainImporter resolves files through the shared reader registry. Install the format library for the incoming file, such as CSV, XLS, or XLSX, in addition to Database.

Preview before import#

PHP
use Mnb\PHPExcel\Import\DomainImporter;
use Mnb\PHPExcel\Domain\DomainImportType;

$importer = DomainImporter::create();
$preview = $importer->preview(DomainImportType::Products, 'products.xlsx', [
    'preview_rows' => 20,
    'header_row' => 'auto',
    'header_detection_rows' => 25,
    'mapping_min_confidence' => 0.60,
]);

print_r($preview['mapping']);
print_r($preview['sample_rows']);

Override mappings and validation#

PHP
$result = $importer->importProducts('products.xlsx', $pdo, 'products', [
    'column_map' => [
        'Product Code' => 'sku',
        'Item Name' => 'name',
        'Unit Price' => 'price',
    ],
    'rules' => [
        'sku' => ['required'],
        'name' => ['required'],
        'price' => ['required', 'numeric', 'min:0'],
    ],
    'strict_mapping' => true,
    'skip_invalid_rows' => true,
]);