PHP Класс Cake\ORM\Association\BelongsToMany

An example of a BelongsToMany association would be Article belongs to many Tags.
Наследование: extends Cake\ORM\Association, use trait Cake\ORM\Association\ExternalAssociationTrait
Показать файл Открыть проект Примеры использования класса

Защищенные свойства (Protected)

Свойство Тип Описание
$_dependent boolean Defaults to true for backwards compatibility.
$_joinType string The type of join to be used when adding the association to a query
$_junctionAssociationName string The name of the hasMany association from the target table to the junction table
$_junctionConditions null | array Filtered conditions that reference the junction table.
$_junctionProperty string The name of the property to be set containing data from the junction table once a record from the target table is hydrated
$_junctionTable Cake\ORM\Table Junction table instance
$_junctionTableName string Junction table name
$_saveStrategy string Saving strategy to be used by this association
$_strategy string The strategy name to be used to fetch associated records.
$_targetConditions null | array Filtered conditions that reference the target table.
$_targetForeignKey string | array The name of the field representing the foreign key to the target table
$_through string | Cake\ORM\Table The table instance for the junction relation.
$_validStrategies array Valid strategies for this type of association

Открытые методы

Метод Описание
_buildBaseQuery ( )
_externalOptions ( )
attachTo ( Query $query, array $options = [] ) : void Alters a Query object to include the associated target table data in the final result
cascadeDelete ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : boolean Clear out the data in the junction table for a given entity.
conditions ( $conditions = null ) {@inheritDoc}
find ( string | array | null $type = null, array $options = [] ) : Query Proxies the finding operation to the target table's find method and modifies the query accordingly based of this association configuration.
isOwningSide ( Table $side ) : boolean Returns boolean true, as both of the tables 'own' rows in the other side of the association via the joint table.
junction ( string | Table | null $table = null ) : Table Sets the table instance for the junction relation. If no arguments are passed, the current configured table instance is returned
link ( Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities, array $options = [] ) : boolean Associates the source entity to each of the target entities provided by creating links in the junction table. Both the source entity and each of the target entities are assumed to be already persisted, if they are marked as new or their status is unknown then an exception will be thrown.
replaceLinks ( Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities, array $options = [] ) : boolean Replaces existing association links between the source entity and the target with the ones passed. This method does a smart cleanup, links that are already persisted and present in $targetEntities will not be deleted, new links will be created for the passed target entities that are not already in the database and the rest will be removed.
saveAssociated ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : boolean | Cake\Datasource\EntityInterface Takes an entity from the source table and looks if there is a field matching the property name for this association. The found entity will be saved on the target table for this association by passing supplied $options
saveStrategy ( string | null $strategy = null ) : string Sets the strategy that should be used for saving. If called with no arguments, it will return the currently configured strategy
targetForeignKey ( string | null $key = null ) : string Sets the name of the field representing the foreign key to the target table.
type ( ) : string Get the relationship type.
unlink ( Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities, array | boolean $options = [] ) : boolean Removes all links between the passed source entity and each of the provided target entities. This method assumes that all passed objects are already persisted in the database and that each of them contain a primary key value.

Защищенные методы

Метод Описание
_appendJunctionJoin ( Query $query, string | array $conditions ) : Query Append a join to the junction table.
_appendNotMatching ( $query, $options ) {@inheritDoc}
_buildQuery ( array $options ) : Query Auxiliary function to construct a new Query object to return all the records in the target table that are associated to those specified in $options from the source table.
_buildResultMap ( Query $fetchQuery, array $options ) : array Builds an array containing the results from fetchQuery indexed by the foreignKey value corresponding to this association.
_checkPersistenceStatus ( Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities ) : boolean Throws an exception should any of the passed entities is not persisted.
_collectJointEntities ( Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities ) : array Returns the list of joint entities that exist between the source entity and each of the passed target entities
_diffLinks ( Query $existing, array $jointEntities, array $targetEntities, array $options = [] ) : array Helper method used to delete the difference between the links passed in $existing and $jointEntities. This method will return the values from $targetEntities that were not deleted from calculating the difference.
_generateJunctionAssociations ( Table $junction, Table $source, Table $target ) : void Generate associations on the junction table as necessary
_generateSourceAssociations ( Table $junction, Table $source ) : void Generate additional source table associations as necessary.
_generateTargetAssociations ( Table $junction, Table $source, Table $target ) : void Generate reciprocal associations as necessary.
_joinCondition ( array $options ) : boolean Return false as join conditions are defined in the junction table
_junctionAssociationName ( ) : string Returns the name of the association from the target table to the junction table, this name is used to generate alias in the query and to later on retrieve the results.
_junctionTableName ( string | null $name = null ) : string Sets the name of the junction table.
_linkField ( array $options ) : string Generates a string used as a table field that contains the values upon which the filter should be applied
_options ( array $opts ) : void Parse extra options passed in the constructor.
_saveLinks ( Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities, array $options ) : boolean Creates links between the source entity and each of the passed target entities
_saveTarget ( Cake\Datasource\EntityInterface $parentEntity, array | Traversable $entities, array $options ) : Cake\Datasource\EntityInterface | boolean Persists each of the entities into the target table and creates links between the parent entity and each one of the saved target entities.
junctionConditions ( ) : array Returns filtered conditions that specifically reference the junction table.
targetConditions ( ) : mixed Returns filtered conditions that reference the target table.

