protected function resolveSetToDistinct() { if ($this->setDistinct) { $this->joinTablesAdapter->setSelectDistinctToTrue(); } }
protected static function resolveJoinsForRelatedAttributeAndGetRelationAttributeTableAliasName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $onTableAliasName) { assert('$modelAttributeToDataProviderAdapter->getRelationType() != RedBeanModel::MANY_MANY'); assert('is_string($onTableAliasName)'); if ($modelAttributeToDataProviderAdapter->getRelationType() == RedBeanModel::HAS_MANY || $modelAttributeToDataProviderAdapter->getRelationType() == RedBeanModel::HAS_MANY_BELONGS_TO) { $onTableJoinIdName = 'id'; $tableJoinIdName = $onTableAliasName . '_id'; //HAS_MANY have the potential to produce more than one row per model, so we need //to signal the query to be distinct. if ($modelAttributeToDataProviderAdapter->getRelationType() == RedBeanModel::HAS_MANY) { $joinTablesAdapter->setSelectDistinctToTrue(); } } elseif ($modelAttributeToDataProviderAdapter->getRelationType() == RedBeanModel::HAS_ONE_BELONGS_TO) { $tableJoinIdName = $onTableAliasName . '_id'; $onTableJoinIdName = 'id'; } else { $onTableJoinIdName = $modelAttributeToDataProviderAdapter->getColumnName(); $tableJoinIdName = 'id'; } $relationTableAliasName = $joinTablesAdapter->addLeftTableAndGetAliasName($modelAttributeToDataProviderAdapter->getRelationTableName(), $onTableJoinIdName, $onTableAliasName, $tableJoinIdName); $relationAttributeTableAliasName = $relationTableAliasName; //the second left join check being performed is if you //are in a contact filtering on related account email as an example. if ($modelAttributeToDataProviderAdapter->getRelatedAttributeModelClassName() != $modelAttributeToDataProviderAdapter->getRelationModelClassName()) { $relationAttributeTableName = $modelAttributeToDataProviderAdapter->getRelatedAttributeTableName(); //Handling special scenario for casted down Person. Todo: Automatically determine a //casted down scenario instead of specifically looking for Person. if ($modelAttributeToDataProviderAdapter->getRelatedAttributeModelClassName() == 'Person') { $onTableJoinIdName = "{$relationAttributeTableName}_id"; } elseif (get_parent_class($modelAttributeToDataProviderAdapter->getRelationModelClassName()) == $modelAttributeToDataProviderAdapter->getRelatedAttributeModelClassName()) { $onTableJoinIdName = "{$relationAttributeTableName}_id"; } else { $onTableJoinIdName = "{$modelAttributeToDataProviderAdapter->getRelatedAttributeColumnName()}" . "_{$relationAttributeTableName}_id"; } $relationAttributeTableAliasName = $joinTablesAdapter->addLeftTableAndGetAliasName($relationAttributeTableName, $onTableJoinIdName, $relationTableAliasName); } return $relationAttributeTableAliasName; }