Skip to content

Instantly share code, notes, and snippets.

@Chrysweel
Last active December 20, 2015 17:59
Show Gist options
  • Save Chrysweel/6172811 to your computer and use it in GitHub Desktop.
Save Chrysweel/6172811 to your computer and use it in GitHub Desktop.
Entities: Section & Category with table intermediare. Relation ManyToOne - OneToMany. How can I get the categories of a section ?
Class Intermediare: CategorySection
<?php
/**
* @ORM\Entity(repositoryClass="xxx\SectionBundle\Repositories\GiftCategorySectionRepositoryDoctrine")
* @ORM\Table(name="gift_category_section")
*/
class GiftCategorySection
{
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="xxx\SectionBundle\Entity\GiftCategory", inversedBy="categories")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
*/
private $category;
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="xxx\SectionBundle\Entity\GiftSection", inversedBy="sections")
* @ORM\JoinColumn(name="section_id", referencedColumnName="id")
*/
private $section;
/**
*
* @ORM\Column(type="string")
*/
private $title;
[...]
?>
Class Category:
<?php
/**
* @ORM\Entity(repositoryClass="xxx\SectionBundle\Repositories\GiftCategoryRepositoryDoctrine")
* @ORM\Table(name="gift_category")
*/
class GiftCategory
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="xxx\SectionBundle\Entity\GiftCategorySection", mappedBy="section")
**/
protected $sections;
[...]
?>
Class Section:
<?php
/**
* @ORM\Entity(repositoryClass="xxx\SectionBundle\Repositories\GiftSectionRepositoryDoctrine")
* @ORM\Table(name="gift_section")
*/
class GiftSection
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToMany(targetEntity="xxx\SectionBundle\Entity\GiftCategorySection", mappedBy="section")
**/
protected $sections;
[...]
?>
Category
+----+--------------------------+
| id | title |
+----+--------------------------+
| 1 | Love |
| 2 | Friendly |
+----+--------------------------+
Section
+----+----------------------+
| id | title |
+----+----------------------+
| 1 | Joke |
| 2 | Postal |
| 3 | History |
+----+----------------------+
CategorySection
+----+-------------+------------+-----------------------+
| id | category_id | section_id | title |
+----+-------------+------------+-----------------------+
| 1 | 1 | 2 | joke of love |
| 2 | 1 | 3 | all histories of love |
+----+-------------+------------+-----------------------+
Now the problem is... I want get the Categories of a Section, but how can I to do ?
Perhaps the relationship is bad ...
i.e I have a section with id=2 I want to know which categories there are to this section.
The query should get category 1...
Thanks in advance : )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment