/** * @param $onTableAliasName * @return null|string */ protected function resolveJoinsForForARelationAttributeThatIsManyToMany($onTableAliasName) { assert('is_string($onTableAliasName)'); $relationTableName = $this->modelAttributeToDataProviderAdapter->getRelationTableName(); $attributeTableName = $this->modelAttributeToDataProviderAdapter->getAttributeTableName(); $relationJoiningTableAliasName = $this->joinTablesAdapter->addLeftTableAndGetAliasName($this->modelAttributeToDataProviderAdapter->getManyToManyTableName(), "id", $onTableAliasName, self::resolveForeignKey($attributeTableName)); //if this is not the id column, then add an additional left join. if ($this->modelAttributeToDataProviderAdapter->getRelatedAttribute() != 'id') { $this->resolveSetToDistinct(); return $this->joinTablesAdapter->addLeftTableAndGetAliasName($relationTableName, self::resolveForeignKey($relationTableName), $relationJoiningTableAliasName, 'id'); } else { return $relationJoiningTableAliasName; } }
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 buildWhereForRelatedAttributeThatIsItselfAHasManyRelation(RedBeanModelAttributeToDataProviderAdapter $modelAttributeToDataProviderAdapter, $joinTablesAdapter, $relationAttributeTableAliasName, $operatorType, $value, &$where, $whereKey) { assert('$joinTablesAdapter instanceof RedBeanModelJoinTablesQueryAdapter'); assert('is_string($relationAttributeTableAliasName)'); assert('is_string($operatorType)'); assert('is_array($value) && count($value) > 0'); assert('is_array($where)'); assert('is_int($whereKey)'); $relationAttributeName = $modelAttributeToDataProviderAdapter->getRelatedAttribute(); $relationAttributeModelClassName = $modelAttributeToDataProviderAdapter->getRelatedAttributeRelationModelClassName(); if ($relationAttributeModelClassName != 'CustomFieldValue') { //Until we can add a third parameter to the search adapter metadata, we have to assume we are only doing //this for CustomFieldValue searches. Below we have $joinColumnName, since we don't have any other way //of ascertaining this information for now. throw new NotSupportedException(); } if ($operatorType != 'oneOf') { //only support oneOf for the moment. Once we add allOf, need to have an alternative sub-query //below that uses if/else logic to compare count against how many possibles. then return 1 or 0. } $relationAttributeTableName = RedBeanModel::getTableName($relationAttributeModelClassName); $tableAliasName = $relationAttributeTableName; $joinColumnName = 'value'; $relationColumnName = RedBeanModel::getTableName($modelAttributeToDataProviderAdapter->getRelatedAttributeModelClassName()) . "_id"; $quote = DatabaseCompatibilityUtil::getQuote(); $where[$whereKey] = "(1 = (select 1 from {$quote}{$relationAttributeTableName}{$quote} {$tableAliasName} " . "where {$quote}{$tableAliasName}{$quote}.{$quote}{$relationColumnName}{$quote} = " . "{$quote}{$relationAttributeTableAliasName}{$quote}.id " . "and {$quote}{$tableAliasName}{$quote}.{$quote}{$joinColumnName}{$quote} " . DatabaseCompatibilityUtil::getOperatorAndValueWherePart($operatorType, $value) . " limit 1))"; }