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 Connection

Holds the Doctrine DBAL instance.

property useUUIDs

protected bool

Flag whether to use UUIDs as primary key.

property definition

protected EntityDefinition

Holds the entity definition.

property filesystem

protected FilesystemInterface

Holds the filesystem.

property events

protected EntityEvents

Holds the events.

addSoftDeletionToQuery(EntityDefinition $definition, QueryBuilder $queryBuilder, $fieldPrefix = '', $method = 'andWhere')

Adds the soft deletion parameters if activated.

Parameters:
  • $definition (EntityDefinition) – the entity definition which might have soft deletion activated
  • $queryBuilder (QueryBuilder) – the query builder to add the deletion condition to
  • $fieldPrefix (string) – the prefix to add before the deleted_at field like an table alias
  • $method (string) – the method to use of the query builder, “where” or “andWhere”
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

deleteManyToManyReferences(Entity $entity)

Deletes any many to many references pointing to the given entity.

Parameters:
  • $entity (Entity) – the referenced entity
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

doCreate(Entity $entity)

{@inheritdoc}

Parameters:
doUpdate(Entity $entity)

{@inheritdoc}

Parameters:
__construct(EntityDefinition $definition, FilesystemInterface $filesystem, Connection $database, $useUUIDs)

Constructor.

Parameters:
  • $definition (EntityDefinition) – the entity definition
  • $filesystem (FilesystemInterface) – the filesystem to use
  • $database (Connection) – the Doctrine DBAL instance to use
  • $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
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 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

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
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

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

getEvents()

Gets the events instance.

Returns:EntityEvents the events instance
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:

boolean true on successful update

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 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