• PHP reference
  • Models

ModelRelations

class ModelRelations

Easily add, remove, and list relations between models.

Properties

string $sourceType

integer $sourceId

Methods

__construct(string $sourceType, integer $sourceId)

Initiate the ModelRelations class.

Parameters

  • string $sourceType
  • integer $sourceId

boolean add(string $objectType, integer $objectId, mixed $initiator = null, integer $weight)

Add a relationship.

Parameters

  • string $objectType
  • integer $objectId
  • mixed $initiator
  • integer $weight

Return Value

  • boolean

boolean remove(integer $id)

Remove a relationship by it's ID.

Parameters

  • integer $id

Return Value

  • boolean

array get(string $objectType = null)

Get all relationships with a certain objectType.

This method returns a associative array in which the keys are the ID's of the relationships. It also tries to automatically resolve objects into class instances, if a Model class exists in the current scope. An object of type 'User' will automatically resolve into a User class instance, while an object of type 'Fake' will simply return an database object with the row result.

Parameters

  • string $objectType

Return Value

  • array

array getSources(string $sourceType = null, string $objectType = null, boolean $ignoreDuplicates = false)

Get all relationships with a certain sourceType.

This method returns a associative array in which the keys are the ID's of the relationships. It also tries to automatically resolve objects into class instances, if a Model class exists in the current scope. An object of type 'User' will automatically resolve into a User class instance, while an object of type 'Fake' will simply return an database object with the row result.

Parameters

  • string $sourceType
  • string $objectType
  • boolean $ignoreDuplicates

Return Value

  • array

object|false getByObject(string $objectType, integer $objectId)

Get a relationship with a certain object.

Parameters

  • string $objectType
  • integer $objectId

Return Value

  • object|false An database or Model object when found, otherwise false

object|false getById(integer $id)

Get a relationship by id.

Parameters

  • integer $id

Return Value

  • object|false An database or Model object when found, otherwise false