Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF2][SF] Relacja 1:n
Forum PHP.pl > Forum > PHP > Frameworki
basso
Witam,
kombinuje z relacjiami 1:n no i kurde ni w ząb exclamation.gif! NIE DZIAŁA. Biore przykłady z ksiażki "Symfony od podstaw" ale to samo => nie działa.

Moja klasy Entity. Strony z treścią i Kategorie stron z treścią.

Dziwne, że mi nie robi ani Kolekcji ani tabli i metod Add, tylko robi mi zwykłe gettery i settery.
Proszę luknijcie jak możecie.


Pages
  1. <?php
  2.  
  3. // src/Backend/PagesBundle/Entity/Pages.php
  4. namespace Backend\PagesBundle\Entity;
  5.  
  6. use Doctrine\ORM\Mapping as ORM;
  7.  
  8. /**
  9.  * Backend\PagesBundle\Entity\Pages
  10.  *
  11.  * @ORM\Table(name="pages")
  12.  * @ORM\Entity
  13.  * @ORM\HasLifecycleCallbacks
  14.  */
  15. class Pages
  16. {
  17. /**
  18.   * @var bigint $id
  19.   *
  20.   * @ORM\Column(name="id", type="bigint", nullable=false)
  21.   * @ORM\Id
  22.   * @ORM\GeneratedValue(strategy="IDENTITY")
  23.   */
  24. private $id;
  25.  
  26.  
  27. /**
  28.   * @var string $title
  29.   *
  30.   * @ORM\Column(name="title", type="string", length=255, nullable=false)
  31.   */
  32. private $title;
  33.  
  34. /**
  35.   * @var text $lead
  36.   *
  37.   * @ORM\Column(name="lead", type="text", nullable=false)
  38.   */
  39. private $lead;
  40.  
  41.  
  42. /**
  43.   * @var text $description
  44.   *
  45.   * @ORM\Column(name="description", type="text", nullable=true)
  46.   */
  47. private $description;
  48.  
  49. /**
  50.   * @var datetime $createdAt
  51.   *
  52.   * @ORM\Column(name="created_at", type="datetime", nullable=false)
  53.   */
  54. private $createdAt;
  55.  
  56. /**
  57.   * @var bigint $category
  58.   *
  59.   * @ORM\Column(name="category", type="bigint", nullable=false)
  60.   * @ORM\ManyToOne(targetEntity="PagesCategory", inversedBy="pages")
  61.   *
  62.   *
  63.   */
  64. private $category;
  65.  
  66.  
  67.  
  68.  
  69. /**
  70.   * Get id
  71.   *
  72.   * @return integer
  73.   */
  74. public function getId()
  75. {
  76. return $this->id;
  77. }
  78.  
  79. /**
  80.   * Set title
  81.   *
  82.   * @param string $title
  83.   * @return Pages
  84.   */
  85. public function setTitle($title)
  86. {
  87. $this->title = $title;
  88.  
  89. return $this;
  90. }
  91.  
  92. /**
  93.   * Get title
  94.   *
  95.   * @return string
  96.   */
  97. public function getTitle()
  98. {
  99. return $this->title;
  100. }
  101.  
  102. /**
  103.   * Set lead
  104.   *
  105.   * @param string $lead
  106.   * @return Pages
  107.   */
  108. public function setLead($lead)
  109. {
  110. $this->lead = $lead;
  111. return $this;
  112. }
  113.  
  114. /**
  115.   * Get lead
  116.   *
  117.   * @return string
  118.   */
  119. public function getLead()
  120. {
  121. return $this->lead;
  122. }
  123.  
  124. /**
  125.   * Set createdAt
  126.   *
  127.   * @param \DateTime $createdAt
  128.   * @return Pages
  129.   */
  130. public function setCreatedAt($createdAt)
  131. {
  132. $this->createdAt = $createdAt;
  133. return $this;
  134. }
  135.  
  136. /**
  137.   * Get createdAt
  138.   *
  139.   * @return \DateTime
  140.   */
  141. public function getCreatedAt()
  142. {
  143. return $this->createdAt;
  144. }
  145.  
  146. /**
  147.   * Set description
  148.   *
  149.   * @param string $description
  150.   * @return Pages
  151.   */
  152. public function setDescription($description)
  153. {
  154. $this->description = $description;
  155.  
  156.  
  157.  
  158. return $this;
  159. }
  160.  
  161. /**
  162.   * Get description
  163.   *
  164.   * @return string
  165.   */
  166. public function getDescription()
  167. {
  168. return $this->description;
  169. }
  170.  
  171.  
  172. /**
  173.   * @ORM\PrePersist
  174.   */
  175. public function setCreatedAtValue() {
  176. if(!$this->createdAt) {
  177. $this->createdAt = new \DateTime();
  178. }
  179. }
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186. /**
  187.   * Set category
  188.   *
  189.   * @param integer $category
  190.   * @return Pages
  191.   */
  192. public function setCategory($category)
  193. {
  194. $this->category = $category;
  195.  
  196. return $this;
  197. }
  198.  
  199. /**
  200.   * Get category
  201.   *
  202.   * @return integer
  203.   */
  204. public function getCategory()
  205. {
  206. return $this->category;
  207. }
  208. }


