1 | <?php |
||
2 | |||
3 | namespace App\DataFixtures; |
||
4 | |||
5 | use App\Entity\Image; |
||
6 | use App\Entity\Tag; |
||
7 | use App\Entity\Trick; |
||
8 | use Doctrine\Bundle\FixturesBundle\Fixture; |
||
9 | use Doctrine\Common\DataFixtures\DependentFixtureInterface; |
||
10 | use Doctrine\Common\Persistence\ObjectManager; |
||
11 | use Faker\Factory; |
||
12 | |||
13 | class TrickImageFixtures extends Fixture implements DependentFixtureInterface |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * This method must return an array of fixtures classes |
||
18 | * on which the implementing class depends on |
||
19 | * |
||
20 | * @return array |
||
21 | */ |
||
22 | public function getDependencies() |
||
23 | { |
||
24 | return array( |
||
25 | TrickFixtures::class, |
||
26 | ); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Load data fixtures with the passed EntityManager |
||
31 | * |
||
32 | * @param ObjectManager $manager |
||
33 | */ |
||
34 | public function load(ObjectManager $manager) |
||
35 | { |
||
36 | $faker = Factory::create(); |
||
37 | |||
38 | $tricks = $manager->getRepository(Trick::class)->findAll(); |
||
39 | |||
40 | |||
41 | foreach ($tricks as $trick) { |
||
42 | $maxTags = rand(0, 8); |
||
43 | $primaryImageSet = false; |
||
44 | if ($maxTags > 0) { |
||
45 | for ($i = 0; $i <= $maxTags; $i++) { |
||
46 | $image = new Image(); |
||
47 | |||
48 | $image->setTitle($faker->words(rand(1, 3), true)); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
49 | $image->setImage($faker->image('public/uploads/trick_images', 400, 300, null, false)); |
||
50 | $image->setTrick($trick); |
||
51 | if(rand(0,2)>0 && !$primaryImageSet){ |
||
52 | $image->setPrimaryImage(true); |
||
53 | $primaryImageSet = true; |
||
54 | } |
||
55 | |||
56 | $manager->persist($image); |
||
57 | $manager->persist($trick); |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | $manager->flush(); |
||
62 | } |
||
63 | } |