我設置了一個測試來更熟悉Symfony2和KnpPaginatorBundle。我有一張寵物表,其中引用了寵物動物類型(Dog
,Cat
等)。我可以通過id
,name
排序,但是當我試圖通過動物type
排序我得到一個錯誤信息,指出:使用KnpPaginatorBundle對連接表進行排序
有一個在給定的查詢組件,通過別名沒有這樣的領域animalkind] [一]
我嘗試了各種字段名稱,但似乎沒有任何工作。
實體:MyPets.php
<?php
namespace Xyz\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* MyPet
*/
class MyPet
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* Set id
*
* @param integer $id
* @return MyPet
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return MyPet
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @var \Xyz\TestBundle\Entity\AnimalKind
*/
private $AnimalKind;
/**
* Set AnimalKind
*
* @param \Xyz\TestBundle\Entity\AnimalKind $animalKind
* @return MyPet
*/
public function setAnimalKind(\Xyz\TestBundle\Entity\AnimalKind $animalKind)
{
$this->AnimalKind = $animalKind;
return $this;
}
/**
* Get AnimalKind
*
* @return \Xyz\TestBundle\Entity\AnimalKind
*/
public function getAnimalKind()
{
return $this->AnimalKind;
}
}
實體:AnimalKind.php
<?php
namespace Xyz\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* AnimalKind
*/
class AnimalKind {
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $type;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set type
*
* @param string $type
* @return AnimalKind
*/
public function setType($type) {
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType() {
return $this->type;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $MyPets;
/**
* Constructor
*/
public function __construct() {
$this->MyPets = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add MyPets
*
* @param \Xyz\TestBundle\Entity\MyPet $myPets
* @return AnimalKind
*/
public function addMyPet(\Xyz\TestBundle\Entity\MyPet $myPets) {
$this->MyPets[] = $myPets;
return $this;
}
/**
* Remove MyPets
*
* @param \Xyz\TestBundle\Entity\MyPet $myPets
*/
public function removeMyPet(\Xyz\TestBundle\Entity\MyPet $myPets) {
$this->MyPets->removeElement($myPets);
}
/**
* Get MyPets
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMyPets() {
return $this->MyPets;
}
public function __toString() {
return $this->getType();
}
}
控制器:MyPetController.php
(IndexAction
)
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery("SELECT a FROM XyzTestBundle:MyPet a");
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($query,$this->get('request')->query->get('page',
1)/*page number*/,15/*limit per page*/);
return $this->render('XyzTestBundle:MyPet:index.html.twig', array(
'pagination' => $pagination,
));
}
檢視:MyPet/index.html.twig
(剪斷)
<table class="records_list">
<thead>
<tr>
{# sorting of properties based on query components #}
<th>{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}</th>
<th>{{ knp_pagination_sortable(pagination, 'Name', 'a.name') }}</th>
<th>{{ knp_pagination_sortable(pagination, 'kind', 'a.animalkind') }}</th>
</tr>
</thead>
<tbody>
{% for mypet in pagination %}
<tr>
<td><a href="{{ path('xyz_mypet_show', { 'id': mypet.id }) }}">{{ mypet.id }}</a></td>
<td>{{ mypet.name }}</td>
<td>{{ mypet.animalkind }}</td>
</tr>
{% endfor %}
</tbody>
</table>
任何人都可以給我任何洞察力的問題?
謝謝Elnur!我沒有更改實體,但是我按照你的建議添加了JOIN和k.type代碼,並且它工作正常。想到它後,我真的需要按動物類型來分類。 – KustomFx 2013-03-26 22:44:44