Spot\Mapper::belongsTo PHP Method

belongsTo() public method

BelongsTo assumes that the localKey will reference the foreignEntity's primary key. If this is not the case, you probably want to use the 'hasOne' relationship instead.
public belongsTo ( EntityInterface $entity, $foreignEntity, $localKey )
$entity EntityInterface
    public function belongsTo(EntityInterface $entity, $foreignEntity, $localKey)
    {
        if (!is_subclass_of($foreignEntity, 'Spot\\EntityInterface')) {
            throw new \InvalidArgumentException("Related entity name must be a " . "valid entity that extends Spot\\Entity. Given '" . $foreignEntity . "'.");
        }
        $foreignMapper = $this->getMapper($foreignEntity);
        $foreignKey = $foreignMapper->primaryKeyField();
        // Return relation object so query can be lazy-loaded
        return new Relation\BelongsTo($this, $foreignEntity, $foreignKey, $localKey, $entity->{$localKey});
    }

Usage Example

Example #1
0
 public static function relations(Mapper $mapper, Entity $entity)
 {
     return ['tags' => $mapper->hasManyThrough($entity, 'SpotTest\\Entity\\Tag', 'SpotTest\\Entity\\PostTag', 'tag_id', 'post_id'), 'comments' => $mapper->hasMany($entity, 'SpotTest\\Entity\\Post\\Comment', 'post_id')->order(['date_created' => 'ASC']), 'author' => $mapper->belongsTo($entity, 'SpotTest\\Entity\\Author', 'author_id')];
 }
All Usage Examples Of Spot\Mapper::belongsTo