Pagination
Opened this issue · 29 comments
Hello. I get the following error to paginate:
I tried the two configuration:
paginator: spy_timeline.paginator.knp
paginator: ~
My config actually is:
#timeline
spy_timeline:
drivers:
orm:
object_manager: doctrine.orm.entity_manager
classes:
query_builder: ~ # Spy\TimelineBundle\Driver\ORM\QueryBuilder\QueryBuilder
timeline: ant\SocialBundle\Entity\Timeline
action: ant\SocialBundle\Entity\Action
component: ant\SocialBundle\Entity\Component
action_component: ant\SocialBundle\Entity\ActionComponent
filters:
duplicate_key:
priority: 10
data_hydrator:
priority: 20
filter_unresolved: true
locators:
- spy_timeline.filter.data_hydrator.locator.doctrine_orm
render:
path: 'SocialBundle:Timeline/verbs'
fallback: 'SocialBundle:Timeline:default.html.twig'
resources:
- 'sdfsBundle:Timeline:components.html.twig'
paginator: spy_timeline.paginator.knp
#Knp_paginator
knp_paginator:
page_range: 5
default_options:
page_name: page
sort_field_name: sort
sort_direction_name: dirección
distinct: true
template:
pagination: sdfsBundle:Pagination:pagination.html.twig
sortable: KnpPaginatorBundle:Pagination:sortable_link.html.twig
My controller is:
/**
* @Template
*/
public function timelineAction()
{
$u = $this->get('security.context')->getToken()->getUser();
$actionManager = $this->get('spy_timeline.action_manager');
$timelineManager = $this->get('spy_timeline.timeline_manager');
$subject = $actionManager->findOrCreateComponent($u);
$timeline = $timelineManager->getTimeline($subject, array('page' => 1, 'max_per_page' => '10', 'paginate' => true));
return array('timeline' => $timeline);
}
And I get the following error:
An exception has been thrown during the rendering of a template ("An exception occurred while executing 'SELECT COUNT(*) AS dctrn_count FROM
(SELECT DISTINCT id3 FROM (SELECT s0_.context AS context0, s0_.type AS type1, s0_.created_at AS created_at2, s0_.id AS id3, s1_.verb AS verb4, s1_.status_current AS status_current5, s1_.status_wanted AS status_wanted6, s1_.duplicate_key AS duplicate_key7, s1_.duplicate_priority AS duplicate_priority8, s1_.created_at AS created_at9, s1_.id AS id10, s2_.type AS type11, s2_.text AS text12, s2_.id AS id13, s3_.model AS model14, s3_.identifier AS identifier15, s3_.hash AS hash16, s3_.id AS id17, s0_.action_id AS action_id18, s0_.subject_id AS subject_id19, s2_.action_id AS action_id20, s2_.component_id AS component_id21
FROM spy_timeline s0_ INNER JOIN spy_timeline_action s1_ ON s0_.action_id = s1_.id
LEFT JOIN spy_timeline_action_component s2_ ON s1_.id = s2_.action_id
LEFT JOIN spy_timeline_component s3_ ON s2_.component_id = s3_.id
WHERE s0_.type = ? AND s0_.context = ? AND s0_.subject_id = ?
ORDER BY s0_.created_at DESC) dctrn_result) dctrn_table' with params {"1":"timeline","2":"ESTADO","3":{}}:
Catchable Fatal Error: Object of class ant\SocialBundle\Entity\Component could not be converted to string in /var/www/workspace/myproject/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php line 1211")
in /var/www/workspace/myproject/vendor/ping86/social-bundle/ant/SocialBundle/Resources/views/Muro/index.html.twig at line 25.
I'll try to reproduce this issue asap.
I'm sorry, i'm busy at this moment, i'll look at this as soon as possible
Ok No problem! : )
Any update on this? Seeing the same thing.
I'll have a look on it this week, sorry ...
Awesome, thanks
I created exactly same context, but i have not this issue :s
What do you do on twig template ? if you only iterate on results, it's throw the exception ?
It seems somewhere something try to cast in string the Component which is very very weird. It's dirty, but you could add a __toString method on your Entity/Component ... May it'll fix your issue.
public function __toString()
{
return $this->getId();
}
Can you paste me your twig file ? I can't define at this moment where/when a string cast is made on component :\
WHERE s0_.type = ? AND s0_.context = ? AND s0_.subject_id = ?
ORDER BY s0_.created_at DESC) dctrn_result) dctrn_table' with params {"1":"timeline","2":"ESTADO","3":{}}:
In exception, it seems subject_id is {} ... it should be the of compoennt id, weird.
This is my twig template.
{% for action in timeline %}
<div class="notificacion">
<div class="container-fluid">
<div class="row-fluid">
{{ timeline_render(action) }}
</div>
</div>
</div>
<hr/>
{% else %}
<p>{{ 'timeline.noAction' | trans({}, 'timeline') }}</p>
{% endfor %}
And the controller is :
/**
* @Template
*/
public function myTimelineAction($u = null)
{
if ($u == null) $u = $this->get('security.context')->getToken()->getUser();
$actionManager = $this->get('spy_timeline.action_manager');
$subject = $actionManager->findOrCreateComponent($u);
$timeline = $actionManager->getSubjectActions($subject);
return array('timeline' => $timeline, 'usuario'=> $u);
}
There's nothing freak ... did you try to put __toString method on Component ?
If you comment {{ timeline_render }}, do you still have this exception ?
I dont know.
This is what I have.
config.yml of timeline
#timeline
spy_timeline:
paginator: spy_timeline.paginator.knp
drivers:
orm:
object_manager: doctrine.orm.entity_manager
classes:
query_builder: ~ # Spy\TimelineBundle\Driver\ORM\QueryBuilder\QueryBuilder
timeline: ant\SocialBundle\Entity\Timeline
action: ant\SocialBundle\Entity\Action
component: ant\SocialBundle\Entity\Component
action_component: ant\SocialBundle\Entity\ActionComponent
filters:
duplicate_key:
priority: 10
data_hydrator:
priority: 20
filter_unresolved: true
locators:
- spy_timeline.filter.data_hydrator.locator.doctrine_orm
render:
path: 'SocialBundle:Timeline/verbs'
fallback: 'SocialBundle:Timeline:default.html.twig'
resources:
- 'MyBundle:Timeline:components.html.twig'
My twig template
{% for action in timeline %}
<div class="notificacion">
<div class="container-fluid">
<div class="row-fluid">
{{ timeline_render(action) }}
</div>
</div>
</div>
<hr/>
{% else %}
<p>{{ 'timeline.noAction' | trans({}, 'timeline') }}</p>
{% endfor %}
{{ knp_pagination_render(timeline) }}
My component.php
<?php
namespace ant\SocialBundle\Entity;
use Spy\TimelineBundle\Entity\Component as BaseComponent;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="spy_timeline_component")
*/
class Component extends BaseComponent
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __toString()
{
return $this->getId();
}
}
And my controller
/**
* @Template
*/
public function myTimelineAction($u = null)
{
if ($u == null) $u = $this->get('security.context')->getToken()->getUser();
$actionManager = $this->get('spy_timeline.action_manager');
$subject = $actionManager->findOrCreateComponent($u);
$timeline = $actionManager->getSubjectActions($subject, array('page' => 1, 'max_per_page' => '3', 'paginate' => true));
return array('timeline' => $timeline, 'usuario'=> $u);
}
And I get the same error.
:_(
And yes !!
If I comment this line:
{# {{ timeline_render(action) }} #}
I get the error.
How can I use the pagination of timeline bundle ?? Maybe the problem is knpPaginator...
Mmmh, what is {{ knp_pagination_render(timeline) }}
?
Try to comment it ...
Else
If you setted __toString
method on your ant\SocialBundle\Entity\Component
, you could not have this error
Catchable Fatal Error: Object of class ant\SocialBundle\Entity\Component could not be converted to string
Edit it and retry:
public function __toString()
{
return (string) $this->getId();
}
How can I use the pagination of timeline bundle ?? Maybe the problem is knpPaginator...
You tried without knp paginator ? I guess you did.
If it's not throw an exception, uncomment for
and
{{ knp_pagination_render(timeline) }} is to use knp_paginator. It show the select of pagination.
If I comment it, I see 3 actions, but I cant choose more pages.
I modified it:
public function __toString()
{
return (string) $this->getId();
}
And now, in my timeline, I dont see none action. The timeline is empty. But I dont get error.
To use without knp paginator that I have to write ?
I did same documentation:
https://github.com/stephpy/TimelineBundle/blob/master/Resources/doc/pagination.markdown
$timeline = $actionManager->getSubjectActions($subject, array('page' => 1, 'max_per_page' => '3', 'paginate' => true));
spy_timeline:
paginator: ~
Because I don't see the select of choose page of pagination, how include it in my template twig ... ?
Thanks for help me! : )
That's the knp_pagination_render which make this fail by this way.
You can remove __toString
method on Component
.
$timeline is not an instanceof Knp\Component\Pager\Paginator
but an instanceof Spy\Timeline\ResultBuilder\Pager\KnpPager
which make the trouble i guess.
There is 2 solutions:
You define your own macro for Spy\Timeline\ResultBuilder\Pager\PagerInterface
classes, or try:
{{ knp_pagination_render(timeline.iterator) }}
$pager->getIterator() will return an object managed by Knp\Paginator component :)
Let me know.
aaahmmm I think it doesnt work.
I changed, {{ knp_pagination_render(timeline.iterator) }} in my template twig
And my timeline, is empty...
Now, I deleted the __tostring method on Component.
I get the same error....
How can I use the pagination timeline, without use the pagination Knp ??
I did'nt try with this use case (with pagination render), i'll look at this tomorrow.
If you can't wait, as i said, you have a https://github.com/stephpy/timeline/blob/master/src/Spy/Timeline/ResultBuilder/Pager/PagerInterface.php object, and you'll be able on your twig to deal with that
{% if timeline.haveToPaginate %}
{% set lastPage = timeline.lastPage %}
{% set currentPage = timeline.page %}
....
{% endif %
That's sure this error comes from this pagination renderer, by this way, i should have the same issue at home.
I found issue, i tested with old version of KnpPaginatorBundle which changes his method to fetch number of results to paginate.
There is an issue on their new behavior, doctrine allow to give as parameter a object which has doctrine metadata and will automatically return primary keys on SQL.
KnpPaginatorBundle expects scalar results and by this way ... it fails ...
You still have to use timeline.iterator
look documentation
Really, really sorry for delay.
@stephpy nothing has changed.
I updated the timeline bundle! But my timeline is empty!
I repeat my code ok ? Maybe you see some wrong.
My config.yml
spy_timeline:
paginator: spy_timeline.paginator.knp
drivers:
orm:
object_manager: doctrine.orm.entity_manager
classes:
query_builder: ~
timeline: ant\SocialBundle\Entity\Timeline
action: ant\SocialBundle\Entity\Action
component: ant\SocialBundle\Entity\Component
action_component: ant\SocialBundle\Entity\ActionComponent
filters:
duplicate_key:
priority: 10
data_hydrator:
priority: 20
filter_unresolved: true
locators:
- spy_timeline.filter.data_hydrator.locator.doctrine_orm
render:
path: 'SocialBundle:Timeline/verbs'
fallback: 'SocialBundle:Timeline:default.html.twig'
resources:
- 'MyBundle:Timeline:components.html.twig'
My twig template
{% for action in timeline %}
<div class="notificacion">
<div class="container-fluid">
<div class="row-fluid">
{{ timeline_render(action) }}
</div>
</div>
</div>
<hr/>
{% else %}
<p>{{ 'timeline.noAction' | trans({}, 'timeline') }}</p>
{% endfor %}
{{ knp_pagination_render(timeline.iterator) }}
And my controller:
/**
* @Template
*/
public function myTimelineAction($u = null)
{
if ($u == null) $u = $this->get('security.context')->getToken()->getUser();
$actionManager = $this->get('spy_timeline.action_manager');
$subject = $actionManager->findOrCreateComponent($u);
$timeline = $actionManager->getSubjectActions($subject, array('page' => 1, 'max_per_page' => '3', 'paginate' => true));
return array('timeline' => $timeline, 'usuario'=> $u);
}
Is all correct ? I dont get the error, but the timeline is empty...
I'll give a new try this night.
Without paginate => true option, you have some actions ? What is the version of your knpPaginatorBundle knpComponents ?
I tried with same configuration and i had no issue. May version of KnpPaginatorBundle/KnpComponents is not the same.
This my version of knp.
"knplabs/knp-paginator-bundle": "2.3.*@dev",
If you know a version that works with the pagination of timeline , you tell me and I change! : )
Or show me the code correct to pagination, only I want that work the pagination! XD
I used exactly same code.
Can you answer to this question please:
Without paginate => true option, you have some actions ?
It's anormal, could you dump me that.
public function myTimelineAction($u = null)
{
if ($u == null) $u = $this->get('security.context')->getToken()->getUser();
$actionManager = $this->get('spy_timeline.action_manager');
$subject = $actionManager->findOrCreateComponent($u);
$timeline = $actionManager->getSubjectActions($subject, array('page' => 1, 'max_per_page' => '3', 'paginate' => true));
var_dump(count($timeline));
return array('timeline' => $timeline, 'usuario'=> $u);
}
It's important for me to know if without pagination you have results, and without no.
Yes !
With " pagination = true " my timeline is empty, and my var_dump(count($timeline)); = int(0).
With "pagination = false " my timeline is correct, and my var_dump(count($timeline)); = int(3);
Weird :s If you're comfortable with sql, you could look at difference between SQL requests with pagination and without. (in sf debug tool bar). Execute theses SQL requests and you'll be able to know why 0 actions are returned. This behavior does not occur at me.
If you are not comfortable with sql, please, paste me theses requests.
Hello @stephpy sorry for delay.
The difference between the call with and without paginate is this call to mysql:
SELECT COUNT(*) AS dctrn_count FROM (SELECT DISTINCT id6 FROM (SELECT s0_.verb AS verb0, s0_.status_current AS status_current1, s0_.status_wanted AS status_wanted2, s0_.duplicate_key AS duplicate_key3, s0_.duplicate_priority AS duplicate_priority4, s0_.created_at AS created_at5, s0_.id AS id6, s1_.type AS type7, s1_.text AS text8, s1_.id AS id9, s2_.model AS model10, s2_.identifier AS identifier11, s2_.hash AS hash12, s2_.id AS id13, s1_.action_id AS action_id14, s1_.component_id AS component_id15 FROM spy_timeline_action s0_ INNER JOIN spy_timeline_action_component s3_ ON s0_.id = s3_.action_id AND ((s3_.action_id = s0_.id AND s3_.component_id = 'subject' AND s3_.type = 109)) LEFT JOIN spy_timeline_action_component s1_ ON s0_.id = s1_.action_id LEFT JOIN spy_timeline_component s2_ ON s1_.component_id = s2_.id WHERE s0_.status_current = 'published' ORDER BY s0_.created_at DESC) dctrn_result) dctrn_table
This SQL request is with paginate, when I have paginate false, this request doesnt exist...
this help to you?
Another discovery
If I have in my config:
spy_timeline:
paginator: ~
And in my template:
{% if timeline.haveToPaginate %}
{% set lastPage = timeline.lastPage %}
{% set currentPage = timeline.page %}
{% endif %}
{{ lastPage }}
{{ currentPage }}
{{ knp_pagination_render(timeline.iterator) }}
I see the last Page and the currentPage. 5 and 1 respectively.
But the line {{ knp_pagination_render(timeline.iterator) }} show the error:
FatalErrorException: Error: Call to undefined method ArrayIterator::getTemplate() in /var/www/workspace/myproject/vendor/knplabs/knp-paginator-bundle/Knp/Bundle/PaginatorBundle/Twig/Extension/PaginationExtension.php line 56
Hi everybody...
Same werid problem here.
If using
paginator: spy_timeline.paginator.knp
and doing
$timeline = $actionManager->getSubjectActions($subject,
array('page' => 1, 'max_per_page' => '10', 'paginate' => true));
I got an empty resultset.
If I set the last parameter to false, i will get the expected results.
Is there something that i'm missing?
Thanks