stephpy / timeline-bundle

Symfony2 bundle to make timeline
193 stars 57 forks source link

Select Timeline type in query builder #179

Closed coatezy closed 7 years ago

coatezy commented 9 years ago

Hi Stéphane,

I was wondering if there was a way to select the timeline type using the query builder? I'd like to build a query for unread notifications. I've had a good dig around but can't seem to fine anyway of doing this. The query builder seems to default to the timeline type?

Tom

coatezy commented 9 years ago

Hey,

I see why you cant query by timeline type. Because 'type' is already being use in the $fieldLocation array here ( https://github.com/stephpy/timeline/blob/master/src/Driver/QueryBuilder/QueryBuilder.php#L46)

It can't be mapped again. I have hacked a fix by extending CriteriaField then adding

    if ($asserter->getField() == 'timeline_type') {
        $field          = 'type';
        $this->location = 'timeline';
    } else {
        $field          = $asserter->getField();
        $this->location = QueryBuilder::getFieldLocation($field);
    }

to the createFromAsserter function.

I then extended QueryBuilder and updated the 'spy_timeline.query_builder.class' parameter to use my class etc..

I will probably revisit and come up with a more elegant solution but it would be great to see something similar implemented within the timeline library and bundle.

Tom

stephpy commented 9 years ago

Hi,

Did you see Notification section ?

I guess it could answer to yours questions.

coatezy commented 9 years ago

Hey,

I'm using notifications but what I'd like to do is find all unread notifications where the timeline subject component is the current user and the indirectComplement is the users Team. Therefore when the user is viewing a specific team, they only see notifications for them within the context of that team. Any ideas how I could achieve this?

Notifiers are really cool by the way. I'm using them to fire push notifications etc.

Tom

stephpy commented 9 years ago

Ok, i didn't understand your use case.

Indeed the TimelineBundle QueryBuilder has many defaults. It's not really optimized and it's hard to ... If you can do your own SQL query to get actions then apply filters (if you have), it's great.

And if you come with a PR to add your usecase into the timeline-bundle it's great too ;). But IMO QueryBuilder of TimelineBundle is a tool which should be used for easy queries.

coatezy commented 9 years ago

No problem,

I ended up creating a activity repository with my desired query.

class ActionRepository extends EntityRepository
{
    public function findBySubjectAndComponentId($subjectId, $componentId = null, $timelineType = 'notifications')
    {
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb->select('a')
            ->from('AcmeTimelineBundle:Action', 'a')
            ->join('a.timelines', 't')
            ->join('a.actionComponents', 'ac')
            ->where('t.subject = :timelineSubjectId')
            ->andWhere('t.type = :timelineType')
            ->setParameter('timelineSubjectId', $subjectId)
            ->setParameter('timelineType', $timelineType);

        if ($componentId) {
            $qb->andWhere('ac.component = :actionComponentType')->setParameter('actionComponentType', $componentId);
        }

        return $qb->getQuery()->getResult();
    }
}

And then passed the collection through my filters

    $actions = $repository->findBySubjectAndComponentId($subject->getId(), $component->getIdentifier(), 'notification');
    $fm = $this->get('spy_timeline.filter.manager');
    $fm = $this->get('spy_timeline.filter.manager');
    $filteredActions = $fm->filter($actions);

Tom

stephpy commented 9 years ago

:+1: