Fixture Load Migration Initialized Data records with upload images for a table

Post Reply
tthlan
Quản trị viên
Posts: 76
Joined: Tue Aug 23, 2016 8:13 am

Fixture Load Migration Initialized Data records with upload images for a table

Post by tthlan »

Code as example load fixtures migration data of a table's records of upload initialized images

- first pre-upload 09 images to folder as path /public/init-photos/facilitator-category
- create fixture file ../src/DataFixtures/FacilitatorCategoryFixtures.php

required some lib and include on 'use'

../src/DataFixtures/FacilitatorCategoryFixtures.php

Code: Select all

<?php

namespace App\DataFixtures;

use App\Entity\FacilitatorCategory;
use App\Entity\Image;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

use App\Service\ImageHelper;
use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
use Liip\ImagineBundle\Async\Commands;
use Liip\ImagineBundle\Async\ResolveCache;


class FacilitatorCategoryFixtures extends Fixture
{
    public function __construct(ImageHelper $imageHelper, ParameterBagInterface $parameters, UploaderHelper $uploaderHelper, \Enqueue\Client\ProducerInterface $producer)
    {
        $this->params = $parameters;
        $this->uploaderHelper = $uploaderHelper;
        $this->imageHelper = $imageHelper;
        $this->producer = $producer;
    }

    public function load($manager){

        $imageFacilitatorCategoryDirectory = __DIR__ . '/../../public/' . 'init-photos/facilitator-category' . '/';

        // Run-late when avaiale image resource files
        $FacilitatorCategories = array(
            array( 'image_path' => $imageFacilitatorCategoryDirectory . 'Category1.png', 'name'=>'Gestion de crise' ),
            array( 'image_path' => $imageFacilitatorCategoryDirectory . 'Category2.png', 'name'=>'Dynamique collective' ),
            array( 'image_path' => $imageFacilitatorCategoryDirectory . 'Category3.png', 'name'=>'Dynamique individuelle',),
            array( 'image_path' => $imageFacilitatorCategoryDirectory . 'Category4.png', 'name'=>'Générateur de chaos' ),
            array( 'image_path' => $imageFacilitatorCategoryDirectory . 'Category5.png', 'name'=>'Cadre de référence', ),
            array( 'image_path' => $imageFacilitatorCategoryDirectory . 'Category6.png', 'name'=>'Règles de vie', ),
            array( 'image_path' => $imageFacilitatorCategoryDirectory . 'Category7.png', 'name'=>'Personas', ),
            array( 'image_path' => $imageFacilitatorCategoryDirectory . 'Category8.png', 'name'=>'Système de jeu' ),
            array( 'image_path' => $imageFacilitatorCategoryDirectory . 'Category9.png', 'name'=>'Rôles' )
        );

        foreach($FacilitatorCategories as $category){

            if (!$manager->getRepository(FacilitatorCategory::class)->findOneBy(['name' => $category['name']])) {

                $facilitatorCategory = new FacilitatorCategory();
                $facilitatorCategory->setName($category['name']);

                $user = $manager->getRepository(User::class)->find(1);
                $imgObj = $this->imageHelper->createImageObjFromSource($user, $category['image_path']);

                if ($imgObj instanceof Image) {
                    $manager->persist($imgObj);
                    $path = $this->uploaderHelper->asset($imgObj, 'imageFile');

                    $this->producer->sendCommand(Commands::RESOLVE_CACHE, new ResolveCache($path));
                    $facilitatorCategory->setImage($imgObj);
                    $manager->persist($facilitatorCategory);
                }

                $manager->flush();
            }
        }
    }
}
../src/Entity/FacilitatorCategory.php

Code: Select all

<?php

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMSSerializer;
use JMS\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * @ORM\Entity
 * @ORM\Table(name="facilitator_category")
 * @ORM\Entity(repositoryClass="App\Repository\FacilitatorCategoryRepository")
 * @ORM\HasLifecycleCallbacks
 * @JMSSerializer\ExclusionPolicy("all")
 */
class FacilitatorCategory {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMSSerializer\Expose
     * @Groups({"facilitator_category_default"})
     */
    protected $id;

    /**
     * Category Name
     * @ORM\Column(type="string", length=40)
     * @var string
     * @JMSSerializer\Expose
     * @Groups({"facilitator_category_default"})
     */
    protected $name;

    /**
     * @ORM\ManyToOne(targetEntity="Image")
     * @ORM\JoinColumn(referencedColumnName="id", nullable=false)
     * @JMSSerializer\Expose
     * @Groups({"facilitator_category_default"})
     */
    protected $image;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getImage(): ?Image
    {
        return $this->image;
    }

    public function setImage(?Image $image): self
    {
        $this->image = $image;

        return $this;
    }
}
Post Reply