Fr3nch13/CakePHP Utilities

AssociationCollection
in package
implements IteratorAggregate uses AssociationsNormalizerTrait, LocatorAwareTrait

A container/collection for association classes.

Contains methods for managing associations, and ordering operations around saving and deleting.

Table of Contents

Interfaces

IteratorAggregate

Properties

$_items  : array<string|int, Association>
Stored associations
$_tableLocator  : LocatorInterface|null
Table locator instance
$defaultTable  : string|null
This object's default table alias.

Methods

__construct()  : mixed
Constructor.
add()  : Association
Add an association to the collection
cascadeDelete()  : bool
Cascade a delete across the various associations.
fetchTable()  : Table
Convenience method to get a table instance.
get()  : Association|null
Fetch an attached association by name.
getByProperty()  : Association|null
Fetch an association by property name.
getByType()  : array<string|int, Association>
Get an array of associations matching a specific type.
getIterator()  : Traversable<string, Association>
Allow looping through the associations
getTableLocator()  : LocatorInterface
Gets the table locator.
has()  : bool
Check for an attached association by name.
keys()  : array<string|int, string>
Get the names of all the associations in the collection.
load()  : Association
Creates and adds the Association object to this collection.
normalizeKeys()  : array<string|int, mixed>
Returns an associative array of association names out a mixed array. If true is passed, then it returns all association names in this collection.
remove()  : void
Drop/remove an association.
removeAll()  : void
Remove all registered associations.
saveChildren()  : bool
Save all the associations that are children of the given entity.
saveParents()  : bool
Save all the associations that are parents of the given entity.
setTableLocator()  : $this
Sets the table locator.
_normalizeAssociations()  : array<string|int, mixed>
Returns an array out of the original passed associations list where dot notation is transformed into nested arrays so that they can be parsed by other routines
_save()  : bool
Helper method for saving an association's data.
_saveAssociations()  : bool
Helper method for saving an association's data.

Properties

$defaultTable

This object's default table alias.

protected string|null $defaultTable = null

Methods

__construct()

Constructor.

public __construct([LocatorInterface|null $tableLocator = null ]) : mixed

Sets the default table locator for associations. If no locator is provided, the global one will be used.

Parameters
$tableLocator : LocatorInterface|null = null

Table locator instance.

add()

Add an association to the collection

public add(string $alias, Association $association) : Association

If the alias added contains a . the part preceding the . will be dropped. This makes using plugins simpler as the Plugin.Class syntax is frequently used.

Parameters
$alias : string

The association alias

$association : Association

The association to add.

Tags
template

T of \Cake\ORM\Association

psalm-param

T $association

psalm-return

T

Return values
Association

The association object being added.

cascadeDelete()

Cascade a delete across the various associations.

public cascadeDelete(EntityInterface $entity, array<string, mixed> $options) : bool

Cascade first across associations for which cascadeCallbacks is true.

Parameters
$entity : EntityInterface

The entity to delete associations for.

$options : array<string, mixed>

The options used in the delete operation.

Return values
bool

fetchTable()

Convenience method to get a table instance.

public fetchTable([string|null $alias = null ][, array<string, mixed> $options = [] ]) : Table
Parameters
$alias : string|null = null

The alias name you want to get. Should be in CamelCase format. If null then the value of $defaultTable property is used.

$options : array<string, mixed> = []

The options you want to build the table with. If a table has already been loaded the registry options will be ignored.

Tags
throws
CakeException

If $alias argument and $defaultTable property both are null.

see
TableLocator::get()
since
4.3.0
Return values
Table

get()

Fetch an attached association by name.

public get(string $alias) : Association|null
Parameters
$alias : string

The association alias to get.

Return values
Association|null

Either the association or null.

getByProperty()

Fetch an association by property name.

public getByProperty(string $prop) : Association|null
Parameters
$prop : string

The property to find an association by.

Return values
Association|null

Either the association or null.

getByType()

Get an array of associations matching a specific type.

