FOF30\Model\DataModel\Relation\HasMany::filterForeignModel PHP Method

filterForeignModel() protected method

Applies the relation filters to the foreign model when getData is called
protected filterForeignModel ( DataModel $foreignModel, Collection $dataCollection = null ) : boolean
$foreignModel FOF30\Model\DataModel The foreign model you're operating on
$dataCollection FOF30\Model\DataModel\Collection If it's an eager loaded relation, the collection of loaded parent records
return boolean Return false to force an empty data collection
    protected function filterForeignModel(DataModel $foreignModel, DataModel\Collection $dataCollection = null)
    {
        // Decide how to proceed, based on eager or lazy loading
        if (is_object($dataCollection)) {
            // Eager loaded relation
            if (!empty($dataCollection)) {
                // Get a list of local keys from the collection
                $values = array();
                /** @var $item DataModel */
                foreach ($dataCollection as $item) {
                    $v = $item->getFieldValue($this->localKey, null);
                    if (!is_null($v)) {
                        $values[] = $v;
                    }
                }
                // Keep only unique values
                $values = array_unique($values);
                // Apply the filter
                if (!empty($values)) {
                    $foreignModel->where($this->foreignKey, 'in', $values);
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else {
            // Lazy loaded relation; get the single local key
            $localKey = $this->parentModel->getFieldValue($this->localKey, null);
            if (is_null($localKey)) {
                return false;
            }
            $foreignModel->where($this->foreignKey, '==', $localKey);
        }
        return true;
    }