Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony][SF][SF6] DataFixtures, Problem z nowym podejściem w czasie migracji z SF 4.4 na SF 6.4
damianooo
post 6.02.2024, 22:43:45
Post #1





Grupa: Zarejestrowani
Postów: 493
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


Zacząłem migrować swój projekt z SF 4.4 na SF 6.4.
Mam problem z DataFixtures.

Korzystam z poniższego rozdziału z dokumentacji:
https://symfony.com/bundles/DoctrineFixture...rent/index.html

Wykonując polecenie z konsoli:
php bin/console doctrine:fixtures:load --group=group0 --group=group1

Dostaję komunikat:

C:\xampp\htdocs\ligatyperow3>php bin/console doctrine:fixtures:load --group=group0 --group=group1

Careful, database "ligatyperow3" will be purged. Do you want to continue? (yes/no) [no]:
> yes

> purging database
> loading App\DataFixtures\CommentsFixtures

In ReferenceRepository.php line 226:

Reference to "season-Wiosna 2018" does not exist


doctrine:fixtures:load [--append] [--group GROUP] [--em EM] [--purger PURGER] [--purge-exclusions PURGE-EXCLUSIONS] [--purge-with-truncate]




Poniżej wklejam dwie Entity powiązane relacją ze sobą. Comment -> Season (wiele do jednego, a więc W Sezonie może być wiele komentarzy).



  1. <?php
  2.  
  3. namespace App\DataFixtures;
  4.  
  5. use Doctrine\Bundle\FixturesBundle\Fixture;
  6. use Doctrine\Persistence\ObjectManager;
  7. use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
  8. use App\Entity\Comment;
  9.  
  10. class CommentsFixtures extends Fixture implements FixtureGroupInterface {
  11.  
  12. public static function getGroups() : array
  13. {
  14. // return 1;
  15. return ['group1'];
  16. }
  17.  
  18. public function load(ObjectManager $manager) : void
  19. {
  20.  
  21. $commentsList = array(
  22. 'text' => 'cos tam cos tam cos tam 1',
  23. 'nick' => 'yyy',
  24. 'season_name' => 'Wiosna 2018'
  25. ),
  26. 'text' => 'cos tam cos tam cos tam 2',
  27. 'nick' => 'ooo',
  28. 'season_name' => 'Wiosna 2018'
  29. ),
  30. 'text' => 'cos tam cos tam cos tam 3',
  31. 'nick' => 'ppp',
  32. 'season_name' => 'Wiosna 2018'
  33. )
  34.  
  35. );
  36.  
  37. foreach ($commentsList as $commentsDetails) {
  38. $comment = new Comment();
  39. $comment->setText($commentsDetails['text']);
  40. $comment->setSeason($this->getReference('season-'.$commentsDetails['season_name']));
  41. $comment->setUser($this->getReference('user-'.$commentsDetails['nick']));
  42.  
  43. $manager->persist($comment);
  44. }
  45.  
  46. $manager->flush();
  47. }
  48.  
  49. }




  1. <?php
  2.  
  3. namespace App\DataFixtures;
  4.  
  5. use Doctrine\Bundle\FixturesBundle\Fixture;
  6. use Doctrine\Persistence\ObjectManager;
  7. use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
  8. use App\Entity\Season;
  9.  
  10. class SeasonsFixtures extends Fixture implements FixtureGroupInterface {
  11.  
  12. public static function getGroups() : array
  13. {
  14. // return 0;
  15. return ['group0'];
  16. }
  17.  
  18. public function load(ObjectManager $manager) : void
  19. {
  20.  
  21. $seasonsList = array(
  22. 'season_name' => 'Jesień 2011',
  23. 'dateStart' => '2011-09-01',
  24. 'dateEnd' => '2011-12-16',
  25. 'active' => false
  26. ),
  27. 'season_name' => 'Wiosna 2012',
  28. 'dateStart' => '2012-02-01',
  29. 'dateEnd' => '2012-05-16',
  30. 'active' => false
  31. ),
  32. 'season_name' => 'Jesień 2012',
  33. 'dateStart' => '2012-09-01',
  34. 'dateEnd' => '2012-12-16',
  35. 'active' => false
  36. ),
  37. 'season_name' => 'Wiosna 2013',
  38. 'dateStart' => '2013-02-01',
  39. 'dateEnd' => '2013-05-16',
  40. 'active' => false
  41. ),
  42. 'season_name' => 'Jesień 2013',
  43. 'dateStart' => '2013-09-01',
  44. 'dateEnd' => '2013-12-16',
  45. 'active' => false
  46. ),
  47. 'season_name' => 'Wiosna 2014',
  48. 'dateStart' => '2014-02-01',
  49. 'dateEnd' => '2014-05-16',
  50. 'active' => false
  51. ),
  52. 'season_name' => 'Jesień 2014',
  53. 'dateStart' => '2014-09-01',
  54. 'dateEnd' => '2014-12-16',
  55. 'active' => false
  56. ),
  57. 'season_name' => 'Wiosna 2015',
  58. 'dateStart' => '2015-02-01',
  59. 'dateEnd' => '2015-05-16',
  60. 'active' => false
  61. ),
  62. 'season_name' => 'Jesień 2015',
  63. 'dateStart' => '2015-09-07',
  64. 'dateEnd' => '2015-12-20',
  65. 'active' => false
  66. ),
  67. 'season_name' => 'Wiosna 2016',
  68. 'dateStart' => '2016-02-01',
  69. 'dateEnd' => '2016-05-16',
  70. 'active' => false
  71. ),
  72. 'season_name' => 'Jesień 2016',
  73. 'dateStart' => '2016-09-05',
  74. 'dateEnd' => '2016-12-18',
  75. 'active' => false
  76. ),
  77. 'season_name' => 'Wiosna 2017',
  78. 'dateStart' => '2017-02-06',
  79. 'dateEnd' => '2017-05-21',
  80. 'active' => false
  81. ),
  82. 'season_name' => 'Jesień 2017',
  83. 'dateStart' => '2017-09-11',
  84. 'dateEnd' => '2017-12-24',
  85. 'active' => false
  86. ),
  87. 'season_name' => 'Wiosna 2018',
  88. 'dateStart' => '2018-02-05',
  89. 'dateEnd' => '2018-05-20',
  90. 'active' => false
  91. ),
  92. 'season_name' => 'Jesień 2018',
  93. 'dateStart' => '2018-09-01',
  94. 'dateEnd' => '2018-12-20',
  95. 'active' => false
  96. ),
  97. 'season_name' => 'Wiosna 2019',
  98. 'dateStart' => '2019-02-11',
  99. 'dateEnd' => '2019-05-26',
  100. 'active' => true
  101. ),
  102. 'season_name' => 'Jesień 2019',
  103. 'dateStart' => '2019-09-02',
  104. 'dateEnd' => '2019-12-22',
  105. 'active' => false
  106. ),
  107. 'season_name' => 'Wiosna 2020',
  108. 'dateStart' => '2020-02-10',
  109. 'dateEnd' => '2020-05-24',
  110. 'active' => false
  111. ),
  112. 'season_name' => 'Jesień 2020',
  113. 'dateStart' => '2020-09-07',
  114. 'dateEnd' => '2020-12-20',
  115. 'active' => false
  116. ),
  117. 'season_name' => 'Wiosna 2021',
  118. 'dateStart' => '2021-02-01',
  119. 'dateEnd' => '2021-05-26',
  120. 'active' => true
  121. ),
  122. 'season_name' => 'Jesień 2021',
  123. 'dateStart' => '2021-09-13',
  124. 'dateEnd' => '2021-12-26',
  125. 'active' => true
  126. ),
  127. 'season_name' => 'Wiosna 2022',
  128. 'dateStart' => '2022-02-07',
  129. 'dateEnd' => '2022-05-22',
  130. 'active' => true
  131. ),
  132. 'season_name' => 'Jesień 2022',
  133. 'dateStart' => '2022-08-01',
  134. 'dateEnd' => '2022-11-20',
  135. 'active' => true
  136. ),
  137. 'season_name' => 'Wiosna 2023',
  138. 'dateStart' => '2023-02-13',
  139. 'dateEnd' => '2023-06-04',
  140. 'active' => true
  141. ),
  142. 'season_name' => 'Jesień 2023',
  143. 'dateStart' => '2023-09-11',
  144. 'dateEnd' => '2023-12-24',
  145. 'active' => true
  146. ),
  147. 'season_name' => 'Wiosna 2024',
  148. 'dateStart' => '2024-02-12',
  149. 'dateEnd' => '2024-05-26',
  150. 'active' => true
  151. )
  152. );
  153.  
  154. foreach ($seasonsList as $seasonsDetails) {
  155. $season = new Season();
  156. $season->setName($seasonsDetails['season_name']);
  157. $season->setDateStart(new \DateTime($seasonsDetails['dateStart']));
  158. $season->setDateEnd(new \DateTime($seasonsDetails['dateEnd']));
  159. $season->setIsActive($seasonsDetails['active']);
  160.  
  161. $this->addReference('season-'.$seasonsDetails['season_name'], $season);
  162.  
  163. $manager->persist($season);
  164. }
  165.  
  166. $manager->flush();
  167.  
  168. }
  169.  
  170. }




