Documentation

ImportModel extends Model
in package
Uses Validation

Model used for importing data

Tags
author

Alexey Bobkov, Samuel Georges

Table of Contents

$attachOne  : mixed
Relations
$guarded  : array<string|int, mixed>
The attributes that aren't mass assignable.
$resultStats  : array<string|int, mixed>
$validationDefaultAttrNames  : array<string|int, mixed>
$validationErrors  : MessageBag
bootValidation()  : void
Boot the validation trait for this model.
errors()  : MessageBag
Get validation error message collection for the Model
forceSave()  : bool
Force save the model even if validation fails.
getFormatEncodingOptions()  : array<string|int, mixed>
Returns all available encodings values from the localization config
getImportFilePath()  : string
Returns an attached imported file local path, if available.
getResultStats()  : mixed
import()  : mixed
Import data based on column names matching header indexes in the CSV.
importData()  : mixed
Called when data is being imported.
isAttributeRequired()  : bool
Determines if an attribute is required based on the validation rules.
setValidationAttributeName()  : void
Programatically sets the validation attribute names, will take lower priority to model defined attribute names found in `$attributeNames`.
setValidationAttributeNames()  : void
Programatically sets multiple validation attribute names.
validate()  : bool
Validate the model instance
validated()  : void
Create a new native event for handling afterValidate().
validating()  : void
Create a new native event for handling beforeValidate().
decodeArrayValue()  : array<string|int, mixed>
Explodes a string using pipes (|) to a single dimension array
getRelationValidationValue()  : mixed
Attachments validate differently to their simple values.
getValidationAttributes()  : array<string|int, mixed>
Returns the model data used for validation.
logCreated()  : mixed
logError()  : mixed
logSkipped()  : mixed
logUpdated()  : mixed
logWarning()  : mixed
makeValidator()  : Validator
Instantiates the validator used by the validation process, depending if the class is being used inside or outside of Laravel. Optional connection string to make the validator use a different database connection than the default connection.
processImportData()  : array<string|int, mixed>
Converts column index to database column map to an array containing database column names and values pulled from the CSV file. Eg:
processImportRow()  : array<string|int, mixed>
Converts a single row of CSV data to the column map.
processRuleFieldNames()  : array<string|int, mixed>
Processes field names in a rule array.
processValidationRules()  : mixed
Process rules
processValidationUniqueRule()  : string
Rebuilds the unique validation rule to force for the existing ID

Properties

$attachOne

Relations

public mixed $attachOne = ['import_file' => [SystemModelsFile::class, 'public' => false]]

$guarded

The attributes that aren't mass assignable.

protected array<string|int, mixed> $guarded = []

$resultStats

protected array<string|int, mixed> $resultStats = ['updated' => 0, 'created' => 0, 'errors' => [], 'warnings' => [], 'skipped' => []]

Import statistics store.

$validationDefaultAttrNames

protected array<string|int, mixed> $validationDefaultAttrNames = []

Default custom attribute names.

$validationErrors

protected MessageBag $validationErrors

The message bag instance containing validation error messages

Methods

bootValidation()

Boot the validation trait for this model.

public static bootValidation() : void
Return values
void

forceSave()

Force save the model even if validation fails.

public forceSave([mixed $options = null ][, mixed $sessionKey = null ]) : bool
Parameters
$options : mixed = null
$sessionKey : mixed = null
Return values
bool

getFormatEncodingOptions()

Returns all available encodings values from the localization config

public getFormatEncodingOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>

getImportFilePath()

Returns an attached imported file local path, if available.

public getImportFilePath([mixed $sessionKey = null ]) : string
Parameters
$sessionKey : mixed = null
Return values
string

getResultStats()

public getResultStats() : mixed
Return values
mixed

import()

Import data based on column names matching header indexes in the CSV.

public import(mixed $matches[, mixed $options = [] ]) : mixed

The $matches array should be in the format of:

[ 0 => [db_name1, db_name2], 1 => [db_name3], ... ]

The key (0, 1) is the column index in the CSV and the value is another array of target database column names.

Parameters
$matches : mixed
$options : mixed = []
Return values
mixed

importData()

Called when data is being imported.

public abstract importData(mixed $results[, mixed $sessionKey = null ]) : mixed

The $results array should be in the format of:

[ 'db_name1' => 'Some value', 'db_name2' => 'Another value' ], [...]

Parameters
$results : mixed
$sessionKey : mixed = null
Return values
mixed

isAttributeRequired()

Determines if an attribute is required based on the validation rules.

public isAttributeRequired(string $attribute[, bool $checkDependencies = true ]) : bool
Parameters
$attribute : string
$checkDependencies : bool = true

Checks the attribute dependencies (for required_if & required_with rules). Note that it will only be checked up to the next level, if another dependent rule is found then it will just assume the field is required

Return values
bool

setValidationAttributeName()

Programatically sets the validation attribute names, will take lower priority to model defined attribute names found in `$attributeNames`.

public setValidationAttributeName(string $attr, string $name) : void
Parameters
$attr : string
$name : string
Return values
void

setValidationAttributeNames()

Programatically sets multiple validation attribute names.

public setValidationAttributeNames(array<string|int, mixed> $attributeNames) : void
Parameters
$attributeNames : array<string|int, mixed>
Return values
void

validate()

Validate the model instance

public validate([mixed $rules = null ][, mixed $customMessages = null ][, mixed $attributeNames = null ]) : bool
Parameters
$rules : mixed = null
$customMessages : mixed = null
$attributeNames : mixed = null
Return values
bool

validated()

Create a new native event for handling afterValidate().

public static validated(Closure|string $callback) : void
Parameters
$callback : Closure|string
Return values
void

validating()

Create a new native event for handling beforeValidate().

public static validating(Closure|string $callback) : void
Parameters
$callback : Closure|string
Return values
void

decodeArrayValue()

Explodes a string using pipes (|) to a single dimension array

protected decodeArrayValue(mixed $value[, mixed $delimeter = '|' ]) : array<string|int, mixed>
Parameters
$value : mixed
$delimeter : mixed = '|'
Return values
array<string|int, mixed>

getRelationValidationValue()

Attachments validate differently to their simple values.

protected getRelationValidationValue(mixed $relationName) : mixed
Parameters
$relationName : mixed
Return values
mixed

getValidationAttributes()

Returns the model data used for validation.

protected getValidationAttributes() : array<string|int, mixed>
Return values
array<string|int, mixed>

logCreated()

protected logCreated() : mixed
Return values
mixed

logError()

protected logError(mixed $rowIndex, mixed $message) : mixed
Parameters
$rowIndex : mixed
$message : mixed
Return values
mixed

logSkipped()

protected logSkipped(mixed $rowIndex, mixed $message) : mixed
Parameters
$rowIndex : mixed
$message : mixed
Return values
mixed

logUpdated()

protected logUpdated() : mixed
Return values
mixed

logWarning()

protected logWarning(mixed $rowIndex, mixed $message) : mixed
Parameters
$rowIndex : mixed
$message : mixed
Return values
mixed

makeValidator()

Instantiates the validator used by the validation process, depending if the class is being used inside or outside of Laravel. Optional connection string to make the validator use a different database connection than the default connection.

protected static makeValidator(mixed $data, mixed $rules, mixed $customMessages, mixed $attributeNames[, mixed $connection = null ]) : Validator
Parameters
$data : mixed
$rules : mixed
$customMessages : mixed
$attributeNames : mixed
$connection : mixed = null
Return values
Validator

processImportData()

Converts column index to database column map to an array containing database column names and values pulled from the CSV file. Eg:

protected processImportData(mixed $filePath, mixed $matches, mixed $options) : array<string|int, mixed>

[0 => [first_name], 1 => [last_name]]

Will return:

[first_name => Joe, last_name => Blogs], [first_name => Harry, last_name => Potter], [...]

Parameters
$filePath : mixed
$matches : mixed
$options : mixed
Return values
array<string|int, mixed>

processImportRow()

Converts a single row of CSV data to the column map.

protected processImportRow(mixed $rowData, mixed $matches) : array<string|int, mixed>
Parameters
$rowData : mixed
$matches : mixed
Return values
array<string|int, mixed>

processRuleFieldNames()

Processes field names in a rule array.

protected processRuleFieldNames(array<string|int, mixed> $rules) : array<string|int, mixed>

Converts any field names using array notation (ie. field[child]) into dot notation (ie. field.child)

Parameters
$rules : array<string|int, mixed>

Rules array

Return values
array<string|int, mixed>

processValidationRules()

Process rules

protected processValidationRules(mixed $rules) : mixed
Parameters
$rules : mixed
Return values
mixed

processValidationUniqueRule()

Rebuilds the unique validation rule to force for the existing ID

protected processValidationUniqueRule(string $definition, string $fieldName) : string
Parameters
$definition : string
$fieldName : string
Return values
string

Search results