Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hidden fields #151

Closed
peter-gribanov opened this issue Apr 25, 2017 · 2 comments
Closed

Hidden fields #151

peter-gribanov opened this issue Apr 25, 2017 · 2 comments

Comments

@peter-gribanov
Copy link
Member

peter-gribanov commented Apr 25, 2017

Some queries require complex conditions that can not be realized using your library.
Sometimes we need to add to the query a hidden field that will only be used for conditions.

Example from Doctrine docs:

$query = $em->createQuery('
    SELECT
        u,
        u.posts_count + u.likes_count AS HIDDEN score
    FROM
        CmsUser u
    ORDER BY
        score
');
$users = $query->getResult(); // array of User objects

Example implementation in my project as specification:

class PopularBroadcast extends BaseSpecification
{
    private $start;

    public function __construct(\DateTime $day, $dqlAlias = null)
    {
        // popular broadcast per month
        $this->start = $day->modify('-1 month')->setTime(0, 0, 0);

        parent::__construct($dqlAlias);
    }

    protected function getSpec()
    {
        return Spec::andX(
            Spec::innerJoin('broadcast_view_daily', 'bvd'),
            Spec::gte('day', $this->start, 'bvd'),
            Spec::groupBy('id'),
            Spec::having('views > 0')
        );
    }

    public function modify(QueryBuilder $qb, $dqlAlias)
    {
        $qb
            // add hidden field
            ->addSelect('SUM(bvd.viewed) AS HIDDEN views')
            // imposiable use Spec::orderBy() becose it add $dqlAlias for hidden field
            ->orderBy('views', 'DESC')
        ;

        parent::modify($qb, $dqlAlias);
    }
}
@peter-gribanov
Copy link
Member Author

peter-gribanov commented Apr 25, 2017

Example solution

Create ValueObject for hidden field and use it in conditions:

$field = new HiddenField('SUM(%s.viewed)', 'views', $dqlAlias); // $dqlAlias nullable

$spec = Spec::andX(
    Spec::addHiddenField($field),
    Spec::orderBy($field, 'DESC')
);

In OrderBy specification:

class OrderBy implements QueryModifier
{
    // ...

    public function modify(QueryBuilder $qb, $dqlAlias)
    {
        if ($this->dqlAlias !== null) {
            $dqlAlias = $this->dqlAlias;
        }

        if ($this->field instanceof HiddenField) {
            $field = $this->field->getAlias();
        } else {
            $field = sprintf('%s.%s', $dqlAlias, $this->field);
        }

        $qb->addOrderBy($field, $this->order);
    }
}

@peter-gribanov
Copy link
Member Author

Now we ca use hidden fields for order result:

// DQL: SELECT e, e.posts_count + e.likes_count HIDDEN score FROM User e ORDER BY score
$spec = Spec::andX(
    Spec::addSelect(
        Spec::selectHiddenAs(
            Spec::add(Spec::field('posts_count'), Spec::field('likes_count')),
            'score'
        )
    ),
    Spec::orderBy(
        Spec::alias('score')
    )
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant