# CRUDlex\AbstractData

- CRUDlex\MySQLData

class `AbstractData`

  The abstract class for reading and writing data.

  constant `DELETION_SUCCESS`

    Return value on successful deletion.

  constant `DELETION_FAILED_STILL_REFERENCED`

    Return value on failed deletion due to existing references.

  constant `DELETION_FAILED_EVENT`

    Return value on failed deletion due to a failed before delete event.

  property `definition`

    protected

    Holds the {@see EntityDefinition} entity definition.

  property `fileProcessor`

    protected

    Holds the {@see FileProcessorInterface} file processor.

  property `events`

    protected

    Holds the events.

  `doDelete`(Entity $entity, $deleteCascade)

    Performs the actual deletion.

    Parameters: | $entity (Entity) – the id of the entry to delete $deleteCascade (boolean) – whether to delete children and subchildren
    Returns: | integer true on successful deletion

  `hydrate`($row)

    Creates an {@see Entity} from the raw data array with the field name as keys and field values as values.

    Parameters: | $row (array) – the array with the raw data
    Returns: | Entity the entity containing the array data then

  `shouldExecuteEvents`(Entity $entity, $moment, $action)

    Executes the event chain of an entity.

    Parameters: | $entity (Entity) – the entity having the event chain to execute $moment (string) – the “moment” of the event, can be either “before” or “after” $action (string) – the “action” of the event, can be either “create”, “update” or “delete”
    Returns: | boolean true on successful execution of the full chain or false if it broke at any point (and stopped the execution)

  `performOnFiles`(Entity $entity, $entityName, $function)

    Executes a function for each file field of this entity.

    Parameters: | $entity (Entity) – the just created entity $entityName (string) – the name of the entity as this class here is not aware of it $function (Closure) – the function to perform, takes $entity, $entityName and $field as parameter

  `enrichEntityWithMetaData`($id, Entity $entity)

    Enriches an entity with metadata: id, version, created_at, updated_at

    Parameters: | $id (mixed) – the id of the entity to enrich $entity (Entity) – the entity to enrich

  `getManyFields`()

    Gets the many-to-many fields.

    Returns: | array|string[] the many-to-many fields

  `getFormFields`()

    Gets all form fields including the many-to-many-ones.

    Returns: | array all form fields

  `deleteChildren`($id, $deleteCascade)

    Performs the cascading children deletion.

    Parameters: | $id (integer) – the current entities id $deleteCascade (boolean) – whether to delete children and sub children

  `getReferenceIds`($entities, $field)

    Gets an array of reference ids for the given entities.

    Parameters: | $entities (array) – the entities to extract the ids $field (string) – the reference field
    Returns: | array the extracted ids

  `pushEvent`($moment, $action, Closure $function)

    Adds an event to fire for the given parameters. The event function must have this signature: function (Entity $entity) and has to return true or false. The events are executed one after another in the added order as long as they return “true”. The first event returning “false” will stop the process.

    Parameters: | $moment (string) – the “moment” of the event, can be either “before” or “after” $action (string) – the “action” of the event, can be either “create”, “update” or “delete” $function (Closure) – the event function to be called if set

  `popEvent`($moment, $action)

    Removes and returns the latest event for the given parameters.

    Parameters: | $moment (string) – the “moment” of the event, can be either “before” or “after” $action (string) – the “action” of the event, can be either “create”, “update” or “delete”
    Returns: | Closure|null the popped event or null if no event was available.

  `get`($id)

    Gets the entity with the given id.

    Parameters: | $id (string) – the id
    Returns: | Entity the entity belonging to the id or null if not existant

  `listEntries`($filter =, []$filterOperators =, []$skip = null, $amount = null, $sortField = null, $sortAscending = null)

    Gets a list of entities fullfilling the given filter or all if no selection was given.

    Parameters: | $filter (array) – the filter all resulting entities must fulfill, the keys as field names $filterOperators (array) – the operators of the filter like “=” defining the full condition of the field $skip (integer|null) – if given and not null, it specifies the amount of rows to skip $amount (integer|null) – if given and not null, it specifies the maximum amount of rows to retrieve $sortField (string|null) – if given and not null, it specifies the field to sort the entries $sortAscending (boolean|null) – if given and not null, it specifies that the sort order is ascending, descending else
    Returns: | Entity[] the entities fulfilling the filter or all if no filter was given

  `create`(Entity $entity)

    Persists the given entity as new entry in the datasource.

    Parameters: | $entity (Entity) – the entity to persist
    Returns: | boolean true on successful creation

  `update`(Entity $entity)

    Updates an existing entry in the datasource having the same id.

    Parameters: | $entity (Entity) – the entity with the new data
    Returns: | void

  `delete`($entity)

    Deletes an entry from the datasource.

    Parameters: | $entity (Entity) – the entity to delete
    Returns: | integer returns one of: - AbstractData::DELETION_SUCCESS -> successful deletion - AbstractData::DELETION_FAILED_STILL_REFERENCED -> failed deletion due to existing references - AbstractData::DELETION_FAILED_EVENT -> failed deletion due to a failed before delete event

  `getIdToNameMap`($entity, $nameField)

    Gets ids and names of a table. Used for building up the dropdown box of reference type fields for example.

    Parameters: | $entity (string) – the entity $nameField (string) – the field defining the name of the rows
    Returns: | array an array with the ids as key and the names as values

  `countBy`($table, $params, $paramsOperators, $excludeDeleted)

    Retrieves the amount of entities in the datasource fulfilling the given parameters.

    Parameters: | $table (string) – the table to count in $params (array) – an array with the field names as keys and field values as values $paramsOperators (array) – the operators of the parameters like “=” defining the full condition of the field $excludeDeleted (boolean) – false, if soft deleted entries in the datasource should be counted, too
    Returns: | integer the count fulfilling the given parameters

  `hasManySet`($field, $thatIds, $excludeId = null)

    Checks whether a given set of ids is assigned to any entity exactly like it is given (no subset, no superset).

    Parameters: | $field (string) – the many field $thatIds (array) – the id set to check $excludeId (string|null) – one optional own id to exclude from the check
    Returns: | boolean true if the set of ids exists for an entity

  `getDefinition`()

    Gets the {@see EntityDefinition} instance.

    Returns: | EntityDefinition the definition instance

  `createEmpty`()

    Creates a new, empty entity instance having all fields prefilled with null or the defined value in case of fixed fields.

    Returns: | Entity the newly created entity

  `createFiles`(Request $request, Entity $entity, $entityName)

    Creates the uploaded files of a newly created entity.

    Parameters: | $request (Request) – the HTTP request containing the file data $entity (Entity) – the just created entity $entityName (string) – the name of the entity as this class here is not aware of it
    Returns: | boolean true if all before events passed

  `updateFiles`(Request $request, Entity $entity, $entityName)

    Updates the uploaded files of an updated entity.

    Parameters: | $request (Request) – the HTTP request containing the file data $entity (Entity) – the updated entity $entityName (string) – the name of the entity as this class here is not aware of it
    Returns: | boolean true on successful update

  `deleteFile`(Entity $entity, $entityName, $field)

    Deletes a specific file from an existing entity.

    Parameters: | $entity (Entity) – the entity to delete the file from $entityName (string) – the name of the entity as this class here is not aware of it $field (string) – the field of the entity containing the file to be deleted
    Returns: | boolean true on successful deletion

  `deleteFiles`(Entity $entity, $entityName)

    Deletes all files of an existing entity.

    Parameters: | $entity (Entity) – the entity to delete the files from $entityName (string) – the name of the entity as this class here is not aware of it
    Returns: | boolean true on successful deletion

  `renderFile`(Entity $entity, $entityName, $field)

    Renders (outputs) a file of an entity. This includes setting headers like the file size, mimetype and name, too.

    Parameters: | $entity (Entity) – the entity to render the file from $entityName (string) – the name of the entity as this class here is not aware of it $field (string) – the field of the entity containing the file to be rendered
    Returns: | Response the HTTP response, likely to be a streamed one