有2个类Item和Categoy,一个项目可以有更多的类别,一个类别可以有多个项目。下面是这些类的样子:
物品类别:
<?php
namespace m\r\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="tblItems")
*/
class Item {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=200)
*/
protected $title;
/**
* @ORM\ManyToMany(targetEntity="Category")
* @ORM\JoinTable(name="Item_Category",
* joinColumns={@ORM\JoinColumn(name="Item_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="Category_id", referencedColumnName="id")}
* )
*/
private $categories;
public function __construct() {
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
// getters and setters removed to keep post short
// Add categories removed
// Remove categories removed
}
?>
分类类:
<?php
namespace m\rBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="tblCategory")
*/
class Category {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=50)
*/
protected $CategoryName;
/**
* @ORM\ManyToMany(targetEntity="Item")
* @ORM\JoinTable(name="Item_Category",
* joinColumns={@ORM\JoinColumn(name="Category_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="Item_id", referencedColumnName="id")}
* )
*/
private $items;
public function __construct() {
$this->items = new \Doctrine\Common\Collections\ArrayCollection();
}
// bunch of getters and setters removed to keep post short(and addCategory)
}
?>
控制器:
public function indexAction() {
$dql = "select i, c FROM mrBundle:Item i join i.categories c";
$query = $this->getDoctrine()->getManager()->createQuery($dql)
->setFirstResult(0)
->setMaxResults(5);
$paginator = new Paginator($query, $fetchJoinCollection = true);
var_dump(count($paginator));// shows 2143 no matter what setMaxResults is
return "Doest really work";
}
[更新]
在发布这个问题时,我又尝试了一件似乎有帮助的事情:
var_dump(count($paginator->getIterator()));
这在 setMaxResults 设置为 5 时给出 5,当 setMaxResults 设置为 500 时给出 500。必须检查究竟返回了什么,以及在显示它们时是否可以访问每个项目的类别。试图在分页器对象上执行 var_dump 以进行检查,但如果我没有足够快地 xkill 它,它会使 Firefox 崩溃并导致 Fedora 崩溃。