PagesCategory

  1.  
  2. <?php
  3.  
  4. // src/Backend/PagesBundle/Entity/PagesCategory.php
  5. namespace Backend\PagesBundle\Entity;
  6.  
  7. use Doctrine\ORM\Mapping as ORM;
  8.  
  9. /**
  10.  * Backend\PagesBundle\Entity\PagesCategory
  11.  *
  12.  * @ORM\Table(name="pages_category")
  13.  * @ORM\Entity
  14.  * @ORM\HasLifecycleCallbacks
  15.  */
  16. class PagesCategory
  17. {
  18. /**
  19.   * @var bigint $id
  20.   *
  21.   * @ORM\Column(name="id", type="bigint", nullable=false)
  22.   * @ORM\Id
  23.   *
  24.   *
  25.   */
  26. private $id;
  27.  
  28.  
  29. /**
  30.   * @var string $name
  31.   *
  32.   * @ORM\Column(name="name", type="string", length=255, nullable=false)
  33.   *
  34.   */
  35. private $name;
  36.  
  37.  
  38.  
  39. /**
  40.   * @var bigint $pages
  41.   *
  42.   * @ORM\Column(name="pages", type="bigint", nullable=false)
  43.   * @ORM\OneToMany(targetEntity="Pages", mappedBy="category")
  44.   */
  45. private $pages;
  46.  
  47.  
  48.  
  49. /**
  50.   * Set id
  51.   *
  52.   * @param integer $id
  53.   * @return PagesCategory
  54.   */
  55. public function setId($id)
  56. {
  57. $this->id = $id;
  58.  
  59. return $this;
  60. }
  61.  
  62. /**
  63.   * Get id
  64.   *
  65.   * @return integer
  66.   */
  67. public function getId()
  68. {
  69. return $this->id;
  70. }
  71.  
  72. /**
  73.   * Set name
  74.   *
  75.   * @param string $name
  76.   * @return PagesCategory
  77.   */
  78. public function setName($name)
  79. {
  80. $this->name = $name;
  81.  
  82. return $this;
  83. }
  84.  
  85. /**
  86.   * Get name
  87.   *
  88.   * @return string
  89.   */
  90. public function getName()
  91. {
  92. return $this->name;
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100. /**
  101.   * Set pages
  102.   *
  103.   * @param string $pages
  104.   * @return PagesCategory
  105.   */
  106. public function setPages($pages)
  107. {
  108. $this->pages = $pages;
  109.  
  110. return $this;
  111. }
  112.  
  113. /**
  114.   * Get pages
  115.   *
  116.   * @return string
  117.   */
  118. public function getPages()
  119. {
  120. return $this->pages;
  121. }
  122. }
  123.  
Crozin
Definiowanie adnotacji Column dla relacji jest pozbawione sensu. Co najwyżej może pojawić się tam adnotacja JoinColumn. Wszystko jest bardzo jasno i zwięźle opisane w dokumentacji, do której odsyłam.
basso
Spoko zrobiłem, no a dokumentacji o tym za nic nie mogłem znaleźć.

Jak nie w adnotacjach to gdzie? YML? A gdzie będzie dopisywał się kod?
Crozin
Chyba w ogóle nie przeglądałeś dokumentacji, bo bardzo wyraźnie w sekcji Association Mapping podany jest przykład One-To-Many, Bidirectional.
basso
O 1 raz tą stronę widzę... http://symfony.com/doc/current/index.html ja tylko stąd.
Dzięki.
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.