function it_applies_the_reference_data_selector_to_the_data_source($predecessor, Datasource $dataSource, DatagridConfiguration $configuration, QueryBuilder $qb, Join $join) { $source = ['attributes_configuration' => ['sku' => ['properties' => ['reference_data_name' => '']], 'color' => ['properties' => ['reference_data_name' => 'sole_color']]]]; $columns = ['sku' => ['some_config', 'some_other_config'], 'color' => ['some_config', 'some_other_config']]; $predecessor->apply($dataSource, $configuration)->shouldBeCalled(); $configuration->offsetGet('source')->willReturn($source); $configuration->offsetGet('columns')->willReturn($columns); $dataSource->getQueryBuilder()->willReturn($qb); $qb->leftJoin('values.', '')->shouldNotBeCalled(); $qb->leftJoin('values.sole_color', 'sole_color')->shouldBeCalled()->willReturn($qb); $qb->addSelect('sole_color')->shouldBeCalled()->willReturn($qb); $qb->getDQLPart('join')->willReturn(['p' => [$join]]); $qb->getRootAliases()->willReturn(['p']); $join->getAlias()->willReturn('values'); $this->apply($dataSource, $configuration); }
/** * @param QueryBuilder $qb * @param string $entityClass * @param string $fieldName * @return string */ protected function getVirtualFieldExpression(QueryBuilder $qb, $entityClass, $fieldName) { $rootAlias = $this->getRootTableAlias($qb); $conditions = ['entity' => $rootAlias]; $fieldConfig = $this->virtualFieldProvider->getVirtualFieldQuery($entityClass, $fieldName); $fieldConfigJoins = $fieldConfig['join']; $joins = $qb->getDQLPart('join'); if (empty($joins)) { foreach ($fieldConfigJoins as $type => $typedFieldConfigJoins) { foreach ($typedFieldConfigJoins as $typedFieldConfigJoin) { $join = new Join($type, $this->replaceAliases($conditions, $typedFieldConfigJoin['join']), $typedFieldConfigJoin['alias'], $typedFieldConfigJoin['conditionType'], $typedFieldConfigJoin['condition']); $qb->add('join', [$rootAlias => $join], true); $joins[$rootAlias][] = $join; } } } /** @var Join $join */ foreach ($joins[$rootAlias] as $join) { $joinType = strtolower($join->getJoinType()); if (array_key_exists($joinType, $fieldConfigJoins)) { foreach ($fieldConfigJoins[$joinType] as $fieldJoin) { if (strtoupper($fieldJoin['conditionType']) == strtoupper($join->getConditionType())) { $fixedJoin = $this->replaceAliases($conditions, $fieldJoin['join']); if ($fixedJoin == $join->getJoin()) { $conditions[$fieldJoin['alias']] = $join->getAlias(); $fixedCondition = $this->replaceAliases($conditions, $fieldJoin['condition']); if ($fixedCondition != (string) $join->getCondition()) { unset($conditions[$fieldJoin['alias']]); } } } } } } return $this->replaceAliases($conditions, $fieldConfig['select']['expr']); }
/** * @group DDC-1686 */ public function testExpressionGetter() { // Andx $andx = new Expr\Andx(array('1 = 1', '2 = 2')); $this->assertEquals(array('1 = 1', '2 = 2'), $andx->getParts()); // Comparison $comparison = new Expr\Comparison('foo', Expr\Comparison::EQ, 'bar'); $this->assertEquals('foo', $comparison->getLeftExpr()); $this->assertEquals('bar', $comparison->getRightExpr()); $this->assertEquals(Expr\Comparison::EQ, $comparison->getOperator()); // From $from = new Expr\From('Foo', 'f', 'f.id'); $this->assertEquals('f', $from->getAlias()); $this->assertEquals('Foo', $from->getFrom()); $this->assertEquals('f.id', $from->getIndexBy()); // Func $func = new Expr\Func('MAX', array('f.id')); $this->assertEquals('MAX', $func->getName()); $this->assertEquals(array('f.id'), $func->getArguments()); // GroupBy $group = new Expr\GroupBy(array('foo DESC', 'bar ASC')); $this->assertEquals(array('foo DESC', 'bar ASC'), $group->getParts()); // Join $join = new Expr\Join(Expr\Join::INNER_JOIN, 'f.bar', 'b', Expr\Join::ON, 'b.bar_id = 1', 'b.bar_id'); $this->assertEquals(Expr\Join::INNER_JOIN, $join->getJoinType()); $this->assertEquals(Expr\Join::ON, $join->getConditionType()); $this->assertEquals('b.bar_id = 1', $join->getCondition()); $this->assertEquals('b.bar_id', $join->getIndexBy()); $this->assertEquals('f.bar', $join->getJoin()); $this->assertEquals('b', $join->getAlias()); // Literal $literal = new Expr\Literal(array('foo')); $this->assertEquals(array('foo'), $literal->getParts()); // Math $math = new Expr\Math(10, '+', 20); $this->assertEquals(10, $math->getLeftExpr()); $this->assertEquals(20, $math->getRightExpr()); $this->assertEquals('+', $math->getOperator()); // OrderBy $order = new Expr\OrderBy('foo', 'DESC'); $this->assertEquals(array('foo DESC'), $order->getParts()); // Andx $orx = new Expr\Orx(array('foo = 1', 'bar = 2')); $this->assertEquals(array('foo = 1', 'bar = 2'), $orx->getParts()); // Select $select = new Expr\Select(array('foo', 'bar')); $this->assertEquals(array('foo', 'bar'), $select->getParts()); }
/** * Build and add an oder field to the query builder. * * @param \Doctrine\Orm\QueryBuilder $qb * @param mixed $order * @return Gmg_Service_Abstract */ protected function addOrder($qb, $order) { // add the where expression to the query // process the $order if (is_string($order)) { // straight DQL string $qb->orderBy($order); } elseif (is_array($order) && count($order)) { // loop through each order clause supplied foreach ($order as $col => $dir) { // set the alias to the default $alias = $this->alias; // if col relates to Relation i.e. Role.id // then set up the alias and column names (the join should have // already been performed in the $where) // TODO: this will cause an error if the column wasn't specified in the $where if (strpos($col, '.') !== false) { $parts = explode('.', $col); $col = array_pop($parts); $par = $this->alias; // test for existing joins $as = array(); foreach ($qb->getDQLPart('join') as $j) { $as[] = $j; } foreach ($parts as $rel) { $alias = strtolower($rel); $jt = new Expr\Join(Expr\Join::LEFT_JOIN, $par . '.' . $rel, $alias); if (!strpos($qb->getDql(), $jt->__toString()) !== false) { $qb->leftJoin($par . '.' . $rel, $alias); } $par = $alias; } } $qb->addOrderBy($alias . '.' . $col, $dir); } } return $this; }
/** * Gets the alias from a Join expression. * * @param Join $join * * @return string */ public static function getJoinAlias(Join $join) : string { static $aliasProperty = null; static $initialized = false; if (!$initialized && !method_exists(Join::class, 'getAlias')) { $aliasProperty = new \ReflectionProperty(Join::class, '_alias'); $aliasProperty->setAccessible(true); $initialized = true; } return null === $aliasProperty ? $join->getAlias() : $aliasProperty->getValue($join); }
/** * @param Expr\Join $join * @return array * @throws \RuntimeException */ private function validateJoinAssociation(Expr\Join $join) { $alias = $this->getJoinParentAlias($join->getJoin()); $association = $this->getJoinAssociation($join->getJoin()); $parentClassMetadata = $this->getClassMetadata($this->getClassByAlias($alias)); if (!$parentClassMetadata->hasAssociation($association)) { throw new RuntimeException(sprintf("Cannot find association named %s in class %s", $association, $parentClassMetadata->getName())); } }