Documentation

ImportExport
in package

AbstractYes

Parent class for Import and Export controllers

Tags
copyright

CLOUDREXX CMS - CLOUDREXX AG

author

CLOUDREXX Development Team info@cloudrexx.com

subpackage

lib_convert

Table of Contents

Properties

$em  : EntityManager
$internalDateTimeZone  : DateTimeZone

Methods

__construct()  : mixed
Initializes this component
findBy()  : array<string|int, mixed>
Gets a set of entities by class, import/export field mapping and values
findOneBy()  : EntityBase
Gets an entity by class, import/export field mapping and values
findValueBy()  : EntityBase
Gets an entity related to $entity by import/export field mapping and values
parseMapping()  : array<string|int, mixed>
Parses data based on the mapping of fields from the file to import a single entity
applyParams()  : void
Applies params to a string
callCallback()  : mixed
Calls a callback specified
parseFieldMapping()  : mixed
Parses the field mapping for a single field
resolveCondition()  : void
Extends a query builder by the necessary joins and conditions for a filter
resolveConditions()  : void
See `resolveCondition()`
resolveFilterValues()  : array<string|int, mixed>
Resolves the value part of a filter

Properties

$internalDateTimeZone

protected DateTimeZone $internalDateTimeZone

Interal timezone

Methods

__construct()

Initializes this component

public __construct(EntityManager $em, DateTimeZone $internalDateTimeZone) : mixed
Parameters
$em : EntityManager
$internalDateTimeZone : DateTimeZone
Tags
todo

Instead we should pass $em to this or parseMapping() directly

findBy()

Gets a set of entities by class, import/export field mapping and values

public findBy(EntityManager $em, string $entityClass, array<string|int, mixed> $filter, array<string|int, mixed> $params[, int $limit = 0 ][, int $offset = 0 ]) : array<string|int, mixed>

See findOneBy(). This does the same but to get multiple entities.

Use-cases related to import/export:

  • Find all entities to be exported
Parameters
$em : EntityManager

Entity manager to use

$entityClass : string

Class of the entity to get

$filter : array<string|int, mixed>

Filter to parse

$params : array<string|int, mixed>

Additional params to parse in mapping

$limit : int = 0

(optional) Limits the number of results returned if set

$offset : int = 0

(optional) Sets the offset to start from if set

Tags
todo

Add limit/offset support

todo

Add order support

throws
If

no entity was found

Return values
array<string|int, mixed>

The entity collection found

findOneBy()

Gets an entity by class, import/export field mapping and values

public findOneBy(EntityManager $em, string $entityClass, array<string|int, mixed> $filter, array<string|int, mixed> $data, array<string|int, mixed> $params) : EntityBase

This resolves the field mapping to get a simple field/value array to match entities to. However, the field can be a path of entity properties to follow, split by ".". Additionally, a specific value for a property within the path can be supplied within square brackets ("[3]").

This can for example be used to get a user by a specific profile attribute matching a value:

findOneBy(
    $em,
    \Cx\Core\User\Model\Entity\User::class,
    array(
        'profile.attributes[3].value' => array(
            'fields' => array('myImportExportField'),
        ),
    ),
    $data = $myData,
    array()
)

The example above gets us a user ($entityClass) where the attribute 3 has the value specified in the import/export data. This follows the user's relation to the profile and its attributes.

Use-cases related to import/export:

  • Check whether the entity to import is already present in the DB
Parameters
$em : EntityManager

Entity manager to use

$entityClass : string

Class of the entity to get

$filter : array<string|int, mixed>

Filter to parse

$data : array<string|int, mixed>

Data from the import or export

$params : array<string|int, mixed>

Additional params to parse in mapping

Tags
throws
If

no or more than one entity was found

Return values
EntityBase

The single entity found

findValueBy()

Gets an entity related to $entity by import/export field mapping and values

public findValueBy(EntityBase $entity, string $propertyPath, array<string|int, mixed> $data, array<string|int, mixed> $params) : EntityBase

See findOneBy(). This does the same but based of a specific entity instead of a class name. This means that $filter does only have to point to a unique entity related to $entity instead of to a overall unique entity.

Use-cases related to import/export:

  • Set fields of related entites on import
Parameters
$entity : EntityBase

Entity to get relation of

$propertyPath : string
$data : array<string|int, mixed>

Data from the import or export

$params : array<string|int, mixed>

Additional params to parse in mapping

Tags
throws
If

no or more than one entity was found

Return values
EntityBase

The single entity found

parseMapping()

Parses data based on the mapping of fields from the file to import a single entity

public parseMapping(array<string|int, mixed> $mapping, array<string|int, mixed> $importData, array<string|int, mixed> $params[, EntityBase|null $referenceEntity = null ]) : array<string|int, mixed>

