CRUDlex\MySQLData

class MySQLData

MySQL Data implementation using a given Doctrine DBAL instance.

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 database

protected

Holds the Doctrine DBAL instance.

property useUUIDs

protected

Flag whether to use UUIDs as primary key.

property definition

protected

Holds the {@see EntityDefinition} entity definition.

property fileProcessor

protected

Holds the {@see FileProcessorInterface} file processor.

property events

protected

Holds the events.

setValuesAndParameters(Entity $entity, QueryBuilder $queryBuilder, $setMethod)

Sets the values and parameters of the upcoming given query according to the entity.

Parameters:
  • $entity (Entity) – the entity with its fields and values
  • $queryBuilder (QueryBuilder) – the upcoming query
  • $setMethod (string) – what method to use on the QueryBuilder: ‘setValue’ or ‘set’
hasChildren($id)

Checks whether the by id given entity still has children referencing it.

Parameters:
  • $id (integer) – the current entities id
Returns:

boolean true if the entity still has children

doDelete(Entity $entity, $deleteCascade)

{@inheritdoc}

Parameters:
  • $entity (Entity) –
  • $deleteCascade
getManyIds($fields, $params)

Gets all possible many-to-many ids existing for this definition.

Parameters:
  • $fields (array) – the many field names to fetch for
  • $params
Returns:

array an array of this many-to-many ids

addFilter(QueryBuilder $queryBuilder, $filter, $filterOperators)

Adds sorting parameters to the query.

Parameters:
  • $queryBuilder (QueryBuilder) – the query
  • $filter
  • $filterOperators
addPagination(QueryBuilder $queryBuilder, $skip, $amount)

Adds pagination parameters to the query.

Parameters:
  • $queryBuilder (QueryBuilder) – the query
  • $skip (integer|null) – the rows to skip
  • $amount (integer|null) – the maximum amount of rows
addSort(QueryBuilder $queryBuilder, $sortField, $sortAscending)

Adds sorting parameters to the query.

Parameters:
  • $queryBuilder (QueryBuilder) – the query
  • $sortField (string|null) – the sort field
  • $sortAscending (boolean|null) – true if sort ascending, false if descending
fetchReferencesForField($entities, $field)

Adds the id and name of referenced entities to the given entities. The reference field is before the raw id of the referenced entity and after the fetch, it’s an array with the keys id and name.

Parameters:
  • $entities
  • $field (string) – the reference field
generateUUID()

Generates a new UUID.

Returns:string|null the new UUID or null if this instance isn’t configured to do so
enrichWithManyField($idToData, $manyField)

Enriches the given mapping of entity id to raw entity data with some many-to-many data.

Parameters:
  • $idToData (array) – a reference to the map entity id to raw entity data
  • $manyField
enrichWithMany($rows)

Fetches to the rows belonging many-to-many entries and adds them to the rows.

Parameters:
  • $rows (array) – the rows to enrich
Returns:

array the enriched rows

saveMany(Entity $entity)

First, deletes all to the given entity related many-to-many entries from the DB and then writes them again.

Parameters:
  • $entity (Entity) – the entity to save the many-to-many entries of
enrichWithReference($entities)

Adds the id and name of referenced entities to the given entities. Each reference field is before the raw id of the referenced entity and after the fetch, it’s an array with the keys id and name.

Parameters:
  • $entities
Returns:

void

__construct(EntityDefinition $definition, FileProcessorInterface $fileProcessor, $database, $useUUIDs)

Constructor.

Parameters:
  • $definition (EntityDefinition) – the entity definition
  • $fileProcessor (FileProcessorInterface) – the file processor to use
  • $database
  • $useUUIDs (boolean) – flag whether to use UUIDs as primary key
get($id)

{@inheritdoc}

Parameters:
  • $id
listEntries($filter =, []$filterOperators =, []$skip = null, $amount = null, $sortField = null, $sortAscending = null)

{@inheritdoc}

Parameters:
  • $filter
  • $filterOperators
  • $skip
  • $amount
  • $sortField
  • $sortAscending
create(Entity $entity)

{@inheritdoc}

Parameters:
update(Entity $entity)

{@inheritdoc}

Parameters:
getIdToNameMap($entity, $nameField)

{@inheritdoc}

Parameters:
  • $entity
  • $nameField
countBy($table, $params, $paramsOperators, $excludeDeleted)

{@inheritdoc}

Parameters:
  • $table
  • $params
  • $paramsOperators
  • $excludeDeleted
hasManySet($field, $thatIds, $excludeId = null)

{@inheritdoc}

Parameters:
  • $field
  • $thatIds
  • $excludeId
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.

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

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