使用 KNP 分页器进行分页根本不起作用



我非常努力地让KNP分页器工作。

我只想将实体添加到 id,但分页接受所有参数,但会用它进行注释!

这是我的代码:

使用 Symfony\Bundle\FrameworkBundle\Controller\Controller;使用 Symfony\Component\HttpFoundation\Request;

控制器类:

class StartController extends Controller
{
    public function indexAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $dql   = "SELECT a FROM MainArtBundle:Art a";
        $query = $em->createQuery($dql);

        $paginator  = $this->get('knp_paginator');
        $pagination = $paginator->paginate(
            $query,
            $request->query->get('page', 1)          /*page number*/,
            8                                        /*limit per page*/
        );
        $pagination->setUsedRoute('homepage');
        if (!$pagination) {
            throw $this->createNotFoundException('Unable to find Art entities.');
        }
        return $this->render('MainShowBundle:Default:index.html.twig', array(
                             'pagination' => $pagination,
        ));
    }

树枝模板:

<li>{{ knp_pagination_sortable(pagination, 'Oldest', 'a.id', {'direction': 'desc'}) }}</li>
<li>{{ knp_pagination_sortable(pagination, 'Newest', 'a.id', {'direction': 'asc'}) }}</li>

艺术实体(整个实体,因为错误可能是由实体引起的,可以吗)

<?php
namespace MainArtBundleEntity;
use GedmoMappingAnnotation as Gedmo;
use DoctrineORMMapping as ORM;
use MainLikeBundleEntityThumb;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyComponentHttpFoundationRequest;
/**
 * Art
 *
 * @ORMTable(name="art")
 * @ORMEntity(repositoryClass="MainArtBundleEntityArtRepository")
 */
class Art
{
    /**
     * @var integer $id
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var string $locale
     *
     * @ORMColumn(name="locale", type="string", length=5, nullable=false)
     */
    protected $locale;
    /**
     * @var arrayCollection $user
     * 
     * @ORMManyToOne(targetEntity="MainUserBundleEntityUser", inversedBy="arts")
     *
     */
    protected $user;
    /**
     * @var DoctrineCommonCollectionsArrayCollection $visits
     *
     * @ORMOneToMany(targetEntity="MainArtBundleEntityArtVisit", mappedBy="art", fetch="EXTRA_LAZY")
     *
     */
    protected $visits;
    /**
     * @var DoctrineCommonCollectionsArrayCollection $tags
     *
     * @ORMManyToMany(targetEntity="Tags", inversedBy="arts", cascade={"persist"})
     */
    protected $tags;
    /**
     * @var DoctrineCommonCollectionsArrayCollection $feature_partner
     * @ORMManyToMany(targetEntity="MainUserBundleEntityUser", inversedBy="feature_partner")
     *
     */
    protected $feature_partner;
    /**
     * @var string $headline
     *
     * @AssertNotBlank()
     *
     * @ORMColumn(type="string", length=255, unique=false, nullable=false)
     */
    protected $headline;
    /**
     * @var MainStorageBundleEntityImage
     *
     * @ORMOneToOne(targetEntity="MainStorageBundleEntityImage")
     * @ORMColumn(nullable=true)
     */
    protected $image;
    /**
     * @var string $content
     *
     * @AssertNotBlank()
     *
     * @ORMColumn(type="text", unique=false, nullable=false)
     */
    protected $content;
    /**
     * @var string $description
     *
     * @ORMColumn(type="text",  unique=false, nullable=true)
     */
    protected $description;
    /**
     * @var datetime $created
     *
     * @GedmoTimestampable(on="create")
     * @ORMColumn(type="datetime")
     */
    private $created;
    /**
     * @var datetime $updated
     *
     * @GedmoTimestampable(on="update")
     * @ORMColumn(type="datetime")
     */
    private $updated;
    /**
     * @var datetime $contentChanged
     *
     * @ORMColumn(name="content_changed", type="datetime", nullable=true)
     * @GedmoTimestampable(on="change", field={"headline", "content"})
     */
    private $contentChanged;
    /**
     * @var  integer $viewed
     *
     * @ORMColumn(name="viewed", type="integer", nullable=true)
     */
    private $viewed;