This should not be public! $mapping is a list of entity properties. For each property you can specify how the value for it is generated based on the import data. The following options are available for the mapping:

  • 'fields': A list of fields that need to be present in the import file. To make fields optional, set a default value for them, see 'defaultValue'.
  • 'defaultValue': An optional list of fields specified in 'fields' as key with the respective default value as value.
  • 'format': A string with '<...>'-style placeholders for the fields specified in 'fields'. This can be used to combine multiple fields to one.
  • 'entity': Foreign entity for this field. See 'type' "entity".
  • 'type': If set a type conversion will be applied. Known types are: "int": Result will be converted to int "DateTime": Result will be converted to \DateTime "entity": Result will be used as an argument to $em->find() on the repository for the entity specified in 'entity'.
  • 'callback': Optional callback to do custom stuff. The callback is applied last, just before the type conversion and will be given the result of previous conversions as first param and all data fields to import as second param.

Either 'fields' or 'format' must be specified. If more than one field is specified, 'format' is mandatory.

$mapping = array(
     <entityProperty> => array(
         'fields' => array(...),
         'format' => '',
         'type' => '',
         'entity' => '',
         'defaultValue' => array(...),
         'callback' => function(string $formattedString, array $fields) {
             return $formattedString;
         }
     ),
     ...
)
Parameters
$mapping : array<string|int, mixed>

Mapping according to the description above

$importData : array<string|int, mixed>

Data to import

$params : array<string|int, mixed>

Additional params to parse in mapping

$referenceEntity : EntityBase|null = null
Return values
array<string|int, mixed>

Parsed data to import

applyParams()

Applies params to a string

protected applyParams(string &$string, array<string|int, mixed> $params) : void

$params is a key-value array. For each entry in $params "" is replaced by the value in $string.

Parameters
$string : string

(reference) String to apply params to

$params : array<string|int, mixed>

List of params to apply

callCallback()

Calls a callback specified

protected callCallback(array<string|int, mixed> $data, string $offset, array<string|int, mixed> $arguments) : mixed
Parameters
$data : array<string|int, mixed>

Array to get callback from

$offset : string

Offset, delimited by "/", of $data to get callback from

$arguments : array<string|int, mixed>

Arguments to pass to the callback

Tags
throws
NoCallbackException

If the provided offset is not set

throws
IllegalCallbackException

If a callback as a direct reference to a PHP method is provided

Return values
mixed

Return value of the callback

parseFieldMapping()

Parses the field mapping for a single field

protected parseFieldMapping(array<string|int, mixed> $fieldMapping, array<string|int, mixed> $importData, string $debugFieldName, array<string|int, mixed> $params[, EntityBase|null $referenceEntity = null ]) : mixed
Parameters
$fieldMapping : array<string|int, mixed>

Mapping of the field

$importData : array<string|int, mixed>

Data to import

$debugFieldName : string

Name of the field to add to debug output

$params : array<string|int, mixed>

Additional params to parse in mapping

$referenceEntity : EntityBase|null = null
Tags
see
parseMapping()
Return values
mixed

Returns the mapped value. Its type depends on the mapping.

resolveCondition()

Extends a query builder by the necessary joins and conditions for a filter

protected resolveCondition(EntityManager $em, QueryBuilder $qb, string $entityClass, string $fieldPath, mixed $value, array<string|int, mixed> $params) : void

This turns a path and a value into a query part. The path could be something simple like "id" or something more complex like "profile.attributes[5]". The latter would match the ID field of the profile attribute with ID 5.

This assumes that "a" is the alias for the table in the main from clause. This may add relations to other tables and will alias them. To avoid conflicts it will start aliasing them at "aa" (going towards "az"). So "a" to "z" can be used outside this method without conflicts.

Parameters
$em : EntityManager

Entity manager to use

$qb : QueryBuilder

Query builder to extend

$entityClass : string

Class of the entity to get

$fieldPath : string

The path to the related field

$value : mixed

The value to match

$params : array<string|int, mixed>

Additional params to parse in mapping

resolveConditions()

See `resolveCondition()`

protected resolveConditions(EntityManager $em, QueryBuilder $qb, string $entityClass, array<string|int, mixed> $filter, array<string|int, mixed> $params) : void
Parameters
$em : EntityManager

Entity manager to use

$qb : QueryBuilder

Query builder to extend

$entityClass : string

Class of the entity to get

$filter : array<string|int, mixed>

Filter to parse

$params : array<string|int, mixed>

Additional params to parse in mapping

resolveFilterValues()

Resolves the value part of a filter

protected resolveFilterValues(array<string|int, mixed> $filter, array<string|int, mixed> $data, bool &$simple, array<string|int, mixed> $params) : array<string|int, mixed>

This simply replaces the values using the import mapping. Doing this might lead to recursion.

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

Filter to parse

$data : array<string|int, mixed>

Data from the import or export

$simple : bool

Set to true if there's no advanced filter present, false otherwise

$params : array<string|int, mixed>

Additional params to parse in mapping

Return values
array<string|int, mixed>

The updated filter with resolved values


        
On this page

Search results