/**
  * @param RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter
  * @return string
  */
 protected static function resolveSortColumnName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter)
 {
     if ($modelAttributeToDataProviderAdapter->hasRelatedAttribute()) {
         return $modelAttributeToDataProviderAdapter->getRelatedAttributeColumnName();
     } else {
         return $modelAttributeToDataProviderAdapter->getColumnName();
     }
 }
Example #2
0
 /**
  * @return mixed
  */
 private function resolveOnTableAliasNameForDerivedRelationViaCastedUpModel()
 {
     if ($this->modelAttributeToDataProviderAdapter->isAttributeDerivedRelationViaCastedUpModel()) {
         $onTableAliasName = $this->modelAttributeToDataProviderAdapter->getModelTableName();
     } else {
         $onTableAliasName = $this->modelAttributeToDataProviderAdapter->getAttributeTableName();
     }
     return $onTableAliasName;
 }
 /**
  * If the attribute is not on the same model class but nested up, it should be blocked from being configured
  * in the designer tool since it can have side effects. You can still manually override this in the code if
  * necessary.
  */
 protected function isAttributeOnModelOrCastedUp($attributeName)
 {
     assert('is_string($attributeName)');
     $attributeAdapter = new RedBeanModelAttributeToDataProviderAdapter($this->modelClassName, $attributeName);
     if (!$attributeAdapter->getModel()->isAttribute($attributeName)) {
         return false;
     }
     if ($attributeAdapter->getAttributeModelClassName() != $this->modelClassName) {
         return true;
     }
     return false;
 }
 /**
  * @param $modelToReportAdapter
  * @param $modelAttributeToDataProviderAdapter
  * @param string $modelClassName
  * @param string $realAttributeName
  */
 protected function resolveCastingHintForAttribute($modelToReportAdapter, $modelAttributeToDataProviderAdapter, $modelClassName, $realAttributeName)
 {
     assert('$modelToReportAdapter instanceof ModelRelationsAndAttributesToReportAdapter');
     $hintAdapter = new RedBeanModelAttributeToDataProviderAdapter($modelClassName, $realAttributeName);
     $hintModelClassName = $hintAdapter->getAttributeModelClassName();
     $modelAttributeToDataProviderAdapter->setCastingHintModelClassNameForAttribute($hintModelClassName);
 }
 /**
  * @param string $modelClassName
  * @param array $concatedAttributeNames
  * @param RedBeanModelJoinTablesQueryAdapter $joinTablesAdapter
  * @param null | string $onTableAliasName
  * @return array
  */
 protected static function makeTableAliasAndColumnNamesForNonRelatedConcatedAttributes($modelClassName, $concatedAttributeNames, $joinTablesAdapter, $onTableAliasName = null)
 {
     assert('is_string($modelClassName)');
     assert('is_array($concatedAttributeNames) && count($concatedAttributeNames) == 2');
     assert('$joinTablesAdapter instanceof RedBeanModelJoinTablesQueryAdapter');
     assert('is_string($onTableAliasName) || $onTableAliasName == null');
     $tableAliasAndColumnNames = array();
     foreach ($concatedAttributeNames as $attributeName) {
         $modelAttributeToDataProviderAdapter = new RedBeanModelAttributeToDataProviderAdapter($modelClassName, $attributeName);
         $builder = new ModelWhereAndJoinBuilder($modelAttributeToDataProviderAdapter, $joinTablesAdapter, true);
         $tableAliasName = $builder->resolveJoins($onTableAliasName, self::resolveCanUseFromJoins($onTableAliasName));
         $tableAliasAndColumnNames[] = array($tableAliasName, $modelAttributeToDataProviderAdapter->getColumnName());
     }
     return $tableAliasAndColumnNames;
 }
 public function testRelatedAttributesSortUsesTwoAttributes()
 {
     $adapter = new RedBeanModelAttributeToDataProviderAdapter('A', 'a');
     $this->assertFalse($adapter->sortUsesTwoAttributes());
     $this->assertEquals('a', $adapter->getColumnNameByPosition(0));
     try {
         $adapter->getColumnNameByPosition(1);
     } catch (InvalidArgumentException $exception) {
         $this->assertEquals('Attribute position is not valid', $exception->getMessage());
     }
     $adapter = new RedBeanModelAttributeToDataProviderAdapter('Q', 'a');
     $this->assertTrue($adapter->sortUsesTwoAttributes());
     $this->assertEquals('a', $adapter->getColumnNameByPosition(0));
     $this->assertEquals('junk', $adapter->getColumnNameByPosition(1));
     $this->assertEquals('uniquerequiredemail', $adapter->getColumnNameByPosition(2));
     try {
         $adapter->getColumnNameByPosition(3);
     } catch (InvalidArgumentException $exception) {
         $this->assertEquals('Attribute position is not valid', $exception->getMessage());
     }
     $adapter = new RedBeanModelAttributeToDataProviderAdapter('QQ', 'q');
     $this->assertTrue($adapter->relatedAttributesSortUsesTwoAttributes());
     $this->assertEquals('q_id', $adapter->getColumnNameByPosition(0));
     $this->assertEquals('junk', $adapter->getColumnNameByPosition(1));
     $this->assertEquals('uniquerequiredemail', $adapter->getColumnNameByPosition(2));
     $adapter = new RedBeanModelAttributeToDataProviderAdapter('QQ', 'qRequired');
     $this->assertTrue($adapter->relatedAttributesSortUsesTwoAttributes());
     $this->assertEquals('erequired_q_id', $adapter->getColumnNameByPosition(0));
     $this->assertEquals('junk', $adapter->getColumnNameByPosition(1));
     $this->assertEquals('uniquerequiredemail', $adapter->getColumnNameByPosition(2));
     $adapter = new RedBeanModelAttributeToDataProviderAdapter('QQ', 'qUnique');
     $this->assertTrue($adapter->relatedAttributesSortUsesTwoAttributes());
     $this->assertEquals('eunique_q_id', $adapter->getColumnNameByPosition(0));
     $this->assertEquals('junk', $adapter->getColumnNameByPosition(1));
     $this->assertEquals('uniquerequiredemail', $adapter->getColumnNameByPosition(2));
     $adapter = new RedBeanModelAttributeToDataProviderAdapter('QQ', 'qMany');
     $this->assertTrue($adapter->relatedAttributesSortUsesTwoAttributes());
     $this->assertEquals('qq_id', $adapter->getColumnNameByPosition(0));
     $this->assertEquals('junk', $adapter->getColumnNameByPosition(1));
     $this->assertEquals('uniquerequiredemail', $adapter->getColumnNameByPosition(2));
 }
 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());
 }
 protected static function buildJoinForManyToManyRelatedAttributeAndGetWhereClauseData(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, $joinTablesAdapter)
 {
     assert('$modelAttributeToDataProviderAdapter->getRelatedAttribute() != null');
     assert('$joinTablesAdapter instanceof RedBeanModelJoinTablesQueryAdapter');
     assert('$modelAttributeToDataProviderAdapter->getRelationType() == RedBeanModel::MANY_MANY');
     $relationTableName = $modelAttributeToDataProviderAdapter->getRelationTableName();
     $onTableAliasName = self::resolveShouldAddFromTableAndGetAliasName($modelAttributeToDataProviderAdapter, $joinTablesAdapter);
     $manyToManyTables = array($relationTableName, $onTableAliasName);
     sort($manyToManyTables);
     $relationJoiningTableAliasName = $joinTablesAdapter->addLeftTableAndGetAliasName(implode('_', $manyToManyTables), "id", $onTableAliasName, $modelAttributeToDataProviderAdapter->getAttributeTableName() . '_id');
     //if this is not the id column, then add an additional left join.
     if ($modelAttributeToDataProviderAdapter->getRelatedAttribute() != 'id') {
         $joinTablesAdapter->setSelectDistinctToTrue();
         $relationTableAliasName = $joinTablesAdapter->addLeftTableAndGetAliasName($relationTableName, $relationTableName . '_id', $relationJoiningTableAliasName, 'id');
         $relationAttributeTableAliasName = $relationTableAliasName;
         $whereClauseRelationColumnNameToUse = $modelAttributeToDataProviderAdapter->getRelatedAttributeColumnName();
     } else {
         $whereClauseRelationColumnNameToUse = $relationTableName . '_id';
         $relationAttributeTableAliasName = $relationJoiningTableAliasName;
     }
     return array($relationAttributeTableAliasName, $whereClauseRelationColumnNameToUse);
 }
 /**
  * @param RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter
  * @param ModelJoinBuilder $builder
  * @return string
  */
 protected function resolvedTableAliasName(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, ModelJoinBuilder $builder)
 {
     if ($modelAttributeToDataProviderAdapter->hasRelatedAttribute()) {
         return $builder->getTableAliasNameForRelatedModel();
     } else {
         return $builder->getTableAliasNameForBaseModel();
     }
 }