我设置了一个测试,以便更熟悉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>
任何人都可以就此问题给我任何见解吗?
答案 0 :(得分:5)
第一个问题是缺少ORM关系声明;基本上在您的数据库中YourPet
无法与AnimalKind
相关联。
您可以找到有关Entity relationships and associations on the Symfony's website。
的文档从YourPet
实体的角度来看,您可能想要使用ManyToOne关系。
第二个问题,一旦解决了上述问题,就是您没有加入查询中的AnimalKind
实体。
第三个问题是您无法按实体订购查询结果。
首发潜在解决方案:
// In MyPet class
/**
* @var \Xyz\TestBundle\Entity\AnimalKind
* @ORM\OneToMany(targetEntity="AnimalKind", inversedBy="MyPets")
*/
private $AnimalKind;
// In AnimalKind class
/**
* @var \Doctrine\Common\Collections\Collection
* @ORM\ManyToOne(targetEntity="MyPet", inversedBy="AnimalKind")
*/
private $MyPets;
第二期潜在解决方案:
$query = $em->createQuery("
SELECT a, k
FROM XyzTestBundle:MyPet a
JOIN a.animalKind k
");
第三期潜在解决方案:
<th>{{ knp_pagination_sortable(pagination, 'kind', 'k.type') }}</th>
答案 1 :(得分:0)
要显示没有animalKind的动物,您必须在查询中使用LEFT JOIN
>>> df.loc['A'].query('col2 == 2')
col1 col2
ind2
1 2 2
2 1 2
>>> df.loc['A'].query('col2 == 2').index
EXPECTATION: Int64Index([0, 1, 2], dtype='int64', name='ind2')
REALITY: Int64Index([1, 2], dtype='int64', name='ind2')