    /**
     * @var object $CommentThread
     *
     */
    private $thread_id;
    /**
     * @var DoctrineCommonCollectionsArrayCollection $thumbs
     *
     * @ORMOneToMany(targetEntity="MainLikeBundleEntityThumb", mappedBy="entity", fetch="EXTRA_LAZY")
     */
    private $thumbs;


    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->tags = new DoctrineCommonCollectionsArrayCollection();
        $this->feature_partner = new DoctrineCommonCollectionsArrayCollection();
        $this->thumbs = new DoctrineCommonCollectionsArrayCollection();
        $this->commentThread = new MainArtBundleEntityCommentThread($this->getId());
        /*$request = new Request();
        $this->locale = $request->getLocale()*/;
    }
    /**
     * @return string
     */
    public function __toString()
    {
        return $this->getHeadline();
    }
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set user
     *
     * @param integer $userId
     * @return Art
     */
    public function setUserId($userId)
    {
        $this->user = $userId;
        return $this;
    }
    /**
     * Get user
     *
     * @return integer 
     */
    public function getUserId()
    {
        return $this->user;
    }
    /**
     * Set tags
     *
     * @param integer $tags
     * @return Art
     */
    public function setTags($tags)
    {
        $this->tags = $tags;
        return $this;
    }
    /**
     * Get tags
     *
     * @return integer 
     */
    public function getTags()
    {
        return $this->tags;
    }
    /**
     * Set headline
     *
     * @param string $headline
     * @return Art
     */
    public function setHeadline($headline)
    {
        $this->headline = $headline;
        return $this;
    }
    /**
     * Get headline
     *
     * @return string 
     */
    public function getHeadline()
    {
        return $this->headline;
    }
    /**
     * Set content
     *
     * @param string $content
     * @return Art
     */
    public function setContent($content)
    {
        $this->content = $content;
        return $this;
    }
    /**
     * Get content
     *
     * @return string 
     */
    public function getContent()
    {
        return $this->content;
    }
    /**
     * Set created
     *
     * @param DateTime $created
     * @return Art
     */
    public function setCreated($created)
    {
        $this->created = $created;
        return $this;
    }
    /**
     * Get created
     *
     * @return DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }
    /**
     * Set updated
     *
     * @param DateTime $updated
     * @return Art
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;
        return $this;
    }
    /**
     * Get updated
     *
     * @return DateTime 
     */
    public function getUpdated()
    {
        return $this->updated;
    }
    /**
     * Set contentChanged
     *
     * @param DateTime $contentChanged
     * @return Art
     */
    public function setContentChanged($contentChanged)
    {
        $this->contentChanged = $contentChanged;
        return $this;
    }
    /**
     * Get contentChanged
     *
     * @return DateTime 
     */
    public function getContentChanged()
    {
        return $this->contentChanged;
    }
    /**
     * Set feature_partner
     *
     * @param integer $featurePartner
     * @return Art
     */
    public function setFeaturePartner($featurePartner)
    {
        $this->feature_partner = $featurePartner;
        return $this;
    }
    /**
     * Get feature_partner
     *
     * @return DoctrineCommonCollectionsArrayCollection
     */
    public function getFeaturePartner()
    {
        return $this->feature_partner;
    }
    /**
     * Add feature_partner
     *
     * @param MainUserBundleEntityUser $featurePartner
     * @return Art
     */
    public function addFeaturePartner(MainUserBundleEntityUser $featurePartner)
    {
        $this->feature_partner[] = $featurePartner;
        return $this;
    }
    /**
     * Remove feature_partner
     *
     * @param MainUserBundleEntityUser $featurePartner
     */
    public function removeFeaturePartner(MainUserBundleEntityUser $featurePartner)
    {
        $this->feature_partner->removeElement($featurePartner);
    }
    /**
     * Set description
     *
     * @param string $description
     * @return Art
     */
    public function setDescription($description)
    {
        $this->description = $description;
        return $this;
    }
    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }
    /**
     * Set user
     *
     * @param MainUserBundleEntityUser $user
     * @return Art
     */
    public function setUser(MainUserBundleEntityUser $user = null)
    {
        $this->user = $user;
        return $this;
    }
    /**
     * Get user
     *
     * @return MainUserBundleEntityUser 
     */
    public function getUser()
    {
        return $this->user;
    }
    /**
     * Add tags
     *
     * @param MainArtBundleEntityTags $tags
     * @return Art
     */
    public function addTag(MainArtBundleEntityTags $tags)
    {
        $this->tags[] = $tags;
        return $this;
    }
    /**
     * Remove tags
     *
     * @param MainArtBundleEntityTags $tags
     */
    public function removeTag(MainArtBundleEntityTags $tags)
    {
        $this->tags->removeElement($tags);
    }
    /**
     * Add visits
     *
     * @param MainArtBundleEntityArtVisit $visits
     * @return Art
     */
    public function addVisit(MainArtBundleEntityArtVisit $visits)
    {
        $this->visits[] = $visits;
        return $this;
    }
    /**
     * Remove visits
     *
     * @param MainArtBundleEntityArtVisit $visits
     */
    public function removeVisit(MainArtBundleEntityArtVisit $visits)
    {
        $this->visits->removeElement($visits);
    }
    /**
     * Get visits
     *
     * @return DoctrineCommonCollectionsCollection 
     */
    public function getVisits()
    {
        return $this->visits;
    }
    /**
     * Set locale
     *
     * @param string $locale
     * @return Art
     */
    public function setLocale($locale)
    {
        $this->locale = $locale;
        return $this;
    }
    /**
     * Get locale
     *
     * @return string
     */
    public function getLocale()
    {
        return $this->locale;
    }
    /**
     * Set image
     *
     * @param MainStorageBundleEntityImage $image
     * @return Art
     */
    public function setImage(MainStorageBundleEntityImage $image = null)
    {
        $this->image = $image;
        return $this;
    }
    /**
     * Get image
     *
     * @return MainStorageBundleEntityImage 
     */
    public function getImage()
    {
        return $this->image;
    }
    /**
     * Set viewed
     *
     * @param integer $viewed
     * @return Art
     */
    public function setViewed($viewed)
    {
        $this->viewed = $viewed;
        return $this;
    }
    /**
     * Get viewed
     *
     * @return integer 
     */
    public function getViewed()
    {
        return $this->viewed;
    }
    /**
     * Set thumbs
     *
     * @param MainLikeBundleEntityThumb $thumbs
     * @return Art
     */
    public function setThumbs(Thumb $thumbs = null)
    {
        $this->thumbs = $thumbs;
        return $this;
    }
    /**
     * Get thumbs
     *
     * @return DoctrineCommonCollectionsArrayCollection $thumbs
     */
    public function getThumbs()
    {
        return $this->thumbs;
    }
    /**
     * Add thumb
     *
     * @param MainLikeBundleEntityThumb $thumb
     * @return $this $thumbs
     */
    public function addThumb(Thumb $thumb)
    {
        $this->thumbs[] = $thumb;
        return $this;
    }
    /**
     * Remove thumbs
     *
     * @param MainLikeBundleEntityThumb $thumbs
     */
    public function removeThumb(MainLikeBundleEntityThumb $thumbs)
    {
        $this->thumbs->removeElement($thumbs);
    }
    /**
     * Count all Thumbs of the piece of art.
     */
    public function countAllThumbs() {
        return $this->thumbs->count();
    }
    /**
     * Check weather a user has thumbed a piece of art or not.
     */
    public function isThumbed($user) {
        // my first Closure :D
        $p = function($key, $element) use ($user) {
             return $element->getUser() == $user;
        };
        return $this->thumbs->exists($p);
    }
    /**
     * Get the thumb object from a special user.
     */
    public function getThumbFromUser ($user) {
        $p = function($element) use ($user) {
            return $element->getUser() == $user;
        };
        return $this->thumbs->filter($p);
    }

}

