我有两张桌子 - 类别和主题。以下是这些结构:
/** * ForumCategories * @ORM\Table(name="forum_categories") * @ORM\Entity */ class ForumCategories { /** * @ORM\OneToMany(targetEntity="ForumTopics", mappedBy="category") */ protected $topics; public function __construct() { $this->topics = new ArrayCollection(); $this->children = new ArrayCollection(); } /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string * * @ORM\Column(name="name", type="string", length=255) */ private $name; /** * @var boolean * * @ORM\Column(name="top_menu", type="boolean") */ private $topMenu; /** * @var integer * * @ORM\Column(name="ord", type="integer", type="decimal", options={"default"="0"}) */ private $ord; /** * @var string * * @ORM\Column(name="color_class", type="string", length=255, nullable=true) */ private $colorClass; /** * @ORM\OneToMany(targetEntity="ForumCategories", mappedBy="parent") **/ private $children; /** * @ORM\ManyToOne(targetEntity="ForumCategories", inversedBy="children") * @ORM\JoinColumn(name="parent", referencedColumnName="id") **/ private $parent; private $parentMenu; private $parentCats; /** * Set name * * @param string $name * @return ForumCategories */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set topMenu * * @param boolean $topMenu * @return ForumCategories */ public function setTopMenu($topMenu) { $this->topMenu = $topMenu; return $this; } /** * Get topMenu * * @return boolean */ public function getTopMenu() { return $this->topMenu; } /** * Set ord * * @param integer $ord * @return ForumCategories */ public function setOrd($ord) { $this->ord = $ord; return $this; } /** * Get ord * * @return integer */ public function getOrd() { return $this->ord; } /** * Set colorClass * * @param string $colorClass * @return ForumCategories */ public function setColorClass($colorClass) { $this->colorClass = $colorClass; return $this; } /** * Get colorClass * * @return string */ public function getColorClass() { return $this->colorClass; } /** * Set parent * * @param string $parent * @return ForumCategories */ public function setParent($parent) { $this->parent = $parent; return $this; } /** * Get parent * * @return string */ public function getParent() { return $this->parent; } /** * @return ArrayCollection[] */ public function getChildren() { return $this->children; } /** * @return array[int] */ public function getChildrenId() { $result = []; foreach ($this->getChildren() as $child) { $result[] = $child->getId(); } return $result; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Add topics * * @param \Test\ForumBundle\Entity\ForumTopics $topics * @return ForumCategories */ public function addTopic(\Test\ForumBundle\Entity\ForumTopics $topics) { $this->topics[] = $topics; return $this; } /** * Remove topics * * @param \Test\ForumBundle\Entity\ForumTopics $topics */ public function removeTopic(\Test\ForumBundle\Entity\ForumTopics $topics) { $this->topics->removeElement($topics); } /** * Get topics * * @return \Doctrine\Common\Collections\Collection */ public function getTopics() { return $this->topics; }
主题:
/** * ForumCategories * @ORM\Table(name="forum_topics") * @ORM\Entity */ class ForumTopics { /** * @ORM\ManyToOne(targetEntity="ForumCategories", inversedBy="forum_topics") * @ORM\JoinColumn(name="cat_id", referencedColumnName="id") */ private $cat_id; /** * @ORM\ManyToOne(targetEntity="ForumCategories", inversedBy="forum_topics") * @ORM\JoinColumn(name="cat_id", referencedColumnName="id", nullable=true) */ private $category; /** * @var integer * * @ORM\Column(name="user_id", type="integer", type="decimal", options={"default"="0"}) */ private $userId; /** * @ORM\ManyToOne(targetEntity="User", inversedBy="forum_topics") * @ORM\JoinColumn(name="user_id", referencedColumnName="id") */ protected $userName; /** * @ORM\OneToMany(targetEntity="ForumPosts", mappedBy="topic_id") */ public $topic_id; protected $posts; public function __construct() { //parent::__construct(); $this->posts = new ArrayCollection(); } /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var integer * * @ORM\Column(name="cat_id", type="integer", type="decimal", options={"default"="0"}) */ private $catId; /** * @var string * * @ORM\Column(name="name", type="string", length=255) */ private $name; /** * @var \DateTime * * * @ORM\Column(name="cdate", type="datetime") */ private $cdate; /** * @var \DateTime * * * @ORM\Column(name="lastpost", type="datetime") */ private $lastpost; /** * Set catId * * @param integer $catId * @return ForumTopics */ public function setCatId($catId) { $this->catId = $catId; return $this; } /** * Get catId * * @return integer */ public function getCatId() { return $this->catId; } /** * Set name * * @param string $name * @return ForumTopics */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set userId * * @param integer $userId * @return User */ public function setUserId($userId) { $this->userId = $userId; return $this; } /** * Get userId * * @return integer */ public function getUserId() { return $this->userId; } /** * Set cdate * * @param \DateTime $cdate * @return ForumTopics */ public function setCdate($cdate) { $this->cdate = $cdate; return $this; } /** * Get cdate * * @return \DateTime */ public function getCdate() { return $this->cdate; } /** * Set lastpost * * @param \DateTime $lastpost * @return ForumTopics */ public function setLastpost($lastpost) { $this->lastpost = $lastpost; return $this; } /** * Get lastpost * * @return \DateTime */ public function getLastpost() { return $this->lastpost; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set category * * @param \Test\ForumBundle\Entity\ForumCategories $category * @return ForumTopics */ public function setCategory(\Test\ForumBundle\Entity\ForumCategories $category = null) { $this->category = $category; return $this; } /** * Get catId * * @return \Test\ForumBundle\Entity\ForumCategories */ public function getCategory() { return $this->catId; } /** * Add posts * * @param \Test\ForumBundle\Entity\ForumPosts $posts * @return ForumTopics */ public function addPost(\Test\ForumBundle\Entity\ForumPosts $posts) { $this->posts[] = $posts; return $this; } /** * Remove posts * * @param \Test\ForumBundle\Entity\ForumPosts $posts */ public function removePost(\Test\ForumBundle\Entity\ForumPosts $posts) { $this->posts->removeElement($posts); } /** * Get posts * * @return \Doctrine\Common\Collections\Collection */ public function getPosts() { return $this->posts; } /** * Set userName * * @param \Test\ForumBundle\Entity\User $userName * @return ForumTopics */ public function setUserName(\Test\ForumBundle\Entity\User $userName = null) { $this->userName = $userName; return $this; } /** * Get userName * * @return \Test\ForumBundle\Entity\User */ public function getUserName() { return $this->userName; } /** * Add topic_id * * @param \Test\ForumBundle\Entity\ForumPosts $topicId * @return ForumTopics */ public function addTopicId(\Test\ForumBundle\Entity\ForumPosts $topicId) { $this->topic_id[] = $topicId; return $this; } /** * Remove topic_id * * @param \Test\ForumBundle\Entity\ForumPosts $topicId */ public function removeTopicId(\Test\ForumBundle\Entity\ForumPosts $topicId) { $this->topic_id->removeElement($topicId); } /** * Get topic_id * * @return \Doctrine\Common\Collections\Collection */ public function getTopicId() { return $this->topic_id; } }
现在我只想采用参数top_menu = 1的类别。但我所做的一切都是给我带有子类别的类别,所有主题都分配给每个类别,所有帖子都分配给每个主题..很多数据。我只想要那些类别名称。继承我的控制器功能:
/** * Lists only top categories * * @Route("/top.{_format}", defaults={"_format"="html"}) * @Method("GET") * @ApiDoc() */ public function listTopAction() { $em = $this->getDoctrine()->getManager(); $entities = $em->getRepository('TestForumBundle:ForumCategories')->findBy( array('parent' => NULL, 'topMenu' => '1') ); return $entities; } /** * Lists only top categories WDQL * * @Route("/tops.{_format}", defaults={"_format"="html"}) * @Method("GET") * @ApiDoc() */ public function listTopDQLAction() { $em = $this->getDoctrine()->getManager(); $query = $em->getRepository("TestForumBundle:ForumCategories")->createQueryBuilder('c') ->where('c.topMenu = 1') ->getQuery(); $results = $query->getResult(); return $results; }
这是一种只获取那些数据的方法我不需要所有其他表中的所有数据都是按键组合的吗?谢谢你的帮助!
答案 0 :(得分:2)
我能理解的你可能想要这样的东西
Symfony2 Select one column in doctrine
但我无法真正了解您想要获得的确切数据结构,以及您所指的控制器中的哪个查询。
这是一种只获取那些数据的方法我不需要所有其他表中的所有数据都是按键组合的吗?
也不明白,因为这些查询会返回带有属性的对象,而不是带有键的数组。此外,默认情况下,doctrine不会仅获取单行的所有数据,只有当您访问方法时才会向db请求更多数据。
答案 1 :(得分:0)
在这种情况下,我要做的是在 ForumCategories 的存储库中创建一个特定的方法,您将在其中返回所需类别的名称。因此,您将必须在包的实体/存储库文件夹中创建一个名为 ForumCategoriesRepository 的文件。
然后,您必须以这种方式告诉ForumCategories它具有一个存储库:
@ORM\Entity(repositoryClass="{YourNamespacePath}\Entity\Repository\ForumCategoriesRepository")
现在您可以创建这样的方法:
public function getCitiesNames($stateId) {
$cities = $this->_em->createQueryBuilder()->select('city.name')
->from('\MSD\CommonBundle\Entity\City', 'city')
->where('city.state = :id')
->orderBy('city.name', 'ASC')
->setParameter('id', $stateId)
->getQuery()
->getResult();
return $cities;
}
然后在您的控制器上调用它:
$cities = $em->getRepository('MSDCommonBundle:City')
->getCitiesNames($stateId);