public function getFromPivot (\Sonic\Model $target, \Sonic\Model $pivot, $key = FALSE, $params = [])
	{
		
		// Find the pivot attribute pointing to the source
		
		$sourceClass	= get_called_class ();
		$sourceRef		= FALSE;
		
		foreach ($pivot::$attributes as $name => $attribute)
		{
			if (isset ($attribute['relation']) &&
				$attribute['relation'] == $sourceClass)
			{
				$sourceRef = $name;
				break;
			}
		}
		
		if (!$sourceRef)
		{
			return FALSE;
		}
		
		// Find the pivot attribute pointing to the target
		
		$targetClass	= get_class ($target);
		$targetRef		= FALSE;
		
		foreach ($pivot::$attributes as $name => $attribute)
		{
			if (isset ($attribute['relation']) &&
				$attribute['relation'] == $targetClass)
			{
				$targetRef = $name;
				break;
			}
		}
		
		if (!$targetRef)
		{
			return FALSE;
		}
		
		// Query parameters
		
		if (!isset ($params['select']))
		{
			$params['select']	= 'DISTINCT t.*';
		}
		
		if (isset ($params['from']) && !is_array ($params['from']))
		{
			$params['from']	= [$params['from']];
		}
		
		$params['from'][]	= $target::$dbTable . ' as t';
		$params['from'][]	= $pivot::$dbTable . ' as p';
		
		$params['where'][]	= ['p.' . $sourceRef, $this->getPK ()];
		$params['where'][]	= 'p.' . $targetRef . ' = t.' . $target::$pk;
		
		// Get related objects
		
		return $target::_getObjects ($params, $key);
		
	}
public function getFromPivot (\Sonic\Model $target, \Sonic\Model $pivot, $key = FALSE, $params = [])
	{
		
		// Find the pivot attribute pointing to the source
		
		$sourceClass	= get_called_class ();
		$sourceRef		= FALSE;
		
		foreach ($pivot::$attributes as $name => $attribute)
		{
			if (isset ($attribute['relation']) &&
				$attribute['relation'] == $sourceClass)
			{
				$sourceRef = $name;
				break;
			}
		}
		
		if (!$sourceRef)
		{
			return FALSE;
		}
		
		// Find the pivot attribute pointing to the target
		
		$targetClass	= get_class ($target);
		$targetRef		= FALSE;
		
		foreach ($pivot::$attributes as $name => $attribute)
		{
			if (isset ($attribute['relation']) &&
				$attribute['relation'] == $targetClass)
			{
				$targetRef = $name;
				break;
			}
		}
		
		if (!$targetRef)
		{
			return FALSE;
		}
		
		// Query parameters
		
		if (!isset ($params['select']))
		{
			$params['select']	= 'DISTINCT t.*';
		}
		
		if (isset ($params['from']) && !is_array ($params['from']))
		{
			$params['from']	= [$params['from']];
		}
		
		$params['from'][]	= $target::$dbTable . ' as t';
		$params['from'][]	= $pivot::$dbTable . ' as p';
		
		$params['where'][]	= ['p.' . $sourceRef, $this->getPK ()];
		$params['where'][]	= 'p.' . $targetRef . ' = t.' . $target::$pk;
		
		// Get related objects
		
		return $target::_getObjects ($params, $key);
		
	}
public function getFromPivot ( \ Sonic \ Model $ target , \ Sonic \ Model $ pivot , $ key = FALSE , $ params = [ ] ) { // Find the pivot attribute pointing to the source $ sourceClass = get_called_class ( ) ; $ sourceRef = FALSE ; foreach ( $ pivot :: $ attributes as $ name => $ attribute ) { if ( isset ( $ attribute [ 'relation' ] ) && $ attribute [ 'relation' ] == $ sourceClass ) { $ sourceRef = $ name ; break ; } } if ( ! $ sourceRef ) { return FALSE ; } // Find the pivot attribute pointing to the target $ targetClass = get_class ( $ target ) ; $ targetRef = FALSE ; foreach ( $ pivot :: $ attributes as $ name => $ attribute ) { if ( isset ( $ attribute [ 'relation' ] ) && $ attribute [ 'relation' ] == $ targetClass ) { $ targetRef = $ name ; break ; } } if ( ! $ targetRef ) { return FALSE ; } // Query parameters if ( ! isset ( $ params [ 'select' ] ) ) { $ params [ 'select' ] = 'DISTINCT t.*' ; } if ( isset ( $ params [ 'from' ] ) && ! is_array ( $ params [ 'from' ] ) ) { $ params [ 'from' ] = [ $ params [ 'from' ] ] ; } $ params [ 'from' ] [ ] = $ target :: $ dbTable . ' as t' ; $ params [ 'from' ] [ ] = $ pivot :: $ dbTable . ' as p' ; $ params [ 'where' ] [ ] = [ 'p.' . $ sourceRef , $ this -> getPK ( ) ] ; $ params [ 'where' ] [ ] = 'p.' . $ targetRef . ' = t.' . $ target :: $ pk ; // Get related objects return $ target :: _getObjects ( $ params , $ key ) ; }
Return related objects from a many-to-many pivot table
@param \Sonic\Model $target Target objects to return
@param \Sonic\Model $pivot Pivot object
@return boolean|Model\Collection
Return related objects from a many - to - many pivot table