Represents an 1 - N relationship where the source side of the relation is related to only one record in the target table.
An example of a BelongsTo association would be Article belongs to Author.
Cake\ORM\Association\BelongsTo uses Cake\ORM\Association\SelectableAssociationTrait _buildResultMap( mixed $fetchQuery , mixed $options )
_joinCondition( array $options )
Returns a single or multiple conditions to be appended to the generated join clause for getting the results on the target table.
$options arrayRuntimeExceptionCake\ORM\Association::_joinCondition() _linkField( mixed $options )
cascadeDelete( Cake\Datasource\EntityInterface $entity , array $options [] )
Handle cascading deletes.
BelongsTo associations are never cleared in a cascading delete scenario.
Cake\Datasource\EntityInterface $entity $options optional [] booleanforeignKey( string|null $key null )
Sets the name of the field representing the foreign key to the target table. If no parameters are passed current field is returned
$key optional null stringCake\ORM\Association::foreignKey() isOwningSide( Cake\ORM\Table $side )
Returns whether or not the passed table is the owning side for this association. This means that rows in the 'target' table would miss important or required information if the row in 'source' did not exist.
Cake\ORM\Table $side booleanproperty( string|null $name null )
Sets the property name that should be filled with data from the target table in the source table record. If no arguments are passed, currently configured type is returned.
$name optional null stringCake\ORM\Association::property() saveAssociated( Cake\Datasource\EntityInterface $entity , array $options [] )
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
Cake\Datasource\EntityInterface $entity $options optional [] boolean|Cake\Datasource\EntityInterface__call( string $method , array $argument )
Proxies method calls to the target table.
$method $argument mixedBadMethodCallException__construct( string $alias , array $options [] )
Constructor. Subclasses can override _options function to get the original list of passed options if expecting any other special key
$alias $options optional [] __get( string $property )
Proxies property retrieval to the target table. This is handy for getting this association's associations
$property Cake\ORM\AssociationRuntimeException__isset( string $property )
Proxies the isset call to the target table. This is handy to check if the target table has another association with the passed name
$property boolean_appendFields( Cake\ORM\Query $query , Cake\ORM\Query $surrogate , array $options )
Helper function used to conditionally append fields to the select clause of a query from the fields found in another query object.
Cake\ORM\Query $query Cake\ORM\Query $surrogate $options attachTo
_appendNotMatching( Cake\Database\Query $query , array $options )
Conditionally adds a condition to the passed Query that will make it find records where there is no match with this association.
Cake\Database\Query $query $options negateMatch key._bindNewAssociations( Cake\ORM\Query $query , Cake\ORM\Query $surrogate , array $options )
Applies all attachable associations to $query out of the containments found in the $surrogate query.
Copies all contained associations from the $surrogate query into the passed $query. Containments are altered so that they respect the associations chain from which they originated.
Cake\ORM\Query $query Cake\ORM\Query $surrogate $options attachTo
_dispatchBeforeFind( Cake\ORM\Query $query )
Triggers beforeFind on the target table for the query this association is attaching to
Cake\ORM\Query $query _extractFinder( string|array $finderData )
Helper method to infer the requested finder and its options.
Returns the inferred options from the finder $type.
The following will call the finder 'translations' with the value of the finder as its options: $query->contain(['Comments' => ['finder' => ['translations']]]); $query->contain(['Comments' => ['finder' => ['translations' => []]]]); $query->contain(['Comments' => ['finder' => ['translations' => ['locales' => ['en_US']]]]]);
$finderData array_formatAssociationResults( Cake\ORM\Query $query , Cake\ORM\Query $surrogate , array $options )
Adds a formatter function to the passed $query if the $surrogate query declares any other formatter. Since the $surrogate query correspond to the associated target table, the resulting formatter will be the result of applying the surrogate formatters to only the property corresponding to such table.
Cake\ORM\Query $query Cake\ORM\Query $surrogate $options attachTo
_options( array $options )
Override this function to initialize any concrete association class, it will get passed the original list of options used in the constructor
$options attachTo( Cake\ORM\Query $query , array $options [] )
Alters a Query object to include the associated target table data in the final result
The options array accept the following keys:
Cake\ORM\Query $query $options optional [] RuntimeExceptionbindingKey( string|null $key null )
Sets the name of the field representing the binding field with the target table. When not manually specified the primary key of the owning side table is used.
If no parameters are passed the current field is returned
$key optional null string|arraycanBeJoined( array $options [] )
Whether this association can be expressed directly in a query join
$options optional [] booleancascadeCallbacks( boolean|null $cascadeCallbacks null )
Sets whether or not cascaded deletes should also fire callbacks. If no arguments are passed, the current configured value is returned
$cascadeCallbacks optional null booleanconditions( array|null $conditions null )
Sets a list of conditions to be always included when fetching records from the target association. If no parameters are passed the current list is returned
$conditions optional null arraydefaultRowValue( array $row , boolean $joined )
Returns a modified row after appending a property for this association with the default empty value according to whether the association was joined or fetched externally.
$row $joined arraydeleteAll( mixed $conditions )
Proxies the delete operation to the target table's deleteAll method
$conditions booleanCake\ORM\Table::deleteAll()dependent( boolean|null $dependent null )
Sets whether the records on the target table are dependent on the source table.
This is primarily used to indicate that records should be removed if the owning record in the source table is deleted.
If no parameters are passed the current setting is returned.
$dependent optional null booleaneagerLoader( array $options )
Eager loads a list of records in the target table that are related to another set of records in the source table. Source records can specified in two ways: first one is by passing a Query object setup to find on the source table and the other way is by explicitly passing an array of primary key values from the source table.
The required way of passing related source records is controlled by "strategy" When the subquery strategy is used it will require a query on the source table. When using the select strategy, the list of primary keys will be used.
Returns a closure that should be run for each record returned in a specific Query. This callable will be responsible for injecting the fields that are related to each specific passed row.
Options array accepts the following keys:
$options Closurefind( string|array $type null , array $options [] )
Proxies the finding operation to the target table's find method and modifies the query accordingly based of this association configuration
$type optional null $options parameter$options optional [] Cake\ORM\QueryCake\ORM\Table::find()finder( string|null $finder null )
Sets the default finder to use for fetching rows from the target table. If no parameters are passed, it will return the currently configured finder name.
$finder optional null stringjoinType( string $type null )
Sets the type of join to be used when adding the association to a query. If no arguments are passed, the currently configured type is returned.
$type optional null stringname( string|null $name null )
Sets the name for this association. If no argument is passed then the current configured name will be returned
$name optional null stringsource( Cake\ORM\Table $table null )
Sets the table instance for the source side of the association. If no arguments are passed, the current configured table instance is returned
Cake\ORM\Table $table optional null Cake\ORM\Tablestrategy( string|null $name null )
Sets the strategy name to be used to fetch associated records. Keep in mind that some association types might not implement but a default strategy, rendering any changes to this setting void. If no arguments are passed, the currently configured strategy is returned.
$name optional null stringInvalidArgumentExceptiontarget( Cake\ORM\Table $table null )
Sets the table instance for the target side of the association. If no arguments are passed, the current configured table instance is returned
Cake\ORM\Table $table optional null Cake\ORM\TabletransformRow( array $row , string $nestKey , boolean $joined )
Correctly nests a result row associated values into the correct array keys inside the source results.
$row $nestKey $joined arrayupdateAll( array $fields , mixed $conditions )
Proxies the update operation to the target table's updateAll method
$fields $conditions booleanCake\ORM\Table::updateAll()_addFilteringCondition( Cake\ORM\Query $query , string|array $key , mixed $filter )
Appends any conditions required to load the relevant set of records in the target table query given a filter key and some filtering values.
Cake\ORM\Query $query $key $filter Cake\ORM\Query_addFilteringJoin( Cake\ORM\Query $query , string $key , Cake\ORM\Query $subquery )
Appends any conditions required to load the relevant set of records in the target table query given a filter key and some filtering values when the filtering needs to be done using a subquery.
Cake\ORM\Query $query $key Cake\ORM\Query $subquery Cake\ORM\Query_buildQuery( array $options )
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
$options Cake\ORM\QueryInvalidArgumentException_buildSubquery( Cake\ORM\Query $query )
Builds a query to be used as a condition for filtering records in the target table, it is constructed by cloning the original query that was used to load records in the source table.
Cake\ORM\Query $query Cake\ORM\Query_createTupleCondition( Cake\ORM\Query $query , array $keys , mixed $filter , string $operator )
Returns a TupleComparison object that can be used for matching all the fields from $keys with the tuple values in $filter using the provided operator.
Cake\ORM\Query $query $keys $filter $operator Cake\Database\Expression\TupleComparison_defaultOptions( )
Returns the default options to use for the eagerLoader
array_multiKeysInjector( array $resultMap , array $sourceKeys , string $nestKey )
Returns a callable to be used for each row in a query result set for injecting the eager loaded rows when the matching needs to be done with multiple foreign keys
$resultMap $sourceKeys $nestKey Closure_resultInjector( Cake\ORM\Query $fetchQuery , array $resultMap , array $options )
Returns a callable to be used for each row in a query result set for injecting the eager loaded rows
Cake\ORM\Query $fetchQuery $resultMap $options Closure_subqueryFields( Cake\ORM\Query $query )
Calculate the fields that need to participate in a subquery.
Normally this includes the binding key columns. If there is a an ORDER BY, those columns are also included as the fields may be calculated or constant values, that need to be present to ensure the correct association data is loaded.
Cake\ORM\Query $query arrayeagerLoader( array $options )
requiresKeys( array $options [] )
Returns true if the eager loading process will require a set of the owning table's binding keys in order to use them as a filter in the finder query.
$options optional [] boolean_camelize( string $name )
Creates a camelized version of $name
$name string_entityName( string $name )
Creates the proper entity name (singular) for the specified name
$name string_fixtureName( string $name )
Creates a fixture name
$name string_modelKey( string $name )
Creates the proper underscored model key for associations
If the input contains a dot, assume that the right side is the real table name.
$name string_modelNameFromKey( string $key )
Creates the proper model name from a foreign key
$key string_pluginNamespace( string $pluginName )
Return plugin's namespace
$pluginName string_pluginPath( string $pluginName )
Find the correct path for a plugin. Scans $pluginPaths for the plugin you want.
$pluginName string_pluralHumanName( string $name )
Creates the plural human name used in views
$name string_singularHumanName( string $name )
Creates the singular human name used in views
$name string_singularName( string $name )
Creates the singular name for use in views.
$name string_variableName( string $name )
Creates the plural variable name for views
$name stringtableLocator( Cake\ORM\Locator\LocatorInterface $tableLocator null )
Sets the table locator. If no parameters are passed, it will return the currently used locator.
Cake\ORM\Locator\LocatorInterface $tableLocator optional null Cake\ORM\Locator\LocatorInterfaceMANY_TO_MANY, MANY_TO_ONE, ONE_TO_MANY, ONE_TO_ONE, STRATEGY_JOIN, STRATEGY_SELECT, STRATEGY_SUBQUERY |
protected string|array
The field name in the owning side table that is used to match with the foreignKey
protected string
Whether or not cascaded deletes should also fire callbacks.
false protected array
A list of conditions to be always included when fetching records from the target association
[]
protected boolean
Whether the records on the target table are dependent on the source table, often used to indicate that records should be removed if the owning record in the source table is deleted.
false protected string
The default finder name to use for fetching rows from the target table
'all' protected string|array
The name of the field representing the foreign key to the table to load
protected string
The type of join to be used when adding the association to a query
'LEFT' protected string
Name given to the association, it usually represents the alias assigned to the target associated table
protected string
The property name that should be filled with data from the target table in the source table record.
protected string
The strategy name to be used to fetch associated records. Some association types might not implement but one strategy to fetch records.
© 2005–2016 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
http://api.cakephp.org/3.1/class-Cake.ORM.Association.BelongsTo.html