Cake\ORM\Table::hasMany PHP Method

hasMany() public method

Target table can be inferred by its name, which is provided in the first argument, or you can either pass the class name 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 - dependent: Set to true if you want CakePHP to cascade deletes to the associated table when an entity is removed on this table. The delete operation on the associated table will not cascade further. To get recursive cascades enable cascadeCallbacks as well. Set to false if you don't want CakePHP to remove associated data, or when you are using database constraints. - cascadeCallbacks: Set to true if you want CakePHP to fire callbacks on cascaded deletes. If false the ORM will use deleteAll() to remove data. When true records will be loaded and then deleted. - conditions: array with a list of conditions to filter the join with - sort: The order in which results for this association should be returned - saveStrategy: Either 'append' or 'replace'. When 'append' the current records are appended to any records in the database. When 'replace' associated records not in the current set will be removed. If the foreign key is a null able column or if dependent is true records will be orphaned. - strategy: The strategy to be used for selecting results Either 'select' or 'subquery'. If subquery is selected the query used to return results in the source table will be used as conditions for getting rows in the target table. - finder: The finder method to use when loading records from this association. Defaults to 'all'. This method will return the association object that was built.
public hasMany ( string $associated, array $options = [] ) : Cake\ORM\Association\HasMany
$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\HasMany
    public function hasMany($associated, array $options = [])
    {
        $options += ['sourceTable' => $this];
        $association = new HasMany($associated, $options);
        return $this->_associations->add($association->name(), $association);
    }

Usage Example

 /**
  * Creates the associations between the bound table and every field passed to
  * this method.
  *
  * Additionally it creates a `i18n` HasMany association that will be
  * used for fetching all versions for each record in the bound table
  *
  * @param string $table the table name to use for storing each field version
  * @return void
  */
 public function setupFieldAssociations($table)
 {
     $alias = $this->_table->alias();
     foreach ($this->_fields() as $field) {
         $name = $this->_table->alias() . '_' . $field . '_version';
         $target = TableRegistry::get($name);
         $target->table($table);
         $this->_table->hasOne($name, ['targetTable' => $target, 'foreignKey' => 'foreign_key', 'joinType' => 'LEFT', 'conditions' => [$name . '.model' => $alias, $name . '.field' => $field], 'propertyName' => $field . '_version']);
     }
     $this->_table->hasMany($table, ['foreignKey' => 'foreign_key', 'strategy' => 'subquery', 'conditions' => ["{$table}.model" => $alias], 'propertyName' => '__version', 'dependent' => true]);
 }
All Usage Examples Of Cake\ORM\Table::hasMany