/** * @param RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter * @return string */ protected static function resolveSortColumnName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter) { if ($modelAttributeToDataProviderAdapter->hasRelatedAttribute()) { return $modelAttributeToDataProviderAdapter->getRelatedAttributeColumnName(); } else { return $modelAttributeToDataProviderAdapter->getColumnName(); } }
public function testAllMethodsOnAttributeOnSameModelAndRelatedAttribute() { $adapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'j', 'jMember'); $this->assertEquals('I', $adapter->getModelClassName()); $this->assertEquals('j', $adapter->getAttribute()); $this->assertEquals('jMember', $adapter->getRelatedAttribute()); $this->assertEquals('i', $adapter->getModelTableName()); $this->assertEquals('I', $adapter->getAttributeModelClassName()); $this->assertEquals('i', $adapter->getAttributeTableName()); $this->assertEquals('j_id', $adapter->getColumnName()); $this->assertTrue($adapter->isRelation()); $this->assertTrue($adapter->hasRelatedAttribute()); $this->assertEquals('J', $adapter->getRelationModelClassName()); $this->assertEquals('J', $adapter->getRelatedAttributeModelClassName()); $this->assertEquals('j', $adapter->getRelationTableName()); $this->assertEquals('j', $adapter->getRelatedAttributeTableName()); $this->assertEquals('jmember', $adapter->getRelatedAttributeColumnName()); $this->assertFalse($adapter->isRelatedAttributeRelation()); }
/** * If the $onTableAliasName is used (not null): * Special use of group by attribute resolution. If you are resolving a group by attribute against a relation then * the joins must utilize a left join in the case of casting up. Does not support when the attribute is a * relation itself as this expects any relation processing to be done before this is called. * * @param RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter * @param RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter * @param null | string $onTableAliasName * @return string * @throws NotSupportedException */ public static function resolveGroupByAttributeColumnName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter, $onTableAliasName = null) { assert('is_string($onTableAliasName) || $onTableAliasName == null'); $builder = new ModelJoinBuilder($modelAttributeToDataProviderAdapter, $joinTablesAdapter); $tableAliasName = $builder->resolveJoins($onTableAliasName, self::resolveCanUseFromJoins($onTableAliasName)); if ($modelAttributeToDataProviderAdapter->hasRelatedAttribute()) { $resolvedGroupByColumnName = $modelAttributeToDataProviderAdapter->getRelatedAttributeColumnName(); } else { $resolvedGroupByColumnName = $modelAttributeToDataProviderAdapter->getColumnName(); } return self::resolveGroupByColumnNameString($tableAliasName, $resolvedGroupByColumnName); }
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; }