Drukowana wersja tematu

Kliknij tu, aby zobaczyć temat w orginalnym formacie

Forum PHP.pl _ Frameworki _ [Symfony][SF][SF6] DataFixtures

Napisany przez: damianooo 6.02.2024, 22:43:45

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/DoctrineFixturesBundle/current/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 http://www.php.net/static function getGroups() : http://www.php.net/array
  13. {
  14. // return 1;
  15. return ['group1'];
  16. }
  17.  
  18. public function load(ObjectManager $manager) : void
  19. {
  20.  
  21. $commentsList = http://www.php.net/array(
  22. http://www.php.net/array(
  23. 'text' => 'cos tam cos tam cos tam 1',
  24. 'nick' => 'yyy',
  25. 'season_name' => 'Wiosna 2018'
  26. ),
  27. http://www.php.net/array(
  28. 'text' => 'cos tam cos tam cos tam 2',
  29. 'nick' => 'ooo',
  30. 'season_name' => 'Wiosna 2018'
  31. ),
  32. http://www.php.net/array(
  33. 'text' => 'cos tam cos tam cos tam 3',
  34. 'nick' => 'ppp',
  35. 'season_name' => 'Wiosna 2018'
  36. )
  37.  
  38. );
  39.  
  40. foreach ($commentsList as $commentsDetails) {
  41. $comment = new Comment();
  42. $comment->setText($commentsDetails['text']);
  43. $comment->setSeason($this->getReference('season-'.$commentsDetails['season_name']));
  44. $comment->setUser($this->getReference('user-'.$commentsDetails['nick']));
  45.  
  46. $manager->persist($comment);
  47. }
  48.  
  49. $manager->flush();
  50. }
  51.  
  52. }




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





Napisany przez: jacek.e3 7.02.2024, 09:07:03

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(): http://www.php.net/array
  2. {
  3. return [
  4. SeasonsFixtures::class
  5. ];
  6. }

Napisany przez: damianooo 7.02.2024, 20:39:47

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/DoctrineFixturesBundle/current/index.html
nie było o tym słowa.
Ale w sumie jednak powinienem czytać dokumantację samej wtyczki.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)