PHP code example of evotodi / seed-bundle

1. Go to this page and download the library: Download evotodi/seed-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

evotodi / seed-bundle example snippets




namespace App\Seeds;

use Evotodi\SeedBundle\Command\Seed;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use App\Entity\User;

class UserSeed extends Seed
{
    
    /**
    * Return the name of your seed
    */
    public static function seedName(): string
    {
        /**
        * The seed won't load if this is not set
        * The resulting command will be seed:user
        */
        return 'user';
    }
    
    /**
    * Optional ordering of the seed load/unload.
    * Seeds are loaded/unloaded in ascending order starting from 0. 
    * Multiple seeds with the same order are randomly loaded. 
    */
    public static function getOrder(): int 
    {
      return 0; 
    }
    
    /**
    * The load method is called when loading a seed 
    */
    public function load(InputInterface $input, OutputInterface $output): int
    { 

        /**
        * Doctrine logging eats a lot of memory, this is a wrapper to disable logging
        */ 
        $this->disableDoctrineLogging();

        $users = [
            [
                'email' => '[email protected]',
                'password' => 'password123',
                'roles' => ['ROLE_ADMIN'],
            ],


        ];

        foreach ($users as $user){
            $userRepo = new User();
            $userRepo->setEmail($user['email']);
            $userRepo->setRoles($user['roles']);
            $userRepo->setPassword($this->passwordEncoder->encodePassword($userRepo, $user['password']));
            $this->manager->persist($userRepo);
        }
        $this->manager->flush();
        $this->manager->clear();
        
        /**
        * Must return an exit code.
        * A value other than 0 or Command::SUCCESS is considered a failed seed load/unload.
        */ 
        return 0;
    }
    
    /**
    * The unload method is called when unloading a seed 
    */
    public function unload(InputInterface $input, OutputInterface $output): int
    {
        //Clear the table
        $this->manager->getConnection()->exec('DELETE FROM user');
        
        /**
        * Must return an exit code.
        * A value other than 0 or Command::SUCCESS is considered a failed seed load/unload.
        */ 
        return 0;
    }
}