stephpy/timeline-bundle

Is there have any solutions for query ODM's timeline, just like QueryBuilder?

Closed this issue · 2 comments

I would like to say thanks the great project. The database structures are beautiful.

I have one issue when I try to implement. How to made customize query from timeline if I use ODM?
I know QueryBuilder only support ORM right now, so I try to use doctrine manager to query Timeline document directly, I think it should be work, but unfortunately it doesn't. I can't filter timelines with components.

The situation is my timeline will use in 2 page, so, one action will have 2 components:
Component A is User Object
Component B is Target Object

In page One will show one User's all actions with many targets.
In page Two will show one Target's all actions by many users.

So, I would like to ask that can it be done in ODM?
If it can be done, may I have some crue to reach it?

I tried out the solution, but it is dummy and cost performance.

'''php
$dm = $this->get('doctrine_mongodb')->getManager();

    // find out which action_component has component
    $actionComponents = $dm->createQueryBuilder('ActivityBundle:ActionComponent')
            ->select('id')
            ->field('component.$id')->equals(new \MongoId($subject->getId()))
            ->getQuery()
            ->execute()
    ;

    $actionComponentIDs = array();
    foreach ($actionComponents as $actionComponent){
        $actionComponentIDs[] = new \MongoId($actionComponent->getId());
    }

    // then find out which action has action_components 
    $actions = $dm->createQueryBuilder('ActivityBundle:Action')
            ->field('actionComponents.$id')->in($actionComponentIDs)
            ->getQuery()
            ->execute()
    ;

    $actionIDs = array();
    foreach ($actions as $action){
        $actionIDs[] = new \MongoId($action->getId());
    }

    // finally
    $qb = $dm->createQueryBuilder('ActivityBundle:Timeline')
            ->field('action.$id')->in($actionIDs)
            ->sort('createdAt', 'desc')
    ;

    $resultBuilder = $this->get('spy_timeline.result_builder');
    $activities = $resultBuilder->fetchResults($qb, 1, 10, true, true);  // don't forget set "filter" be true

'''

My solution is dummy, may be we have more effective solution in future.
Post it, in case someone need it.

Hi,

Yes, QueryBuilder can be very very slow about performances and it's normal i guess. I'm not an expert on Mongo but in Mysql, there is no way to optimize queries ... May be a SearchEngine is an ideal solution if you want to use it.