Bonsoir,
Actuellement, j'essaie d'apprendre a utiliser Symfony avec un projet simple : Un portfolio.
La, le sujet que j'essaie de travailler, ce sont les jointures. J'ai donc une page qui liste les compétences dans diverses catégories.
Donc, selon moi, le ManyToOne/OneToMany est la solution.
Voici mes entitées :
SkillsCategories :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | <?php namespace PortfolioBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; /** * SkillsCategories * * @ORM\Table() * @ORM\Entity(repositoryClass="PortfolioBundle\Entity\SkillsCategoriesRepository") */ class SkillsCategories { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="name", type="string", length=255) */ private $name; /** * @ORM\OneToMany(targetEntity="Skills", mappedBy="skillsCategories") */ protected $skills; public function __construct() { $this->skills = new ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name * @return SkillsCategories */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Add skills * * @param \PortfolioBundle\Entity\Skills $skills * @return SkillsCategories */ public function addSkill(\PortfolioBundle\Entity\Skills $skills) { $this->skills[] = $skills; return $this; } /** * Remove skills * * @param \PortfolioBundle\Entity\Skills $skills */ public function removeSkill(\PortfolioBundle\Entity\Skills $skills) { $this->skills->removeElement($skills); } /** * Get skills * * @return \Doctrine\Common\Collections\Collection */ public function getSkills() { return $this->skills; } /** * Get skill * * @return \Doctrine\Common\Collections\Collection */ public function getSkill() { return $this->skill; } } |
Skills
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | <?php namespace PortfolioBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Skills * * @ORM\Table() * @ORM\Entity(repositoryClass="PortfolioBundle\Entity\SkillsRepository") */ class Skills { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="name", type="string", length=255) */ private $skill; /** * @var integer * * @ORM\Column(name="progress", type="integer") */ private $progress; /** * @ORM\ManyToOne(targetEntity="skillsCategories", inversedBy="Skills") * @ORM\JoinColumn(name="category_id", referencedColumnName="id") */ protected $category; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set name * * @param string $name * @return Skills */ public function setName($name) { $this->name = $name; return $this; } /** * Get name * * @return string */ public function getName() { return $this->name; } /** * Set progress * * @param integer $progress * @return Skills */ public function setProgress($progress) { $this->progress = $progress; return $this; } /** * Get progress * * @return integer */ public function getProgress() { return $this->progress; } /** * Set category * * @param \PortfolioBundle\Entity\skillsCategories $category * @return Skills */ public function setCategory(\PortfolioBundle\Entity\skillsCategories $category = null) { $this->category = $category; return $this; } /** * Get category * * @return \PortfolioBundle\Entity\skillsCategories */ public function getCategory() { return $this->category; } /** * Set skill * * @param string $skill * @return Skills */ public function setSkill($skill) { $this->skill = $skill; return $this; } /** * Get skill * * @return string */ public function getSkill() { return $this->skill; } } |
Ensuite mon SkillsController:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?php namespace PortfolioBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class SkillsController extends Controller { public function indexAction() { $em = $this->getDoctrine() ->getManager(); $skillsCategories = $em->getRepository('PortfolioBundle:SkillsCategories') ->findAll(); $skills = $em->getRepository('PortfolioBundle:Skills') ->findAll(); return $this->render( 'PortfolioBundle:Skills:index.html.twig', array( 'categories' => $skillsCategories, 'skills' => $skills, ) ); } } |
et index.twig.html
1 2 3 4 5 6 7 8 9 10 11 | {% extends 'PortfolioBundle:Layout:base.html.twig' %} {% block body %} {% for category in categories %} <h3>{{ category.id }} - {{ category.name }}</h3> <table class="table"> {% for skill in category.skills %} {{ skill.skill }} {% endfor %} </table> {% endfor %} {% endblock %} |
Et voici maintenant mon erreur :
An exception has been thrown during the rendering of a template ("Notice: Undefined index: skillsCategories") in PortfolioBundle:Skills:index.html.twig at line 6.
Aurais-je oublié quelque chose ? Fait une erreur que je n'aurais pas vu ?
Je vous remercie par avance de votre aide.
J'ai trouvé la solution :
1 2 3 4 5 | /** * @ORM\ManyToOne(targetEntity="PortfolioBundle\Entity\SkillsCategories", inversedBy="skills") * @ORM\JoinColumn(name="category_id", referencedColumnName="id") */ protected $category; |
1 2 3 4 | /** * @ORM\OneToMany(targetEntity="PortfolioBundle\Entity\Skills", mappedBy="category") */ protected $skills; |
+0
-0