Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF2][SF]Relacja 1 do wielu
Szymciosek
post
Post #1





Grupa: Zarejestrowani
Postów: 1 168
Pomógł: 126
Dołączył: 5.02.2010
Skąd: Świdnica

Ostrzeżenie: (0%)
-----


Witam,

zrobiłem przykład z książki W. Gajdy dotyczący relacji 1:n na przykładzie kontynentów i państw

  1. <?php
  2.  
  3. namespace Szymek\RelationsBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Szymek\RelationsBundle\Entity\Kontynent;
  7. use Szymek\RelationsBundle\Entity\Panstwo;
  8.  
  9. class DefaultController extends Controller
  10. {
  11. public function indexAction()
  12. {
  13. $manager = $this->getDoctrine()->getManager();
  14.  
  15. // dodawanie danych do bazy za pomoca relacji 1:m
  16. //
  17. // $kontynent = new Kontynent();
  18. // $kontynent->setNazwa('Inny');
  19. // $manager->persist($kontynent);
  20. //
  21. // $panstwo = new Panstwo();
  22. // $panstwo->setNazwa('Inny kraj');
  23. // $panstwo->setKontynent($kontynent);
  24. // $manager->persist($panstwo);
  25. //
  26. // $manager->flush();
  27.  
  28. // sciaganie danych z bazy za pomoca relacji 1:m
  29. $kontynent = $manager
  30. ->getRepository('SzymekRelationsBundle:Kontynent')
  31. ->findOneByNazwa('Europa');
  32.  
  33. $panstwa = $kontynent->getPanstwa();
  34. foreach ($panstwa as $panstwo)
  35. {
  36. print_r('<pre>');
  37. print_r($panstwo);
  38. print_r('</pre>');
  39. }
  40.  
  41. return array();
  42. }
  43. }
  44.  


  1. <?php
  2.  
  3. namespace Szymek\RelationsBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6.  
  7. /**
  8.  * Panstwo
  9.  *
  10.  * @ORM\Table()
  11.  * @ORM\Entity
  12.  */
  13. class Panstwo
  14. {
  15. /**
  16.   * @var integer
  17.   *
  18.   * @ORM\Column(name="id", type="integer")
  19.   * @ORM\Id
  20.   * @ORM\GeneratedValue(strategy="AUTO")
  21.   */
  22. private $id;
  23.  
  24. /**
  25.   * @var string
  26.   *
  27.   * @ORM\Column(name="nazwa", type="string", length=255)
  28.   */
  29. private $nazwa;
  30.  
  31. /**
  32.   * @var integer
  33.   *
  34.   * @ORM\Column(name="kontynent_id", type="integer")
  35.   */
  36. private $kontynentId;
  37.  
  38. /**
  39.   * @ORM\ManyToOne(targetEntity="Kontynent", inversedBy="panstwa")
  40.   */
  41. protected $kontynent;
  42.  
  43.  
  44. /**
  45.   * Get id
  46.   *
  47.   * @return integer
  48.   */
  49. public function getId()
  50. {
  51. return $this->id;
  52. }
  53.  
  54. /**
  55.   * Set nazwa
  56.   *
  57.   * @param string $nazwa
  58.   * @return Panstwo
  59.   */
  60. public function setNazwa($nazwa)
  61. {
  62. $this->nazwa = $nazwa;
  63.  
  64. return $this;
  65. }
  66.  
  67. /**
  68.   * Get nazwa
  69.   *
  70.   * @return string
  71.   */
  72. public function getNazwa()
  73. {
  74. return $this->nazwa;
  75. }
  76.  
  77. /**
  78.   * Set kontynentId
  79.   *
  80.   * @param integer $kontynentId
  81.   * @return Panstwo
  82.   */
  83. public function setKontynentId($kontynentId)
  84. {
  85. $this->kontynentId = $kontynentId;
  86.  
  87. return $this;
  88. }
  89.  
  90. /**
  91.   * Get kontynentId
  92.   *
  93.   * @return integer
  94.   */
  95. public function getKontynentId()
  96. {
  97. return $this->kontynentId;
  98. }
  99.  
  100. /**
  101.   * Set kontynent
  102.   *
  103.   * @param \Szymek\RelationsBundle\Entity\Kontynent $kontynent
  104.   * @return Panstwo
  105.   */
  106. public function setKontynent(\Szymek\RelationsBundle\Entity\Kontynent $kontynent = null)
  107. {
  108. $this->kontynent = $kontynent;
  109.  
  110. return $this;
  111. }
  112.  
  113. /**
  114.   * Get kontynent
  115.   *
  116.   * @return \Szymek\RelationsBundle\Entity\Kontynent
  117.   */
  118. public function getKontynent()
  119. {
  120. return $this->kontynent;
  121. }
  122. }


  1. <?php
  2.  
  3. namespace Szymek\RelationsBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6.  
  7. /**
  8.  * Kontynent
  9.  *
  10.  * @ORM\Table()
  11.  * @ORM\Entity
  12.  */
  13. class Kontynent
  14. {
  15. /**
  16.   * @var integer
  17.   *
  18.   * @ORM\Column(name="id", type="integer")
  19.   * @ORM\Id
  20.   * @ORM\GeneratedValue(strategy="AUTO")
  21.   */
  22. private $id;
  23.  
  24. /**
  25.   * @var string
  26.   *
  27.   * @ORM\Column(name="nazwa", type="string", length=255)
  28.   */
  29. private $nazwa;
  30.  
  31. /**
  32.   * @ORM\OneToMany(targetEntity="Panstwo", mappedBy="kontynent")
  33.   */
  34. protected $panstwa;
  35.  
  36.  
  37. /**
  38.   * Get id
  39.   *
  40.   * @return integer
  41.   */
  42. public function getId()
  43. {
  44. return $this->id;
  45. }
  46.  
  47. /**
  48.   * Set nazwa
  49.   *
  50.   * @param string $nazwa
  51.   * @return Kontynent
  52.   */
  53. public function setNazwa($nazwa)
  54. {
  55. $this->nazwa = $nazwa;
  56.  
  57. return $this;
  58. }
  59.  
  60. /**
  61.   * Get nazwa
  62.   *
  63.   * @return string
  64.   */
  65. public function getNazwa()
  66. {
  67. return $this->nazwa;
  68. }
  69. /**
  70.   * Constructor
  71.   */
  72. public function __construct()
  73. {
  74. $this->panstwa = new \Doctrine\Common\Collections\ArrayCollection();
  75. }
  76.  
  77. /**
  78.   * Add panstwa
  79.   *
  80.   * @param \Szymek\RelationsBundle\Entity\Panstwo $panstwa
  81.   * @return Kontynent
  82.   */
  83. public function addPanstwa(\Szymek\RelationsBundle\Entity\Panstwo $panstwa)
  84. {
  85. $this->panstwa[] = $panstwa;
  86.  
  87. return $this;
  88. }
  89.  
  90. /**
  91.   * Remove panstwa
  92.   *
  93.   * @param \Szymek\RelationsBundle\Entity\Panstwo $panstwa
  94.   */
  95. public function removePanstwa(\Szymek\RelationsBundle\Entity\Panstwo $panstwa)
  96. {
  97. $this->panstwa->removeElement($panstwa);
  98. }
  99.  
  100. /**
  101.   * Get panstwa
  102.   *
  103.   * @return \Doctrine\Common\Collections\Collection
  104.   */
  105. public function getPanstwa()
  106. {
  107. return $this->panstwa;
  108. }
  109. }


Lecz niestety nie wiedzieć czemu... zapętla się on.

Jak sobie z tym poradzić? Coś źle robię?

Czyżby samo państwo w pętli for było nadal obiektem i to przez niego się wszystko zapętla?

Jak zrobię:

echo $panstwo->getNazwa();
to wyświetla dobrze.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 18:50