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
コード例 #1
1
 /**
  * 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')]);
     }
 }
コード例 #2
0
 /**
  * Constructor.
  *
  * @param \Cake\ORM\Table $table The table this behavior is attached to
  * @param array $config Configuration array for this behavior
  */
 public function __construct(Table $table, array $config = [])
 {
     $this->_table = $table;
     parent::__construct($this->_table, $config);
     if ($this->config('autoBind')) {
         if ($this->_table->hasField($this->config('createdByField'))) {
             $this->_table->belongsTo('CreatedBy', ['className' => $this->config('userModel'), 'foreignKey' => $this->config('createdByField'), 'propertyName' => 'created_by']);
         }
         if ($this->_table->hasField($this->config('modifiedByField'))) {
             $this->_table->belongsTo('ModifiedBy', ['className' => $this->config('userModel'), 'foreignKey' => $this->config('modifiedByField'), 'propertyName' => 'modified_by']);
         }
     }
 }
コード例 #3
0
 /**
  * Generate associations on the junction table as necessary
  *
  * 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.
  *
  * @param \Cake\ORM\Table $junction The junction table.
  * @param \Cake\ORM\Table $source The source table.
  * @param \Cake\ORM\Table $target The target table.
  * @return void
  */
 protected function _generateJunctionAssociations($junction, $source, $target)
 {
     $tAlias = $target->alias();
     $sAlias = $source->alias();
     if (!$junction->association($tAlias)) {
         $junction->belongsTo($tAlias, ['foreignKey' => $this->targetForeignKey(), 'targetTable' => $target]);
     }
     if (!$junction->association($sAlias)) {
         $junction->belongsTo($sAlias, ['foreignKey' => $this->foreignKey(), 'targetTable' => $source]);
     }
 }
コード例 #4
0
 /**
  * Test find('list') with value field from associated table
  *
  * @return void
  */
 public function testFindListWithAssociatedTable()
 {
     $articles = new Table(['table' => 'articles', 'connection' => $this->connection]);
     $articles->belongsTo('Authors');
     $query = $articles->find('list', ['valueField' => 'author.name'])->contain(['Authors'])->order('articles.id');
     $this->assertEmpty($query->clause('select'));
     $expected = [1 => 'mariano', 2 => 'larry', 3 => 'mariano'];
     $this->assertSame($expected, $query->toArray());
 }
コード例 #5
0
ファイル: TableTest.php プロジェクト: neilan35/betterwindow1
 /**
  * Tests that belongsTo() creates and configures correctly the association
  *
  * @return void
  */
 public function testBelongsTo()
 {
     $options = ['foreignKey' => 'fake_id', 'conditions' => ['a' => 'b']];
     $table = new Table(['table' => 'dates']);
     $belongsTo = $table->belongsTo('user', $options);
     $this->assertInstanceOf('Cake\\ORM\\Association\\BelongsTo', $belongsTo);
     $this->assertSame($belongsTo, $table->association('user'));
     $this->assertEquals('user', $belongsTo->name());
     $this->assertEquals('fake_id', $belongsTo->foreignKey());
     $this->assertEquals(['a' => 'b'], $belongsTo->conditions());
     $this->assertSame($table, $belongsTo->source());
 }