Cake\ORM\Table::belongsTo PHP Method

belongsTo() public method

Target table can be inferred by its name, which is provided in the first argument, or you can either pass the to be instantiated or an instance of it directly. The options array accept the following keys: - className: The class name of the target table object - targetTable: An instance of a table object to be used as the target table - 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 - joinType: The type of join to be used (e.g. INNER) - strategy: The loading strategy to use. 'join' and 'select' are supported. - finder: The finder method to use when loading records from this association. Defaults to 'all'. When the strategy is 'join', only the fields, containments, and where conditions will be used from the finder. This method will return the association object that was built.
public belongsTo ( string $associated, array $options = [] ) : Cake\ORM\Association\BelongsTo
$associated string the alias for the target table. This is used to uniquely identify the association
$options array list of options to configure the association definition
return Cake\ORM\Association\BelongsTo
    public function belongsTo($associated, array $options = [])
    {
        $options += ['sourceTable' => $this];
        $association = new BelongsTo($associated, $options);
        return $this->_associations->add($association->name(), $association);
    }

Usage Example

 /**
  * Constructor
  *
  * @param \Cake\ORM\Table $table Table who requested the behavior.
  * @param array $config Options.
  */
 public function __construct(Table $table, array $config = [])
 {
     parent::__construct($table, $config);
     $this->Table = $table;
     if ($this->config('created_by')) {
         $this->Table->belongsTo('CreatedBy', ['foreignKey' => $this->config('created_by'), 'className' => $this->config('userModel'), 'propertyName' => $this->config('createdByPropertyName')]);
     }
     if ($this->config('modified_by')) {
         $this->Table->belongsTo('ModifiedBy', ['foreignKey' => $this->config('modified_by'), 'className' => $this->config('userModel'), 'propertyName' => $this->config('modifiedByPropertyName')]);
     }
 }
All Usage Examples Of Cake\ORM\Table::belongsTo