hasOne() 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 - joinType: The type of join to be used (e.g. LEFT) - 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 hasOne ( string $associated, array $options = [] ) : Cake\ORM\Association\HasOne
$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\HasOne
Exemplo n.º 1
0
 /**
  * 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]);
 }
Exemplo n.º 2
0
 /**
  * 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 translations for each record in the bound table
  *
  * @param array $fields list of fields to create associations for
  * @param string $table the table name to use for storing each field translation
  * @param string $model the model field value
  * @param string $strategy the strategy used in the _i18n association
  *
  * @return void
  */
 public function setupFieldAssociations($fields, $table, $model, $strategy)
 {
     $targetAlias = $this->_translationTable->alias();
     $alias = $this->_table->alias();
     $filter = $this->_config['onlyTranslated'];
     $tableLocator = $this->tableLocator();
     foreach ($fields as $field) {
         $name = $alias . '_' . $field . '_translation';
         if (!$tableLocator->exists($name)) {
             $fieldTable = $tableLocator->get($name, ['className' => $table, 'alias' => $name, 'table' => $this->_translationTable->table()]);
         } else {
             $fieldTable = $tableLocator->get($name);
         }
         $conditions = [$name . '.model' => $model, $name . '.field' => $field];
         if (!$this->_config['allowEmptyTranslations']) {
             $conditions[$name . '.content !='] = '';
         }
         $this->_table->hasOne($name, ['targetTable' => $fieldTable, 'foreignKey' => 'foreign_key', 'joinType' => $filter ? 'INNER' : 'LEFT', 'conditions' => $conditions, 'propertyName' => $field . '_translation']);
     }
     $conditions = ["{$targetAlias}.model" => $model];
     if (!$this->_config['allowEmptyTranslations']) {
         $conditions["{$targetAlias}.content !="] = '';
     }
     $this->_table->hasMany($targetAlias, ['className' => $table, 'foreignKey' => 'foreign_key', 'strategy' => $strategy, 'conditions' => $conditions, 'propertyName' => '_i18n', 'dependent' => true]);
 }
 /**
  * Returns association object for all versions or single field version.
  *
  * @param string|null $field Field name for per-field association.
  * @param array $options Association options.
  * @return \Cake\ORM\Association
  */
 public function versionAssociation($field = null, $options = [])
 {
     $name = $this->_associationName($field);
     if (!$this->_table->associations()->has($name)) {
         $model = $this->_config['referenceName'];
         if ($field) {
             $this->_table->hasOne($name, $options + ['className' => $this->_config['versionTable'], 'foreignKey' => $this->_config['foreignKey'], 'joinType' => 'LEFT', 'conditions' => [$name . '.model' => $model, $name . '.field' => $field], 'propertyName' => $field . '_version']);
         } else {
             $this->_table->hasMany($name, $options + ['className' => $this->_config['versionTable'], 'foreignKey' => $this->_config['foreignKey'], 'strategy' => 'subquery', 'conditions' => ["{$name}.model" => $model], 'propertyName' => '__version', 'dependent' => true]);
         }
     }
     return $this->_table->association($name);
 }
Exemplo n.º 4
0
 /**
  * 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 translations for each record in the bound table
  *
  * @param array $fields list of fields to create associations for
  * @param string $table the table name to use for storing each field translation
  * @param array $fieldConditions conditions for finding fields
  * @param string $strategy the strategy used in the _i18n association
  *
  * @return void
  */
 public function setupFieldAssociations($fields, $table, $fieldConditions, $strategy)
 {
     $targetAlias = $this->_translationTable->alias();
     $alias = $this->_table->alias();
     $filter = $this->_config['onlyTranslated'];
     foreach ($fields as $field) {
         $name = $alias . '_' . $field . '_translation';
         $conditions = [$name . '.model' => $fieldConditions['model'], $name . '.field' => $field];
         foreach ($fieldConditions as $fieldName => $fieldValue) {
             $conditions[$name . '.' . $fieldName] = $fieldValue;
         }
         if (!TableRegistry::exists($name)) {
             $fieldTable = TableRegistry::get($name, ['className' => $table, 'alias' => $name, 'table' => $this->_translationTable->table()]);
         } else {
             $fieldTable = TableRegistry::get($name);
         }
         $this->_table->hasOne($name, ['targetTable' => $fieldTable, 'foreignKey' => 'foreign_key', 'joinType' => $filter ? 'INNER' : 'LEFT', 'conditions' => $conditions, 'propertyName' => $field . '_translation']);
     }
     $this->_table->hasMany($targetAlias, ['className' => $table, 'foreignKey' => 'foreign_key', 'strategy' => $strategy, 'conditions' => ["{$targetAlias}.model" => $fieldConditions['model']], 'propertyName' => '_i18n', 'dependent' => true]);
 }
Exemplo n.º 5
0
 /**
  * Test has one with a plugin model
  *
  * @return void
  */
 public function testHasOnePlugin()
 {
     $options = ['className' => 'TestPlugin.Comments'];
     $table = new Table(['table' => 'users']);
     $hasOne = $table->hasOne('Comments', $options);
     $this->assertInstanceOf('Cake\\ORM\\Association\\HasOne', $hasOne);
     $this->assertSame('Comments', $hasOne->name());
     $hasOneTable = $hasOne->target();
     $this->assertSame('Comments', $hasOne->alias());
     $this->assertSame('TestPlugin.Comments', $hasOne->registryAlias());
     $options = ['className' => 'TestPlugin.Comments'];
     $table = new Table(['table' => 'users']);
     $hasOne = $table->hasOne('TestPlugin.Comments', $options);
     $this->assertInstanceOf('Cake\\ORM\\Association\\HasOne', $hasOne);
     $this->assertSame('Comments', $hasOne->name());
     $hasOneTable = $hasOne->target();
     $this->assertSame('Comments', $hasOne->alias());
     $this->assertSame('TestPlugin.Comments', $hasOne->registryAlias());
 }
Exemplo n.º 6
0
 /**
  * Tests that hasOne() creates and configures correctly the association
  *
  * @return void
  */
 public function testHasOne()
 {
     $options = ['foreignKey' => 'user_id', 'conditions' => ['b' => 'c']];
     $table = new Table(['table' => 'users']);
     $hasOne = $table->hasOne('profile', $options);
     $this->assertInstanceOf('Cake\\ORM\\Association\\HasOne', $hasOne);
     $this->assertSame($hasOne, $table->association('profile'));
     $this->assertEquals('profile', $hasOne->name());
     $this->assertEquals('user_id', $hasOne->foreignKey());
     $this->assertEquals(['b' => 'c'], $hasOne->conditions());
     $this->assertSame($table, $hasOne->source());
 }
Exemplo n.º 7
0
 /**
  * {@inheritDoc}
  */
 public function __construct(Table $table, array $config = [])
 {
     $config['pk'] = $table->primaryKey();
     $config['tableAlias'] = (string) Inflector::underscore($table->table());
     $table->hasOne('Search.SearchDatasets', ['foreignKey' => 'entity_id', 'conditions' => ['SearchDatasets.table_alias' => $config['tableAlias']], 'dependent' => true]);
     parent::__construct($table, $config);
 }