PHP Class FOF30\Model\DataModel\Relation

Show file Open project: akeeba/fof Class Usage Examples

Protected Properties

Property Type Description
$container The component container for this relation
$data The data loaded by this relation
$foreignKey The foreign table key for this relation
$foreignKeyMap Maps each local table key to an array of foreign table keys, used in many-to-many relations
$foreignModelClass The class name of the foreign key's model
$foreignModelComponent The application name of the foreign model
$foreignModelName The bade name of the foreign model
$localKey The local table key for this relation
$parentModel The data model we are attached to
$pivotForeignKey For many-to-many relations, the pivot table's column storing the foreign key
$pivotLocalKey For many-to-many relations, the pivot table's column storing the local key
$pivotTable For many-to-many relations, the pivot (glue) table

Public Methods

Method Description
__construct ( DataModel $parentModel, string $foreignModelName, string $localKey = null, string $foreignKey = null, string $pivotTable = null, string $pivotLocalKey = null, string $pivotForeignKey = null ) Public constructor. Initialises the relation.
getCountSubquery ( ) : JDatabaseQuery Returns the count subquery for DataModel's has() and whereHas() methods.
getData ( callable $callback = null, Collection $dataCollection = null ) : Collection | DataModel Get the relation data.
getForeignKeyMap ( ) : array Returns the foreign key map of a many-to-many relation, used for eager loading many-to-many relations
getForeignModel ( array $config = [] ) : DataModel Gets an object instance of the foreign model
getLocalKey ( ) : string Returns the name of the local key of the relation
getNew ( ) : DataModel Returns a new item of the foreignModel type, pre-initialised to fulfil this relation
rebase ( DataModel $model ) Rebase the relation to a different model
reset ( ) Reset the relation data
saveAll ( ) Saves all related items. You can use it to touch items as well: every item being saved causes the modified_by and modified_on fields to be changed automatically, thanks to the DataModel's magic.
setDataFromCollection ( Collection &$data, mixed $keyMap = null ) : void Populates the internal $this->data collection from the contents of the provided collection. This is used by DataModel to push the eager loaded data into each item's relation.

Protected Methods

Method Description
filterForeignModel ( DataModel $foreignModel, Collection $dataCollection = null ) : boolean Applies the relation filters to the foreign model when getData is called

Method Details

__construct() public method

Public constructor. Initialises the relation.
public __construct ( DataModel $parentModel, string $foreignModelName, string $localKey = null, string $foreignKey = null, string $pivotTable = null, string $pivotLocalKey = null, string $pivotForeignKey = null )
$parentModel FOF30\Model\DataModel The data model we are attached to
$foreignModelName string The name of the foreign key's model in the format "modelName@com_something"
$localKey string The local table key for this relation
$foreignKey string The foreign key for this relation
$pivotTable string For many-to-many relations, the pivot (glue) table
$pivotLocalKey string For many-to-many relations, the pivot table's column storing the local key
$pivotForeignKey string For many-to-many relations, the pivot table's column storing the foreign key

filterForeignModel() abstract protected method

Applies the relation filters to the foreign model when getData is called
abstract protected filterForeignModel ( DataModel $foreignModel, Collection $dataCollection = null ) : boolean
$foreignModel FOF30\Model\DataModel The foreign model you're operating on
$dataCollection Collection If it's an eager loaded relation, the collection of loaded parent records
return boolean Return false to force an empty data collection

getCountSubquery() abstract public method

Returns the count subquery for DataModel's has() and whereHas() methods.
abstract public getCountSubquery ( ) : JDatabaseQuery
return JDatabaseQuery

getData() public method

If you want to apply additional filtering to the foreign model, use the $callback. It can be any function, static method, public method or closure with an interface of function(DataModel $foreignModel). You are not supposed to return anything, just modify $foreignModel's state directly. For example, you may want to do: $foreignModel->setState('foo', 'bar')
public getData ( callable $callback = null, Collection $dataCollection = null ) : Collection | DataModel
$callback callable The callback to run on the remote model.
$dataCollection Collection
return Collection | DataModel

getForeignKeyMap() public method

Returns the foreign key map of a many-to-many relation, used for eager loading many-to-many relations
public getForeignKeyMap ( ) : array
return array

getForeignModel() public method

Gets an object instance of the foreign model
public getForeignModel ( array $config = [] ) : DataModel
$config array Optional configuration information for the Model
return FOF30\Model\DataModel

getLocalKey() public method

Returns the name of the local key of the relation
public getLocalKey ( ) : string
return string

getNew() abstract public method

Returns a new item of the foreignModel type, pre-initialised to fulfil this relation
abstract public getNew ( ) : DataModel
return FOF30\Model\DataModel

rebase() public method

Rebase the relation to a different model
public rebase ( DataModel $model )
$model FOF30\Model\DataModel

reset() public method

Reset the relation data
public reset ( )

saveAll() public method

Saves all related items. You can use it to touch items as well: every item being saved causes the modified_by and modified_on fields to be changed automatically, thanks to the DataModel's magic.
public saveAll ( )

setDataFromCollection() public method

Populates the internal $this->data collection from the contents of the provided collection. This is used by DataModel to push the eager loaded data into each item's relation.
public setDataFromCollection ( Collection &$data, mixed $keyMap = null ) : void
$data Collection The relation data to push into this relation
$keyMap mixed Used by many-to-many relations to pass around the local to foreign key map
return void

Property Details

$container protected property

The component container for this relation
protected $container

$data protected property

The data loaded by this relation
protected $data

$foreignKey protected property

The foreign table key for this relation
protected $foreignKey

$foreignKeyMap protected property

Maps each local table key to an array of foreign table keys, used in many-to-many relations
protected $foreignKeyMap

$foreignModelClass protected property

The class name of the foreign key's model
protected $foreignModelClass

$foreignModelComponent protected property

The application name of the foreign model
protected $foreignModelComponent

$foreignModelName protected property

The bade name of the foreign model
protected $foreignModelName

$localKey protected property

The local table key for this relation
protected $localKey

$parentModel protected property

The data model we are attached to
protected $parentModel

$pivotForeignKey protected property

For many-to-many relations, the pivot table's column storing the foreign key
protected $pivotForeignKey

$pivotLocalKey protected property

For many-to-many relations, the pivot table's column storing the local key
protected $pivotLocalKey

$pivotTable protected property

For many-to-many relations, the pivot (glue) table
protected $pivotTable