Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF2][SF]Relacja 1 do wielu
Forum PHP.pl > Forum > PHP > Frameworki
Szymciosek
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.
Crozin
1. Nie korzystaj z print_r() przy wyświetlaniu tylko z var_dump() połączonym z xdebugiem, a najlepiej od razu naucz się korzystać z jakiegoś debuggera.
2. Obiekty kontynentów i państw posiadają wzajemne referencje do siebie, a print_r() nie jest wstanie ich wykryć stąd dostajesz błąd o przekroczeniu limitu pamięci, wielkości stosu czy czasu wykonywania skryptu.
Szymciosek
Możesz mi jeszcze powiedzieć czemu mam ten błąd:

FatalErrorException: Error: Call to a member function getCategoryName() on a non-object in /var/www/k.pl/website_v.1.0/src/K/GalleryBundle/Controller/GalleryController.php line 50

Controller
  1. $manager = $this->getDoctrine()->getManager();
  2.  
  3. $fileCategories = $manager
  4. ->getRepository('KSiteBundle:FileCategory')
  5. ->findAll();
  6.  
  7. $userfiles = $fileCategories->getCategoryName();
  8. foreach ($userfiles as $file)
  9. {
  10. echo $file->getFileUrl();
  11. }


FileCategory - entity
  1. <?php
  2.  
  3. namespace K\SiteBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6.  
  7. /**
  8.  * FileCategory
  9.  *
  10.  * @ORM\Table(name="file_categories")
  11.  * @ORM\Entity
  12.  */
  13. class FileCategory
  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="category_name", type="string", length=255)
  28.   */
  29. private $categoryName;
  30.  
  31. /**
  32.   * @ORM\OneToMany(targetEntity="Userfile", mappedBy="fileCategory")
  33.   */
  34. protected $userfiles;
  35.  
  36.  
  37.  
  38. /**
  39.   * Constructor
  40.   */
  41. public function __construct()
  42. {
  43. $this->userfiles = new \Doctrine\Common\Collections\ArrayCollection();
  44. }
  45.  
  46. /**
  47.   * Get id
  48.   *
  49.   * @return integer
  50.   */
  51. public function getId()
  52. {
  53. return $this->id;
  54. }
  55.  
  56. /**
  57.   * Set categoryName
  58.   *
  59.   * @param string $categoryName
  60.   * @return FileCategory
  61.   */
  62. public function setCategoryName($categoryName)
  63. {
  64. $this->categoryName = $categoryName;
  65.  
  66. return $this;
  67. }
  68.  
  69. /**
  70.   * Get categoryName
  71.   *
  72.   * @return string
  73.   */
  74. public function getCategoryName()
  75. {
  76. return $this->categoryName;
  77. }
  78.  
  79. /**
  80.   * Add userfiles
  81.   *
  82.   * @param \K\SiteBundle\Entity\Userfile $userfiles
  83.   * @return FileCategory
  84.   */
  85. public function addUserfile(\K\SiteBundle\Entity\Userfile $userfiles)
  86. {
  87. $this->userfiles[] = $userfiles;
  88.  
  89. return $this;
  90. }
  91.  
  92. /**
  93.   * Remove userfiles
  94.   *
  95.   * @param \K\SiteBundle\Entity\Userfile $userfiles
  96.   */
  97. public function removeUserfile(\K\SiteBundle\Entity\Userfile $userfiles)
  98. {
  99. $this->userfiles->removeElement($userfiles);
  100. }
  101.  
  102. /**
  103.   * Get userfiles
  104.   *
  105.   * @return \Doctrine\Common\Collections\Collection
  106.   */
  107. public function getUserfiles()
  108. {
  109. return $this->userfiles;
  110. }
  111. }


Userfile - entity
  1. <?php
  2.  
  3. namespace K\SiteBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6.  
  7. /**
  8.  * Userfile
  9.  *
  10.  * @ORM\Table(name="userfiles")
  11.  * @ORM\Entity
  12.  */
  13. class Userfile
  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="file_url", type="string", length=255)
  28.   */
  29. private $fileUrl;
  30.  
  31. /**
  32.   * @var integer
  33.   *
  34.   * @ORM\Column(name="cat_id", type="integer")
  35.   */
  36. private $catId;
  37.  
  38. /**
  39.   * @ORM\ManyToOne(targetEntity="FileCategory", inversedBy="userfile")
  40.   */
  41. protected $fileCategory;
  42.  
  43.  
  44.  
  45.  
  46. /**
  47.   * Get id
  48.   *
  49.   * @return integer
  50.   */
  51. public function getId()
  52. {
  53. return $this->id;
  54. }
  55.  
  56. /**
  57.   * Set fileUrl
  58.   *
  59.   * @param string $fileUrl
  60.   * @return Userfile
  61.   */
  62. public function setFileUrl($fileUrl)
  63. {
  64. $this->fileUrl = $fileUrl;
  65.  
  66. return $this;
  67. }
  68.  
  69. /**
  70.   * Get fileUrl
  71.   *
  72.   * @return string
  73.   */
  74. public function getFileUrl()
  75. {
  76. return $this->fileUrl;
  77. }
  78.  
  79. /**
  80.   * Set catId
  81.   *
  82.   * @param integer $catId
  83.   * @return Userfile
  84.   */
  85. public function setCatId($catId)
  86. {
  87. $this->catId = $catId;
  88.  
  89. return $this;
  90. }
  91.  
  92. /**
  93.   * Get catId
  94.   *
  95.   * @return integer
  96.   */
  97. public function getCatId()
  98. {
  99. return $this->catId;
  100. }
  101.  
  102. /**
  103.   * Set fileCategory
  104.   *
  105.   * @param \K\SiteBundle\Entity\FileCategory $fileCategory
  106.   * @return Userfile
  107.   */
  108. public function setFileCategory(\K\SiteBundle\Entity\FileCategory $fileCategory = null)
  109. {
  110. $this->fileCategory = $fileCategory;
  111.  
  112. return $this;
  113. }
  114.  
  115. /**
  116.   * Get fileCategory
  117.   *
  118.   * @return \K\SiteBundle\Entity\FileCategory
  119.   */
  120. public function getFileCategory()
  121. {
  122. return $this->fileCategory;
  123. }
  124. }


Wydaje mi się, że jest analogicznie do poprzedniego przykładu zrobione.
destroyerr
Pewnie dlatego, że funkcja findAll zwraca tablicę a Ty operujesz jak na obiekcie.

Kolejny bład:
  1. $userfiles = $fileCategories->getCategoryName();
  2. foreach ($userfiles as $file)

Funkcja getCategoryName zwraca string, więc dlaczego próbujesz po tym stringu iterować?
Szymciosek
Tutaj akurat próbowałem sprawdzić inne metody - czy są widoczne.

Już powoli dochodzę do tego wszystkiego, już jakieś wyniki mi się pokazują.

No i oczywiście wyszło na to, że była niezgodność pól pomiędzy bazą a entity, ale prawie dobrze smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.