I installed the test to get to know Symfony2 and KnpPaginatorBundle better. I have a pet table that refers to the type of pet ( Dog , Cat , Etc). I can sort by id , name , but when I try to sort by animal type , I get an error:
The specified Query component does not have such a [animalkind] field superimposed by the symbol [a]
I tried different field names, but nothing works.
Entity: MyPets.php
<?php namespace Xyz\TestBundle\Entity; use Doctrine\ORM\Mapping as ORM; class MyPet { private $id; private $name; public function setId($id) { $this->id = $id; return $this; } public function getId() { return $this->id; } public function setName($name) { $this->name = $name; return $this; } public function getName() { return $this->name; } private $AnimalKind; public function setAnimalKind(\Xyz\TestBundle\Entity\AnimalKind $animalKind) { $this->AnimalKind = $animalKind; return $this; } public function getAnimalKind() { return $this->AnimalKind; } }
Essence: AnimalKind.php
<?php namespace Xyz\TestBundle\Entity; use Doctrine\ORM\Mapping as ORM; class AnimalKind { private $id; private $type; public function getId() { return $this->id; } public function setType($type) { $this->type = $type; return $this; } public function getType() { return $this->type; } private $MyPets; public function __construct() { $this->MyPets = new \Doctrine\Common\Collections\ArrayCollection(); } public function addMyPet(\Xyz\TestBundle\Entity\MyPet $myPets) { $this->MyPets[] = $myPets; return $this; } public function removeMyPet(\Xyz\TestBundle\Entity\MyPet $myPets) { $this->MyPets->removeElement($myPets); } public function getMyPets() { return $this->MyPets; } public function __toString() { return $this->getType(); } }
Controller: 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),15); return $this->render('XyzTestBundle:MyPet:index.html.twig', array( 'pagination' => $pagination, )); }
View: MyPet/index.html.twig (cut off)
<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>
Can someone give me an idea about the problem?