public function testResolveSortAttributeColumnName()
 {
     $quote = DatabaseCompatibilityUtil::getQuote();
     //Test a standard non-relation attribute on I
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
     $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'iMember');
     $sort = ModelDataProviderUtil::resolveSortAttributeColumnName($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
     $this->assertEquals("{$quote}i{$quote}.{$quote}imember{$quote}", $sort);
     $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
     //Test a standard casted up attribute on H from I
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
     $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'name');
     $sort = ModelDataProviderUtil::resolveSortAttributeColumnName($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
     $this->assertEquals("{$quote}h{$quote}.{$quote}name{$quote}", $sort);
     $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(0, $joinTablesAdapter->getLeftTableJoinCount());
     //Test a relation attribute G->g from H
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('H');
     $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('H', 'castUpHasOne', 'g');
     $sort = ModelDataProviderUtil::resolveSortAttributeColumnName($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
     $this->assertEquals("{$quote}g{$quote}.{$quote}g{$quote}", $sort);
     $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
     $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
     $this->assertEquals('g', $leftTables[0]['tableName']);
     //Test a relation attribute G->g where casted up from I
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('I');
     $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('I', 'castUpHasOne', 'g');
     $sort = ModelDataProviderUtil::resolveSortAttributeColumnName($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
     $this->assertEquals("{$quote}g{$quote}.{$quote}g{$quote}", $sort);
     $this->assertEquals(1, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
     $fromTables = $joinTablesAdapter->getFromTablesAndAliases();
     $this->assertEquals('h', $fromTables[0]['tableName']);
     $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
     $this->assertEquals('g', $leftTables[0]['tableName']);
     //Test a customField like TestCustomFieldsModel->industry
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
     $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('TestCustomFieldsModel', 'industry', 'value');
     $sort = ModelDataProviderUtil::resolveSortAttributeColumnName($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
     $this->assertEquals("{$quote}customfield{$quote}.{$quote}value{$quote}", $sort);
     $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
     $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
     $this->assertEquals('customfield', $leftTables[0]['tableName']);
 }
 /**
  *
  */
 public static function resolveSortAttributeColumnName($modelClassName, &$joinTablesAdapter, $sortAttribute)
 {
     assert('$sortAttribute === null || is_string($sortAttribute) && $sortAttribute != ""');
     $sortRelatedAttribute = null;
     if ($modelClassName::isRelation($sortAttribute)) {
         $relationType = $modelClassName::getRelationType($sortAttribute);
         //MANY_MANY not supported currently for sorting.
         assert('$relationType != RedBeanModel::MANY_MANY');
         $relationModelClassName = $modelClassName::getRelationModelClassName($sortAttribute);
         $sortRelatedAttribute = self::getSortAttributeName($relationModelClassName);
     }
     $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter($modelClassName, $sortAttribute, $sortRelatedAttribute);
     return ModelDataProviderUtil::resolveSortAttributeColumnName($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
 }
 /**
  * @depends testResolveSortAttributeColumnName
  */
 public function testResolveSortWhenThereAreTableAliases()
 {
     $quote = DatabaseCompatibilityUtil::getQuote();
     //Test a customField like TestCustomFieldsModel->industry
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('TestCustomFieldsModel');
     $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('TestCustomFieldsModel', 'industry', 'value');
     $sort = ModelDataProviderUtil::resolveSortAttributeColumnName($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
     $this->assertEquals("{$quote}customfield{$quote}.{$quote}value{$quote}", $sort);
     $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(1, $joinTablesAdapter->getLeftTableJoinCount());
     $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
     $this->assertEquals('customfield', $leftTables[0]['tableName']);
     //Now add a second sort on a different CustomField
     $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter('TestCustomFieldsModel', 'market', 'value');
     $sort = ModelDataProviderUtil::resolveSortAttributeColumnName($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
     $this->assertEquals("{$quote}customfield1{$quote}.{$quote}value{$quote}", $sort);
     $this->assertEquals(0, $joinTablesAdapter->getFromTableJoinCount());
     $this->assertEquals(2, $joinTablesAdapter->getLeftTableJoinCount());
     $leftTables = $joinTablesAdapter->getLeftTablesAndAliases();
     $this->assertEquals('customfield', $leftTables[1]['tableName']);
     $this->assertEquals('customfield1', $leftTables[1]['tableAliasName']);
 }