我认为问题在于您的实体,即您定义实体之间关系的方式。你需要使用双向映射http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-many-bidirectional
我更新了您的实体以满足您的需求。
- 为
ForumSection 实体添加了类别
- 为
ForumSection实体添加了addCategory()、removeCategory()、getCategories()方法
- 为
ForumCategory实体添加inversedBy注解以完成关系
- 我使用 getCategories() 方法来获取 twig 中该部分的所有类别。
实体;
论坛部分.php
namespace AppBundle\Entity;
/**
* @version 0.0.1
* @copyright (c) 2015 Ricardo Jacobs. All rights reserved.
* @license Proprietary and confidential source code.
*/
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repositories\Forum")
* @ORM\HasLifecycleCallbacks
* @ORM\Table(name="forum_sections")
*/
class ForumSection
{
/**
* @ORM\Column(type="integer", name="id")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, name="title")
*/
private $title;
/**
* @ORM\Column(type="integer", name="position")
*/
private $position;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\OneToMany(targetEntity="AppBundle\Entity\ForumCategory", mappedBy="section")
*/
private $categories;
public function __construct() {
$this->categories = new ArrayCollection();
}
public function getCategories() {
return $this->categories;
}
/**
* Add category
*
* @param AppBundle\Entity\ForumCategory
* @return ForumSection
*/
public function addCategory(\AppBundle\Entity\ForumCategory $category)
{
$this->categories[] = $category;
return $this;
}
/**
* Remove category
*
* @param AppBundle\Entity\ForumCategory $category
*/
public function removeCategory(\AppBundle\Entity\ForumCategory $category)
{
$this->categories->removeElement($category);
}
/**
* @ORM\PrePersist
* @ORM\PreUpdate
*/
public function updatedTimestamps() {
if ($this->getCreatedAt() == null) {
$this->setCreatedAt(new \DateTime('NOW'));
}
}
/**
* @return mixed
*/
public function getId() {
return $this->id;
}
/**
* @return mixed
*/
public function getTitle() {
return $this->title;
}
/**
* @param $title
* @return $this
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
/**
* @return mixed
*/
public function getPosition() {
return $this->position;
}
/**
* @param $position
* @return $this
*/
public function setPosition($position) {
$this->position = $position;
return $this;
}
/**
* @return Date
*/
public function getCreatedAt() {
return $this->created_at;
}
/**
* @param $date
*/
public function setCreatedAt($date) {
$this->created_at = $date;
return $this;
}
}
论坛分类.php
<?php
namespace AppBundle\Entity;
/**
* @version 0.0.1
* @copyright (c) 2015 Ricardo Jacobs. All rights reserved.
* @license Proprietary and confidential source code.
*/
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="AppBundle\Entity\Repositories\Forum")
* @ORM\Table(name="forum_categories")
*/
class ForumCategory
{
/**
* @ORM\Column(type="integer", name="id")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\ForumSection", inversedBy="categories")
* @ORM\JoinColumn(name="section", referencedColumnName="id")
*/
private $section;
/**
* @ORM\Column(type="integer", name="position")
*/
private $position;
/**
* @ORM\Column(type="string", length=255, name="title")
*/
private $title;
/**
* @ORM\Column(type="string", length=255, nullable=true, name="description")
*/
private $description;
/**
* @return mixed
*/
public function getId() {
return $this->id;
}
/**
* @return mixed
*/
public function getSection() {
return $this->section;
}
/**
* @param $section
* @return $this
*/
public function setSection($section) {
$this->section = $section;
return $this;
}
/**
* @return mixed
*/
public function getPosition() {
return $this->position;
}
/**
* @param $position
* @return $this
*/
public function setPosition($position) {
$this->position = $position;
return $this;
}
/**
* @return mixed
*/
public function getTitle() {
return $this->title;
}
/**
* @param $title
* @return $this
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
/**
* @return mixed
*/
public function getDescription() {
return $this->description;
}
/**
* @param $description
* @return $this
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
}
控制器;
public function indexAction()
{
$em = $this->get('doctrine')->getManager();
$sections = $sections = $this->getDoctrine() ->getRepository('AppBundle:ForumSection') ->findAll();
return $this->render('AppBundle:Default:index.html.twig', array('sections'=>$sections));
}
模板;
<ol>
{% for forum in sections %}
<li>
<h2>{{forum.title}} </h2>
<ul>
{% for category in forum.getCategories() %}
<li>{{category.title}}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ol>
现在您可以访问与数据库中每个 forum_section 行相关的类别。
这是我运行代码后的输出
-
你好
-
世界
不得不提一下,Doctrine 默认使用延迟加载。Doctrine 会执行额外的查询来查找与论坛相关的类别。如果您有n 论坛,您将有n + 1 查询。就我而言,我使用了 2 个论坛部分,这里是查询。
这个找到所有的forum_sections;
SELECT
t0.id AS id1,
t0.title AS title2,
t0.position AS position3,
t0.created_at AS created_at4
FROM
forum_sections t0
然后对于每个论坛,doctual 执行另一个查询以查找具有不同参数的类别。
在您调用 'forum.getCategories()` 方法之前,不会执行此查询。
SELECT
t0.id AS id1,
t0.position AS position2,
t0.title AS title3,
t0.description AS description4,
t0.section AS section5
FROM
forum_categories t0
WHERE
t0.section = ? [Parameters: 1,2]
查看 fetch-join 概念以了解有关延迟加载和替代方案的更多信息。
http://blog.bemycto.com/good-practices/2015-05-31/understanding-doctrine-orm-lazy-load-fetch-join/