Описание методов

_appendJunctionJoin() защищенный Метод

Append a join to the junction table.
protected _appendJunctionJoin ( Query $query, string | array $conditions ) : Query
$query Cake\ORM\Query The query to append.
$conditions string | array The query conditions to use.
Результат Cake\ORM\Query The modified query.

_appendNotMatching() защищенный Метод

{@inheritDoc}
protected _appendNotMatching ( $query, $options )

_buildBaseQuery() публичный Метод

public _buildBaseQuery ( )

_buildQuery() защищенный Метод

This is used for eager loading records on the target table based on conditions.
protected _buildQuery ( array $options ) : Query
$options array options accepted by eagerLoader()
Результат Cake\ORM\Query

_buildResultMap() защищенный Метод

Builds an array containing the results from fetchQuery indexed by the foreignKey value corresponding to this association.
protected _buildResultMap ( Query $fetchQuery, array $options ) : array
$fetchQuery Cake\ORM\Query The query to get results from
$options array The options passed to the eager loader
Результат array

_checkPersistenceStatus() защищенный Метод

Throws an exception should any of the passed entities is not persisted.
protected _checkPersistenceStatus ( Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities ) : boolean
$sourceEntity Cake\Datasource\EntityInterface the row belonging to the `source` side of this association
$targetEntities array list of entities belonging to the `target` side of this association
Результат boolean

_collectJointEntities() защищенный Метод

Returns the list of joint entities that exist between the source entity and each of the passed target entities
protected _collectJointEntities ( Cake\Datasource\EntityInterface $sourceEntity, array $targetEntities ) : array
$sourceEntity Cake\Datasource\EntityInterface The row belonging to the source side of this association.
$targetEntities array The rows belonging to the target side of this association.
Результат array

_externalOptions() публичный Метод

public _externalOptions ( )

_generateJunctionAssociations() защищенный Метод

Generates the following associations: - junction belongsTo source e.g. ArticlesTags belongsTo Tags - junction belongsTo target e.g. ArticlesTags belongsTo Articles You can override these generated associations by defining associations with the correct aliases.
protected _generateJunctionAssociations ( Table $junction, Table $source, Table $target ) : void
$junction Cake\ORM\Table The junction table.
$source Cake\ORM\Table The source table.
$target Cake\ORM\Table The target table.
Результат void

_generateSourceAssociations() защищенный Метод

Generates the following associations: - source hasMany junction e.g. Tags hasMany ArticlesTags You can override these generated associations by defining associations with the correct aliases.
protected _generateSourceAssociations ( Table $junction, Table $source ) : void
$junction Cake\ORM\Table The junction table.
$source Cake\ORM\Table The source table.
Результат void

_generateTargetAssociations() защищенный Метод

Generates the following associations: - target hasMany junction e.g. Articles hasMany ArticlesTags - target belongsToMany source e.g Articles belongsToMany Tags. You can override these generated associations by defining associations with the correct aliases.
protected _generateTargetAssociations ( Table $junction, Table $source, Table $target ) : void
$junction Cake\ORM\Table The junction table.
$source Cake\ORM\Table The source table.
$target Cake\ORM\Table The target table.
Результат void

_joinCondition() защищенный Метод

Return false as join conditions are defined in the junction table
protected _joinCondition ( array $options ) : boolean
$options array list of options passed to attachTo method
Результат boolean false

_junctionAssociationName() защищенный Метод

Returns the name of the association from the target table to the junction table, this name is used to generate alias in the query and to later on retrieve the results.
protected _junctionAssociationName ( ) : string
Результат string

_junctionTableName() защищенный Метод

If no arguments are passed the current configured name is returned. A default name based of the associated tables will be generated if none found.
protected _junctionTableName ( string | null $name = null ) : string
$name string | null The name of the junction table.
Результат string

_linkField() защищенный Метод

Generates a string used as a table field that contains the values upon which the filter should be applied
protected _linkField ( array $options ) : string
$options array the options to use for getting the link field.
Результат string

_options() защищенный Метод

Parse extra options passed in the constructor.
protected _options ( array $opts ) : void
$opts array original list of options passed in constructor
Результат void

_saveTarget() защищенный Метод

Persists each of the entities into the target table and creates links between the parent entity and each one of the saved target entities.
protected _saveTarget ( Cake\Datasource\EntityInterface $parentEntity, array | Traversable $entities, array $options ) : Cake\Datasource\EntityInterface | boolean
$parentEntity Cake\Datasource\EntityInterface the source entity containing the target entities to be saved.
$entities array | Traversable list of entities to persist in target table and to link to the parent entity
$options array list of options accepted by `Table::save()`
Результат Cake\Datasource\EntityInterface | boolean The parent entity after all links have been created if no errors happened, false otherwise

