Track your Doctrine Entities and create reports:
symedit_analytics:
tracker:
post: SymEdit\Bundle\BlogBundle\Model\Post
category: SymEdit\Bundle\BlogBundle\Model\Category
// Get an entity
$post = $repository->find(5);
// Track it
$this->get('symedit_analytics.tracker')->track($post);
{{ symedit_analytics_render() }}
Run reports:
$reporter = $this->get('symedit_analytics.reporter');
$popularPosts = $reporter->runReport('popular', array(
'model' => 'post',
'max' => 10,
));
class MyReport extends AbstractReport
{
public function buildQuery(QueryBuilder $queryBuilder, array $options = array())
{
return parent::buildQuery($queryBuilder, $options)
->andWhere('c.status = :status')
->setParameter('status', Post::STATUS_PUBLISHED);
}
public function setDefaultOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'published' => false,
'range' => (new \DateTime)->modify('1 week ago'),
));
}
public function getName()
{
return 'my_report';
}
}
And the configuration:
<service id="my_bundle.my_report" class="%my_bundle.my_report.class%">
<tag name="symedit_analytics.report" alias="my_report" />
</service>
And then use your report:
$reporter = $this->get('symedit_analytics.reporter');
$result = $reporter->runReport('my_report', array(/*...*/));
Unless you change the buildQuery substantially your results will look something like this:
array(
array(
'object' => [A Doctrine Entity]
'visits' => 45
),
...
)
You can also run reports in twig:
{% set report = symedit_analytics_report('popular', { model: 'post' }) %}
{% for result in report %}
{{ object.title }} - {{ visits }}
{% endfor %}