public getByType(array<string|int, string>|string $class) : array<string|int, Association>
Parameters
$class : array<string|int, string>|string

The type of associations you want. For example 'BelongsTo' or array like ['BelongsTo', 'HasOne']

Tags
since
3.5.3
Return values
array<string|int, Association>

An array of Association objects.

has()

Check for an attached association by name.

public has(string $alias) : bool
Parameters
$alias : string

The association alias to get.

Return values
bool

Whether the association exists.

keys()

Get the names of all the associations in the collection.

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

load()

Creates and adds the Association object to this collection.

public load(string $className, string $associated[, array<string, mixed> $options = [] ]) : Association
Parameters
$className : string

The name of association class.

$associated : string

The alias for the target table.

$options : array<string, mixed> = []

List of options to configure the association definition.

Tags
throws
InvalidArgumentException
template

T of \Cake\ORM\Association

psalm-param

class-string<T> $className

psalm-return

T

Return values
Association

normalizeKeys()

Returns an associative array of association names out a mixed array. If true is passed, then it returns all association names in this collection.

public normalizeKeys(array<string|int, mixed>|bool $keys) : array<string|int, mixed>
Parameters
$keys : array<string|int, mixed>|bool

the list of association names to normalize

Return values
array<string|int, mixed>

remove()

Drop/remove an association.

public remove(string $alias) : void

Once removed the association will no longer be reachable

Parameters
$alias : string

The alias name.

removeAll()

Remove all registered associations.

public removeAll() : void

Once removed associations will no longer be reachable

saveChildren()

Save all the associations that are children of the given entity.

public saveChildren(Table $table, EntityInterface $entity, array<string|int, mixed> $associations, array<string, mixed> $options) : bool

Child associations include any association where the given table is not the owning side.

Parameters
$table : Table

The table entity is for.

$entity : EntityInterface

The entity to save associated data for.

$associations : array<string|int, mixed>

The list of associations to save children from. associations not in this list will not be saved.

$options : array<string, mixed>

The options for the save operation.

Return values
bool

Success

saveParents()

Save all the associations that are parents of the given entity.

public saveParents(Table $table, EntityInterface $entity, array<string|int, mixed> $associations[, array<string, mixed> $options = [] ]) : bool

Parent associations include any association where the given table is the owning side.

Parameters
$table : Table

The table entity is for.

$entity : EntityInterface

The entity to save associated data for.

$associations : array<string|int, mixed>

The list of associations to save parents from. associations not in this list will not be saved.

$options : array<string, mixed> = []

The options for the save operation.

Return values
bool

Success

_normalizeAssociations()

Returns an array out of the original passed associations list where dot notation is transformed into nested arrays so that they can be parsed by other routines

protected _normalizeAssociations(array<string|int, mixed>|string $associations) : array<string|int, mixed>
Parameters
$associations : array<string|int, mixed>|string

The array of included associations.

Return values
array<string|int, mixed>

An array having dot notation transformed into nested arrays

_save()

Helper method for saving an association's data.

protected _save(Association $association, EntityInterface $entity, array<string, mixed> $nested, array<string, mixed> $options) : bool
Parameters
$association : Association

The association object to save with.

$entity : EntityInterface

The entity to save

$nested : array<string, mixed>

Options for deeper associations

$options : array<string, mixed>

Original options

Return values
bool

Success

_saveAssociations()

Helper method for saving an association's data.

protected _saveAssociations(Table $table, EntityInterface $entity, array<string|int, mixed> $associations, array<string, mixed> $options, bool $owningSide) : bool
Parameters
$table : Table

The table the save is currently operating on

$entity : EntityInterface

The entity to save

$associations : array<string|int, mixed>

Array of associations to save.

$options : array<string, mixed>

Original options

$owningSide : bool

Compared with association classes' isOwningSide method.

Tags
throws
InvalidArgumentException

When an unknown alias is used.

Return values
bool

Success


        
On this page

Search results