Go to the top of the page
+Quote Post
jacek.e3
post 7.02.2024, 09:07:03
Post #2





Grupa: Zarejestrowani
Postów: 20
Pomógł: 6
Dołączył: 2.02.2010

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


FixtureGroupInterface pozwala załadować niektóre klasy zamiast wszystkich (ale nie pilnuje kolejności - i tu wpadł Twój przypadek)

do sterowania kolejnością służa te:
DependentFixtureInterface - pozwala upewnić się, że najpierw zostaną wczytane klasy zależne
OrderedFixtureInterface - pozwala ręcznie sterować kolejnością

skoro jedna fixtura wymaga drugiej to obydwie powinny należeć do tej samej group, a ta, która ma zależnośc powinna dodatkowo implementować DependentFixtureInterface
z metodą
  1. public function getDependencies(): array
  2. {
  3. return [
  4. SeasonsFixtures::class
  5. ];
  6. }
Go to the top of the page
+Quote Post
damianooo
post 7.02.2024, 20:39:47
Post #3





Grupa: Zarejestrowani
Postów: 493
Pomógł: 2
Dołączył: 15.07.2011
Skąd: Katowice

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


OK. Użyłem OrderedFixtureInterface bo miałem już tak w poprzedniej wersji projektu.
Nie wiedziałem że mogę teraz również z tego skorzystać, myślałem że teraz jest trochę inne podejście bo na stronie Symfony , tutaj:
https://symfony.com/bundles/DoctrineFixture...rent/index.html
nie było o tym słowa.
Ale w sumie jednak powinienem czytać dokumantację samej wtyczki.
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 27.04.2024 - 16:07