我设置了一个测试来更熟悉 Symfony2 和 KnpPaginatorBundle。我有一张宠物表,其中引用了宠物动物类型(Dog
、、Cat
等)。我可以按id
,排序name
,但是当我尝试按动物排序时,type
我收到一条错误消息,指出:
给定的 Query 组件中没有这样的字段 [animalkind],别名为 [a]
我尝试了各种字段名称,但似乎没有任何效果。
实体: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>
谁能给我关于这个问题的任何见解?