KnpPaginator 不适用于连接表
KnpPaginator not works on joined tables
我正在尝试连接两个 table 并从 KnpPaginator 获取排序的结果集。这是我的代码。
查看
<table class="table table-bordered">
<tr>
<th>{{ knp_pagination_sortable(pagination, 'Id', 'c.id') }}</th>
<th{% if pagination.isSorted('c.name') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Name', 'c.name') }}</th>
<th>{{ knp_pagination_sortable(pagination, 'Version', 'c.model.name') }}</th>
</tr>
{% for article in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ article.id }}</td>
<td>{{ article.getName }}</td>
<td>{{ article.model.name }}</td>
</tr>
{% endfor %}
</table>
控制器
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT c FROM App\Entity\Car c";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query, /* query NOT result */ $request->query->getInt('page', 1)/* page number */, 10/* limit per page */
);
模型模型
/**
* @ORM\Entity(repositoryClass="App\Repository\ModelRepository")
*/
class Model
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
汽车模型
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CarRepository")
*/
class Car
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $code;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Model",cascade={"refresh","merge"}, inversedBy="cars")
* @ORM\JoinColumn(nullable=false)
*/
private $model;
public function getModel(): ?Model
{
return $this->model;
}
public /**
* @param $
*/function setModel(?Model $model): self
{
$this->model = $model;
return $this;
}
}
当我将 article.model.name 放入 table 体内时,它就起作用了。但是knp_pagination_sortable(pagination, 'Version', 'c.model.name')
只显示这个错误
There is no such field [model.name] in the given Query component,
aliased by [c]
我删除了一些不需要的代码以提高可见性。谢谢
终于找到解决办法了。
$em = $this->get('doctrine.orm.entity_manager');
$qb = $em->createQueryBuilder();
$query = $qb->select('c', 'm')
->from('App\Entity\Car', 'c')
->leftJoin('c.model', 'm')
->getQuery();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query, /* query NOT result */ $request->query->getInt('page', 1)/* page number */, 10/* limit per page */
);
但是你需要正确添加关系才能工作这里我已经添加了,
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Model",cascade={"refresh","merge"}, inversedBy="cars")
* @ORM\JoinColumn(nullable=false)
*/
private $model;
public function getModel(): ?Model
{
return $this->model;
}
public /**
* @param $
*/function setModel(?Model $model): self
{
$this->model = $model;
return $this;
}
我正在尝试连接两个 table 并从 KnpPaginator 获取排序的结果集。这是我的代码。
查看
<table class="table table-bordered">
<tr>
<th>{{ knp_pagination_sortable(pagination, 'Id', 'c.id') }}</th>
<th{% if pagination.isSorted('c.name') %} class="sorted"{% endif %}>{{ knp_pagination_sortable(pagination, 'Name', 'c.name') }}</th>
<th>{{ knp_pagination_sortable(pagination, 'Version', 'c.model.name') }}</th>
</tr>
{% for article in pagination %}
<tr {% if loop.index is odd %}class="color"{% endif %}>
<td>{{ article.id }}</td>
<td>{{ article.getName }}</td>
<td>{{ article.model.name }}</td>
</tr>
{% endfor %}
</table>
控制器
$em = $this->get('doctrine.orm.entity_manager');
$dql = "SELECT c FROM App\Entity\Car c";
$query = $em->createQuery($dql);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query, /* query NOT result */ $request->query->getInt('page', 1)/* page number */, 10/* limit per page */
);
模型模型
/**
* @ORM\Entity(repositoryClass="App\Repository\ModelRepository")
*/
class Model
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
汽车模型
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CarRepository")
*/
class Car
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $code;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Model",cascade={"refresh","merge"}, inversedBy="cars")
* @ORM\JoinColumn(nullable=false)
*/
private $model;
public function getModel(): ?Model
{
return $this->model;
}
public /**
* @param $
*/function setModel(?Model $model): self
{
$this->model = $model;
return $this;
}
}
当我将 article.model.name 放入 table 体内时,它就起作用了。但是knp_pagination_sortable(pagination, 'Version', 'c.model.name')
只显示这个错误
There is no such field [model.name] in the given Query component, aliased by [c]
我删除了一些不需要的代码以提高可见性。谢谢
终于找到解决办法了。
$em = $this->get('doctrine.orm.entity_manager');
$qb = $em->createQueryBuilder();
$query = $qb->select('c', 'm')
->from('App\Entity\Car', 'c')
->leftJoin('c.model', 'm')
->getQuery();
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$query, /* query NOT result */ $request->query->getInt('page', 1)/* page number */, 10/* limit per page */
);
但是你需要正确添加关系才能工作这里我已经添加了,
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Model",cascade={"refresh","merge"}, inversedBy="cars")
* @ORM\JoinColumn(nullable=false)
*/
private $model;
public function getModel(): ?Model
{
return $this->model;
}
public /**
* @param $
*/function setModel(?Model $model): self
{
$this->model = $model;
return $this;
}