Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF2][Symfony] - gdzie trzymać przykładowe dane
Forum PHP.pl > Forum > PHP > Frameworki
damianooo
Potrzebuję rozwiązać następujący problem:

W pliku CostamBundle/DataFixtures/ORM/LoadData.php mam klasę:

  1. class LoadData implements \Doctrine\Common\DataFixtures\FixtureInterface{}


a w niej metodę:

  1. function load(ObjectManager $manager){}


W metodzie tej dla każdej z Encji mojej bazy danych porobiłem sobie tablice wypełnione danymi, które później zamieniam na obiekty i ładuję do każdej z tabel. Przykład poniżej:

  1.  
  2. namespace My\CostamBundle\DataFixtures\ORM;
  3.  
  4. use Doctrine\Common\Persistence\ObjectManager;
  5. use My\CostamBundle\Entity\Matchday;
  6. ...
  7.  
  8. class LoadData implements \Doctrine\Common\DataFixtures\FixtureInterface
  9. {
  10. function load(ObjectManager $manager)
  11. {
  12. /**
  13.   * Matchday
  14.   */
  15. $matchdaySchedule = array(
  16. 'date_start' => array(
  17. '2014-02-10',
  18. '2014-02-17',
  19. '2014-02-24',
  20. '2014-03-03',
  21. '2014-03-10',
  22. '2014-03-17',
  23. '2014-03-24',
  24. '2014-03-31',
  25. '2014-04-07',
  26. '2014-04-14',
  27. '2014-04-21',
  28. '2014-04-28',
  29. '2014-05-05',
  30. '2014-05-12',
  31. '2014-05-19'
  32. ),
  33. 'date_end' => array(
  34. '2014-02-16',
  35. '2014-02-23',
  36. '2014-03-02',
  37. '2014-03-09',
  38. '2014-03-16',
  39. '2014-03-23',
  40. '2014-03-30',
  41. '2014-04-06',
  42. '2014-04-13',
  43. '2014-04-20',
  44. '2014-04-27',
  45. '2014-05-04',
  46. '2014-05-11',
  47. '2014-05-18',
  48. '2014-05-25'
  49. )
  50. );
  51.  
  52. $matchdays = array();
  53. for ($i = 0; $i < count($matchdaySchedule['date_start']); $i++) {
  54. $matchday = new Matchday();
  55. $matchday->setName('Kolejka ' . ($i + 1));
  56. $matchday->setDateStart(new \DateTime($matchdaySchedule['date_start'][$i]));
  57. $matchday->setDateEnd(new \DateTime($matchdaySchedule['date_end'][$i]));
  58. $manager->persist($matchday);
  59. $matchdays[] = $matchday;
  60. }
  61.  
  62. ....
  63.  
  64. $manager->flush();
  65.  
  66.  


Wiem że to nie jest dobry pomysł ponieważ w mojej metodzie loadData jest teraz ok. 1000 linijek kodu.

Chciałem się zapytać jak powinienem to prawidłowo zrobić ?
Czy powinienem stworzyć w osobnym katalogu klasę statyczną w której umieszczę metodę/metody a w niej wszystkie tablice do załadowania i następnie tą klasę użyję w klasie loadData ?
Gdzie w Symfony powinienem umieścić takie zbiory danych, które posłużą do załadowania przykładowych danych do bazy danych ?
Jak Wy to robicie ?
kpt_lucek
Może powinieneś zainteresować się TYM i TYM?
damianooo
Pobrałem pakiet nelmio/alice .

Utworzyłem przykładowy plik:

My\CostamBundle\DataFixtures\ORM\matchday.yml

  1. MyCostamBundle\Entity\Matchday:
  2. matchday_1:
  3. date_start: '2014-02-10'
  4. date_end: '2014-02-16'
  5. matchday_2:
  6. date_start: '2014-02-17'
  7. date_end: '2014-02-23'
  8. matchday_3:
  9. date_start: '2014-02-24'
  10. date_end: '2014-03-02'


natomiast w pliku:

My\CostamBundle\DataFixtures\ORM\LoadData.php

zrobiłem tak:

  1. namespace My\CostamBundle\DataFixtures\ORM;
  2. use Doctrine\Common\Persistence\ObjectManager;
  3. use Doctrine\Common\DataFixtures\FixtureInterface;
  4. use Nelmio\Alice\Fixtures;
  5. class LoadData implements FixtureInterface
  6. {
  7. function load(ObjectManager $manager)
  8. {
  9. Fixtures::load(__DIR__.'/matchday.yml', $manager);
  10. }
  11. }


Wykonując teraz w konsoli polecenie:
php app/console doctrine:fixtures:load

otrzymuję następujący błąd:

  1. PHP Fatal error: Class 'MyCostamBundle\Entity\Matchday' not found in /var/www/costam/vendor/nelmio/alice/src/Nelmio/Alice/Loader/Base.php on line 386
  2. Fatal error: Class 'MyCostamBundle\Entity\Matchday' not found in /var/www/costam/vendor/nelmio/alice/src/Nelmio/Alice/Loader/Base.php on line 386


wychodzi na to, że metoda load() nie może znaleźć pliku Entity/Matchday.php z klasą Matchday.

być może źle szuka .... pytanie czy dobre odwołanie mam w pliku YAML .

czy powinienem po instalacji pakietu go nadpiywać w takim sensie, że skopiować pakiet nelmio/alice z vendora i wkleić w app/Resources a następnie dopiero tam umieścić swoje Entity ?
kpt_lucek
Zdaje się że masz dobrze, aczkolwiek zwróć uwagę na błąd

  1. PHP Fatal error: Class 'MyCostamBundle\Entity\Matchday' not found in /var/www/costam/vendor/nelmio/alice/src/Nelmio/Alice/Loader/Base.php on line 386


Czyli nie masz poprawnie podanej encji. Podaj jej kod, do weryfikacji
damianooo
wydaje mi się że jest ok:


  1. namespace My\CostamBundle\Entity;
  2.  
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Gedmo\Mapping\Annotation as Gedmo;
  5.  
  6. /**
  7.  * Matchday
  8.  *
  9.  * @ORM\Table()
  10.  * @ORM\Entity(repositoryClass="My\CostamBundle\Entity\MatchdayRepository")
  11.  */
  12. class Matchday
  13. {
  14. /**
  15.   * @var integer
  16.   *
  17.   * @ORM\Column(name="id", type="integer")
  18.   * @ORM\Id
  19.   * @ORM\GeneratedValue(strategy="AUTO")
  20.   */
  21. private $id;
  22.  
  23. /**
  24.   * @var string
  25.   *
  26.   * @ORM\Column(name="name", type="string", length=255)
  27.   */
  28. private $name;
  29.  
  30. /**
  31.   * @ORM\OneToMany(targetEntity="Meet", mappedBy="matchday")
  32.   */
  33. protected $meets;
  34.  
  35. /**
  36.   * @ORM\OneToMany(targetEntity="Pick", mappedBy="matchday")
  37.   */
  38. protected $picks;
  39.  
  40. /**
  41.   * @ORM\OneToMany(targetEntity="Vote", mappedBy="matchday")
  42.   */
  43. protected $votes;
  44.  
  45. /**
  46.   * @var \DateTime
  47.   *
  48.   * @ORM\Column(name="date_start", type="date")
  49.   */
  50. private $date_start;
  51.  
  52. /**
  53.   * @var \DateTime
  54.   *
  55.   * @ORM\Column(name="date_end", type="date")
  56.   */
  57. private $date_end;
  58.  
  59. /**
  60.   * @var datatime $created
  61.   *
  62.   * @Gedmo\Timestampable(on="create")
  63.   * @ORM\Column(type="date")
  64.   */
  65. private $created;
  66.  
  67. /**
  68.   * @var datatime $updated
  69.   *
  70.   * @Gedmo\Timestampable(on="update")
  71.   * @ORM\Column(type="date")
  72.   */
  73. private $updated;
  74.  
  75.  
  76. /**
  77.   * Get id
  78.   *
  79.   * @return integer
  80.   */
  81. public function getId()
  82. {
  83. return $this->id;
  84. }
  85.  
  86. /**
  87.   * Constructor
  88.   */
  89. public function __construct()
  90. {
  91. $this->meets = new \Doctrine\Common\Collections\ArrayCollection();
  92. }
  93.  
  94. /**
  95.   * Add meets
  96.   *
  97.   * @param \My\CostamBundle\Entity\Meet $meets
  98.   * @return Matchday
  99.   */
  100. public function addMeet(\My\CostamBundle\Entity\Meet $meets)
  101. {
  102. $this->meets[] = $meets;
  103.  
  104. return $this;
  105. }
  106.  
  107. /**
  108.   * Remove meets
  109.   *
  110.   * @param \My\LigaTyperowBundle\Entity\Meet $meets
  111.   */
  112. public function removeMeet(\My\CostamBundle\Entity\Meet $meets)
  113. {
  114. $this->meets->removeElement($meets);
  115. }
  116.  
  117. /**
  118.   * Get meets
  119.   *
  120.   * @return \Doctrine\Common\Collections\Collection
  121.   */
  122. public function getMeets()
  123. {
  124. return $this->meets;
  125. }
  126.  
  127. /**
  128.   * Set date_start
  129.   *
  130.   * @param \DateTime $dateStart
  131.   * @return Matchday
  132.   */
  133. public function setDateStart($dateStart)
  134. {
  135. $this->date_start = $dateStart;
  136.  
  137. return $this;
  138. }
  139.  
  140. /**
  141.   * Get date_start
  142.   *
  143.   * @return \DateTime
  144.   */
  145. public function getDateStart()
  146. {
  147. return $this->date_start;
  148. }
  149.  
  150. /**
  151.   * Set date_end
  152.   *
  153.   * @param \DateTime $dateEnd
  154.   * @return Matchday
  155.   */
  156. public function setDateEnd($dateEnd)
  157. {
  158. $this->date_end = $dateEnd;
  159.  
  160. return $this;
  161. }
  162.  
  163. /**
  164.   * Get date_end
  165.   *
  166.   * @return \DateTime
  167.   */
  168. public function getDateEnd()
  169. {
  170. return $this->date_end;
  171. }
  172.  
  173. /**
  174.   * Set name
  175.   *
  176.   * @param string $name
  177.   * @return Matchday
  178.   */
  179. public function setName($name)
  180. {
  181. $this->name = $name;
  182.  
  183. return $this;
  184. }
  185.  
  186. /**
  187.   * Get name
  188.   *
  189.   * @return string
  190.   */
  191. public function getName()
  192. {
  193. return $this->name;
  194. }
  195.  
  196. /**
  197.   * Set created
  198.   *
  199.   * @param \DateTime $created
  200.   * @return Matchday
  201.   */
  202. public function setCreated($created)
  203. {
  204. $this->created = $created;
  205.  
  206. return $this;
  207. }
  208.  
  209. /**
  210.   * Get created
  211.   *
  212.   * @return \DateTime
  213.   */
  214. public function getCreated()
  215. {
  216. return $this->created;
  217. }
  218.  
  219. /**
  220.   * Set updated
  221.   *
  222.   * @param \DateTime $updated
  223.   * @return Matchday
  224.   */
  225. public function setUpdated($updated)
  226. {
  227. $this->updated = $updated;
  228.  
  229. return $this;
  230. }
  231.  
  232. /**
  233.   * Get updated
  234.   *
  235.   * @return \DateTime
  236.   */
  237. public function getUpdated()
  238. {
  239. return $this->updated;
  240. }
  241.  
  242. /**
  243.   * Add picks
  244.   *
  245.   * @param \My\CostamBundle\Entity\Pick $picks
  246.   * @return Matchday
  247.   */
  248. public function addPick(\My\CostamBundle\Entity\Pick $picks)
  249. {
  250. $this->picks[] = $picks;
  251.  
  252. return $this;
  253. }
  254.  
  255. /**
  256.   * Remove picks
  257.   *
  258.   * @param \My\CostamBundle\Entity\Pick $picks
  259.   */
  260. public function removePick(\My\CostamBundle\Entity\Pick $picks)
  261. {
  262. $this->picks->removeElement($picks);
  263. }
  264.  
  265. /**
  266.   * Get picks
  267.   *
  268.   * @return \Doctrine\Common\Collections\Collection
  269.   */
  270. public function getPicks()
  271. {
  272. return $this->picks;
  273. }
  274.  
  275. /**
  276.   * Add votes
  277.   *
  278.   * @param \My\CostamBundle\Entity\Vote $votes
  279.   * @return Matchday
  280.   */
  281. public function addVote(\My\CostamBundle\Entity\Vote $votes)
  282. {
  283. $this->votes[] = $votes;
  284.  
  285. return $this;
  286. }
  287.  
  288. /**
  289.   * Remove votes
  290.   *
  291.   * @param \My\CostamBundle\Entity\Vote $votes
  292.   */
  293. public function removeVote(\My\CostamBundle\Entity\Vote $votes)
  294. {
  295. $this->votes->removeElement($votes);
  296. }
  297.  
  298. /**
  299.   * Get votes
  300.   *
  301.   * @return \Doctrine\Common\Collections\Collection
  302.   */
  303. public function getVotes()
  304. {
  305. return $this->votes;
  306. }
  307. }
kpt_lucek
Upewnij się że namespace'y są dokładnie przepisane
damianooo
miałeś rację ... namespace miałem złe wpisane w pliku YAML ...

było tak:

  1. MyCostamBundle\Entity\Matchday:

a powinno być tak:

  1. My\CostamBundle\Entity\Matchday:


Jednak pojawił się jeszcze jeden błąd:

  1. PHP Fatal error: Call to a member function format() on a non-object in /var/www/costam/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateType.php on line 44
kpt_lucek
Sprawdź bez uszu -> '
damianooo
właśnie zapomniałem dodać że tak już próbowałem i nie pomogło:

  1. My\CostamBundle\Entity\Matchday:
  2. matchday_1:
  3. date_start: 2014-02-10
  4. date_end: 2014-02-16
  5. matchday_2:
  6. date_start: 2014-02-17
  7. date_end: 2014-02-23
  8. matchday_3:
  9. date_start: 2014-02-24
  10. date_end: 2014-03-02


Zmieniłem też settery:

  1. /**
  2.   * Set date_start
  3.   *
  4.   * @param \DateTime $dateStart
  5.   * @return Matchday
  6.   */
  7. public function setDateStart($dateStart)
  8. {
  9. $this->date_start = new \DateTime($dateStart);
  10. return $this;
  11. }



i też nie pomogło .. ehh sad.gif
kpt_lucek
  1. /**
  2.   * Set date_start
  3.   *
  4.   * @param \DateTime $dateStart
  5.   * @return Matchday
  6.   */
  7. public function setDateStart(\DateTime $dateStart)
  8. {
  9. $this->date_start = $dateStart;
  10.  
  11. return $this;
  12. }


Zgodnie z @param \DateTime $dateStart
damianooo
niestety tak też nie chce zadziałać
kpt_lucek
Bazując na dokumentacji, możesz spróbować w ten sposób:
  1. created: <dateTimeBetween('-200 days', 'now')>
  2. updated: <dateTimeBetween($created, 'now')>
damianooo
Niesamowite ...

Zadziałało w ten sposób:

  1. My\CostamBundle\Entity\Matchday:
  2. matchday_1:
  3. name: a1
  4. date_start: <datetime('2014-02-10')>
  5. date_end: <datetime('2014-02-16')>
  6. matchday_2:
  7. name: a2
  8. date_start: <datetime('2014-02-17')>
  9. date_end: <datetime('2014-02-23')>
  10. matchday_3:
  11. name: a3
  12. date_start: <datetime('2014-02-24')>
  13. date_end: <datetime('2014-03-02')>



dzięki !
kpt_lucek
Teraz możesz zrobić w ten sposób:
  1. My\LigaTyperowBundle\Entity\Matchday:
  2. matchday_{1..X}:
  3. name: a<current()>
  4. date_start: <dateTimeBetween('-200 days', '2014-02-24')>
  5. date_end: <dateTimeBetween($created, '2014-03-02')>


Podmień sobie X na dowolną liczbę naturalną > 1 i masz wygenerowane X fixturek 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.