我非常努力地让KNP Paginator工作。
我只想要一个实体到id,但是分页接受了所有的参数,但注意到它!
这是我的代码:
使用Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller; 使用Symfony \ Component \ HttpFoundation \ Request;
Controller类:
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>
ArtEntity (整个实体因为错误可能是由实体引起的,可能是)
<?php
namespace Main\ArtBundle\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Main\LikeBundle\Entity\Thumb;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\Request;
/**
* Art
*
* @ORM\Table(name="art")
* @ORM\Entity(repositoryClass="Main\ArtBundle\Entity\ArtRepository")
*/
class Art
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $locale
*
* @ORM\Column(name="locale", type="string", length=5, nullable=false)
*/
protected $locale;
/**
* @var arrayCollection $user
*
* @ORM\ManyToOne(targetEntity="Main\UserBundle\Entity\User", inversedBy="arts")
*
*/
protected $user;
/**
* @var \Doctrine\Common\Collections\ArrayCollection $visits
*
* @ORM\OneToMany(targetEntity="Main\ArtBundle\Entity\ArtVisit", mappedBy="art", fetch="EXTRA_LAZY")
*
*/
protected $visits;
/**
* @var \Doctrine\Common\Collections\ArrayCollection $tags
*
* @ORM\ManyToMany(targetEntity="Tags", inversedBy="arts", cascade={"persist"})
*/
protected $tags;
/**
* @var \Doctrine\Common\Collections\ArrayCollection $feature_partner
* @ORM\ManyToMany(targetEntity="Main\UserBundle\Entity\User", inversedBy="feature_partner")
*
*/
protected $feature_partner;
/**
* @var string $headline
*
* @Assert\NotBlank()
*
* @ORM\Column(type="string", length=255, unique=false, nullable=false)
*/
protected $headline;
/**
* @var \Main\StorageBundle\Entity\Image
*
* @ORM\OneToOne(targetEntity="Main\StorageBundle\Entity\Image")
* @ORM\Column(nullable=true)
*/
protected $image;
/**
* @var string $content
*
* @Assert\NotBlank()
*
* @ORM\Column(type="text", unique=false, nullable=false)
*/
protected $content;
/**
* @var string $description
*
* @ORM\Column(type="text", unique=false, nullable=true)
*/
protected $description;
/**
* @var datetime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created;
/**
* @var datetime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated;
/**
* @var datetime $contentChanged
*
* @ORM\Column(name="content_changed", type="datetime", nullable=true)
* @Gedmo\Timestampable(on="change", field={"headline", "content"})
*/
private $contentChanged;
/**
* @var integer $viewed
*
* @ORM\Column(name="viewed", type="integer", nullable=true)
*/
private $viewed;
/**
* @var object $CommentThread
*
*/
private $thread_id;
/**
* @var \Doctrine\Common\Collections\ArrayCollection $thumbs
*
* @ORM\OneToMany(targetEntity="Main\LikeBundle\Entity\Thumb", mappedBy="entity", fetch="EXTRA_LAZY")
*/
private $thumbs;
/**
* Constructor.
*/
public function __construct()
{
$this->tags = new \Doctrine\Common\Collections\ArrayCollection();
$this->feature_partner = new \Doctrine\Common\Collections\ArrayCollection();
$this->thumbs = new \Doctrine\Common\Collections\ArrayCollection();
$this->commentThread = new \Main\ArtBundle\Entity\CommentThread($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 \Doctrine\Common\Collections\ArrayCollection
*/
public function getFeaturePartner()
{
return $this->feature_partner;
}
/**
* Add feature_partner
*
* @param \Main\UserBundle\Entity\User $featurePartner
* @return Art
*/
public function addFeaturePartner(\Main\UserBundle\Entity\User $featurePartner)
{
$this->feature_partner[] = $featurePartner;
return $this;
}
/**
* Remove feature_partner
*
* @param \Main\UserBundle\Entity\User $featurePartner
*/
public function removeFeaturePartner(\Main\UserBundle\Entity\User $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 \Main\UserBundle\Entity\User $user
* @return Art
*/
public function setUser(\Main\UserBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \Main\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Add tags
*
* @param \Main\ArtBundle\Entity\Tags $tags
* @return Art
*/
public function addTag(\Main\ArtBundle\Entity\Tags $tags)
{
$this->tags[] = $tags;
return $this;
}
/**
* Remove tags
*
* @param \Main\ArtBundle\Entity\Tags $tags
*/
public function removeTag(\Main\ArtBundle\Entity\Tags $tags)
{
$this->tags->removeElement($tags);
}
/**
* Add visits
*
* @param \Main\ArtBundle\Entity\ArtVisit $visits
* @return Art
*/
public function addVisit(\Main\ArtBundle\Entity\ArtVisit $visits)
{
$this->visits[] = $visits;
return $this;
}
/**
* Remove visits
*
* @param \Main\ArtBundle\Entity\ArtVisit $visits
*/
public function removeVisit(\Main\ArtBundle\Entity\ArtVisit $visits)
{
$this->visits->removeElement($visits);
}
/**
* Get visits
*
* @return \Doctrine\Common\Collections\Collection
*/
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 \Main\StorageBundle\Entity\Image $image
* @return Art
*/
public function setImage(\Main\StorageBundle\Entity\Image $image = null)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return \Main\StorageBundle\Entity\Image
*/
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 \Main\LikeBundle\Entity\Thumb $thumbs
* @return Art
*/
public function setThumbs(Thumb $thumbs = null)
{
$this->thumbs = $thumbs;
return $this;
}
/**
* Get thumbs
*
* @return \Doctrine\Common\Collections\ArrayCollection $thumbs
*/
public function getThumbs()
{
return $this->thumbs;
}
/**
* Add thumb
*
* @param \Main\LikeBundle\Entity\Thumb $thumb
* @return $this $thumbs
*/
public function addThumb(Thumb $thumb)
{
$this->thumbs[] = $thumb;
return $this;
}
/**
* Remove thumbs
*
* @param \Main\LikeBundle\Entity\Thumb $thumbs
*/
public function removeThumb(\Main\LikeBundle\Entity\Thumb $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: ::Pagination\twitter_bootstrap_v3_pagination.html.twig # sliding pagination controls template
sortable: ::Pagination\sortable_link.html.twig # sort link template
我已将分页对象转储到控制器和模板中。每次params在那里(direction = asc或desc)这样的东西都在url中:
?sort=a.id&direction=desc&page=1
但如果我点击链接改变方向:没有任何改变!!
我相信knp分页有一个错误!或者我非常愚蠢;)
如果有人能帮助我,我会很开心!
问候迈克尔