attachTo() публичный Метод

The options array accept the following keys: - includeFields: Whether to include target model fields in the result or not - foreignKey: The name of the field to use as foreign key, if false none will be used - conditions: array with a list of conditions to filter the join with - fields: a list of fields in the target table to include in the result - type: The type of join to be used (e.g. INNER)
public attachTo ( Query $query, array $options = [] ) : void
$query Cake\ORM\Query the query to be altered to include the target table data
$options array Any extra options or overrides to be taken in account
Результат void

cascadeDelete() публичный Метод

Clear out the data in the junction table for a given entity.
public cascadeDelete ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : boolean
$entity Cake\Datasource\EntityInterface The entity that started the cascading delete.
$options array The options for the original delete.
Результат boolean Success.

conditions() публичный Метод

{@inheritDoc}
public conditions ( $conditions = null )

find() публичный Метод

If your association includes conditions, the junction table will be included in the query's contained associations.
См. также: Cake\ORM\Table::find()
public find ( string | array | null $type = null, array $options = [] ) : Query
$type string | array | null the type of query to perform, if an array is passed, it will be interpreted as the `$options` parameter
$options array The options to for the find
Результат Cake\ORM\Query

isOwningSide() публичный Метод

Returns boolean true, as both of the tables 'own' rows in the other side of the association via the joint table.
public isOwningSide ( Table $side ) : boolean
$side Cake\ORM\Table The potential Table with ownership
Результат boolean

junction() публичный Метод

Sets the table instance for the junction relation. If no arguments are passed, the current configured table instance is returned
public junction ( string | Table | null $table = null ) : Table
$table string | Cake\ORM\Table | null Name or instance for the join table
Результат Cake\ORM\Table

junctionConditions() защищенный Метод

Returns filtered conditions that specifically reference the junction table.
protected junctionConditions ( ) : array
Результат array

saveAssociated() публичный Метод

When using the 'append' strategy, this function will only create new links between each side of this association. It will not destroy existing ones even though they may not be present in the array of entities to be saved. When using the 'replace' strategy, existing links will be removed and new links will be created in the joint table. If there exists links in the database to some of the entities intended to be saved by this method, they will be updated, not deleted.
См. также: Cake\ORM\Table::save()
См. также: Cake\ORM\Association\BelongsToMany::replaceLinks()
public saveAssociated ( Cake\Datasource\EntityInterface $entity, array $options = [] ) : boolean | Cake\Datasource\EntityInterface
$entity Cake\Datasource\EntityInterface an entity from the source table
$options array options to be passed to the save method in the target table
Результат boolean | Cake\Datasource\EntityInterface false if $entity could not be saved, otherwise it returns the saved entity

saveStrategy() публичный Метод

Sets the strategy that should be used for saving. If called with no arguments, it will return the currently configured strategy
public saveStrategy ( string | null $strategy = null ) : string
$strategy string | null the strategy name to be used
Результат string the strategy to be used for saving

targetConditions() защищенный Метод

Any string expressions, or expression objects will also be returned in this list.
protected targetConditions ( ) : mixed
Результат mixed Generally an array. If the conditions are not an array, the association conditions will be returned unmodified.

targetForeignKey() публичный Метод

If no parameters are passed current field is returned
public targetForeignKey ( string | null $key = null ) : string
$key string | null the key to be used to link both tables together
Результат string

type() публичный Метод

Get the relationship type.
public type ( ) : string
Результат string

Описание свойств

$_dependent защищенное свойство

Defaults to true for backwards compatibility.
protected bool $_dependent
Результат boolean

$_joinType защищенное свойство

The type of join to be used when adding the association to a query
protected string $_joinType
Результат string

$_junctionAssociationName защищенное свойство

The name of the hasMany association from the target table to the junction table
protected string $_junctionAssociationName
Результат string

$_junctionConditions защищенное свойство

Filtered conditions that reference the junction table.
protected null|array $_junctionConditions
Результат null | array

$_junctionProperty защищенное свойство

The name of the property to be set containing data from the junction table once a record from the target table is hydrated
protected string $_junctionProperty
Результат string

$_junctionTable защищенное свойство

Junction table instance
protected Table,Cake\ORM $_junctionTable
Результат Cake\ORM\Table

$_junctionTableName защищенное свойство

Junction table name
protected string $_junctionTableName
Результат string

$_saveStrategy защищенное свойство

Saving strategy to be used by this association
protected string $_saveStrategy
Результат string

$_strategy защищенное свойство

The strategy name to be used to fetch associated records.
protected string $_strategy
Результат string

$_targetConditions защищенное свойство

Filtered conditions that reference the target table.
protected null|array $_targetConditions
Результат null | array

$_targetForeignKey защищенное свойство

The name of the field representing the foreign key to the target table
protected string|array $_targetForeignKey
Результат string | array

$_through защищенное свойство

The table instance for the junction relation.
protected string|Table,Cake\ORM $_through
Результат string | Cake\ORM\Table

$_validStrategies защищенное свойство

Valid strategies for this type of association
protected array $_validStrategies
Результат array