KNP 分页配置(在 config.yml 中)

knp_paginator:
    page_range: 5                      # default page range used in pagination control
    default_options:
        page_name: page                # page query parameter name
        sort_field_name: sort          # sort field query parameter name
        sort_direction_name: direction # sort direction query parameter name
        distinct: true                 # ensure distinct results, useful when ORM queries are using GROUP BY statements
    template:
        pagination: ::Paginationtwitter_bootstrap_v3_pagination.html.twig     # sliding pagination controls template
        sortable: ::Paginationsortable_link.html.twig                         # sort link template

我已经将分页对象转储到控制器和模板中。每次参数在那里(direction= asc 或 desc)时,url 中都会出现这样的东西:

?sort=a.id&direction=desc&page=1

但是如果我单击链接以更改方向:没有任何变化!

我相信 knp 分页有一个错误!或者我很愚蠢;)

如果有人能帮助我,我会很高兴!

问候迈克尔

对我来说,我创建了一个新文件 knp_paginator.yaml 而不是 knp_paginator.yml,我添加了这个配置,evrything 工作:

knp_paginator:
    page_range: 5                       # number of links shown in the pagination menu (e.g: you have 10 pages, a page_range of 3, on the 5th page you'll see links to page 4, 5, 6)
    default_options:
        page_name: page                 # page query parameter name
        sort_field_name: sort           # sort field query parameter name
        sort_direction_name: direction  # sort direction query parameter name
        distinct: true                  # ensure distinct results, useful when ORM queries are using GROUP BY statements
        filter_field_name: filterField  # filter field query parameter name
        filter_value_name: filterValue  # filter value query parameter name
    template:
        pagination: '@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig'     # sliding pagination controls template
        sortable: '@KnpPaginator/Pagination/sortable_link.html.twig' # sort link template
        filtration: '@KnpPaginator/Pagination/filtration.html.twig'  # filters template

相关内容

  • 没有找到相关文章

最新更新