Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony]Problem self-referncing
Forum PHP.pl > Forum > PHP > Frameworki
Marys91
Witam,
mam problem z głupimy kategoriami, a mianowicie stworzyłem sobie już wszystko i teraz próbuje ustawić relacje tak, żeby wstawiać podkategorię. Jak ustawiłem sobie relacje i zrobiłem schema update to znikła mi jedna kolumna. Szukam Szukam i nic nie mogę znaleźć.
Dobra to może jeszcze powiem dokładnie co chce osiągnąć.
Chce zrobić sobie taką tabelkę:
id | name | parent_id

i mam już taką klasę w entity:
  1. class Categories
  2. {
  3. /**
  4.   * @var integer $id
  5.   *
  6.   * @ORM\Column(name="id", type="integer")
  7.   * @ORM\Id
  8.   * @ORM\GeneratedValue(strategy="AUTO")
  9.   */
  10. private $id;
  11.  
  12. /**
  13.   * @var string $name
  14.   *
  15.   * @ORM\Column(name="name", type="string", length=255)
  16.   */
  17. private $name;
  18.  
  19. /*
  20.   * @var PrivateBusiness\BackendBundle:Categories
  21.   *
  22.   * @ORM\OneToMany(targetEntity="Categories", mappedBy="parent_id")
  23.   * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
  24.   */
  25. private $children;
  26.  
  27. /*
  28.   * @var integer $parent_id
  29.   *
  30.   * @ORM\Column(name="parent_id", type="integer")
  31.   *
  32.   *
  33.   * @ORM\ManyToOne(targetEntity="Categories", inversedBy="children")
  34.   */
  35. private $parent_id;
  36.  
  37.  
  38.  
  39. public function __construct() {
  40. $this->children = new \Doctrine\Common\Collections\ArrayCollection();
  41. }
  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 name
  56.   *
  57.   * @param string $name
  58.   * @return Categories
  59.   */
  60. public function setName($name)
  61. {
  62. $this->name = $name;
  63.  
  64. return $this;
  65. }
  66.  
  67. /**
  68.   * Get name
  69.   *
  70.   * @return string
  71.   */
  72. public function getName()
  73. {
  74. return $this->name;
  75. }
  76.  
  77. /**
  78.   * Set parent
  79.   *
  80.   * @param integer $parent
  81.   * @return Categories
  82.   */
  83. public function setParent_id($parent)
  84. {
  85. $this->parent_id = $parent;
  86.  
  87. return $this;
  88. }
  89.  
  90. /**
  91.   * Get parent
  92.   *
  93.   * @return integer
  94.   */
  95. public function getParent_id()
  96. {
  97. return $this->parent_id;
  98. }
  99.  
  100. public function setChildren($child){
  101. $this->child = $child;
  102.  
  103. return $this;
  104. }
  105.  
  106. public function getChildren(){
  107. return $this->child;
  108. }
  109.  
  110. public function __toString() {
  111. return $this->getName();
  112. }
  113. }
  114.  


Problem w tym, że symfony generuje mi tylko kolumny id oraz name, a jak zrobiłem tabele ręcznie to nie dodaje mi parent.
Crozin
1. Przecież to nie ma żadnego związku z Symfony.
2. Nazwą klasy powinno być raczej Category.
3. Model obiektowy nie jest tożsamy z modelem w bazie danych. Takich potworków jak parent_id nie ma prawa tutaj być.
4. http://docs.doctrine-project.org/projects/...elf-referencing
Marys91
Jak nie ma żadnego związku z Symfony? Projekt jest w Symfony, normalnie działa a w Symfony nie działa. Dobra nie ważne już chyba sobie poradziłem, wyrzuciłem wszystko i wpisałem jeszcze raz. Nie wiem gdzie był problem.
szok
Może tutaj:

  1. * @ORM\Column(name="parent_id", type="integer")
  2. *
  3. *
  4. * @ORM\ManyToOne(targetEntity="Categories", inversedBy="children")
  5. */

Zamiast:
  1. * @ORM\Column(name="parent_id", type="integer")
  2. * @ORM\ManyToOne(targetEntity="Categories", inversedBy="children")
  3. */


Może Symfony nie przerobiło tego bo były entery między tym?
Tak strzelam, ale zobacz swój aktualny kod, czy to się tym nie różni?
misi3kk
Błąd był na pewno w komentarzu.

Wymagane jest, aby komentarz z adnotacjami zaczynał się od "/**" - koniecznie, "/*" nie zadziała.
Marys91
Dobra chyba był błąd w tych całych komentarzach. Dzięki wielkie za pomoc.
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.