Simple Command Example

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

Simple Command Example

Post by tthlan »

1) Command

Code: Select all

<?php

namespace App\Command;

use Faker\Provider\DateTime;
use Interop\Queue\Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\User;
use App\Entity\PlayerChallenge;


class HotfixActualPointCalculationCommand extends Command
{
    ....
}
In class HotfixActualPointCalculationCommand extend interface Command with configure and execute functions

Code: Select all

    // the name of the command (the part after "bin/console")
    protected static $defaultName = 'app:actual-point-calculation-hotfix';
    private $params;
    private $em;

    public function __construct(ParameterBagInterface $params, EntityManagerInterface $entityManager)
    {
        $this->params = $params;
        $this->em = $entityManager;
        parent::__construct();
    }

    protected function configure()
    {
        $this
            // the short description shown while running "php bin/console list"
            ->setDescription('Actual Point Calculation')

            // the full command description shown when running the command with
            // the "--help" option
            ->setHelp('This command will update all player\'s actual point which total poin is null or 0');
    }

Code: Select all

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // ...
        // outputs multiple lines to the console (adding "\n" at the end of each line)
        $output->writeln([
            'Processing',
            '============',
            '',
        ]);

        // the value returned by someMethod() can be an iterator (https://secure.php.net/iterator)
        // that generates and returns the messages with the 'yield' PHP keyword
        $output->writeln($this->hotfix());


        $output->write('Finish!');

        return 0;
    }
execute will call progress hotfix function(s)

Code: Select all

    private function hotfix()
    {

        $players1 = $this->em->getRepository(User::class)->findBy([
            'total_point' => null
        ]);


        $players2 = $this->em->getRepository(User::class)->findBy([
            'total_point' => 0
        ]);

        $players = array_merge($players1,$players2);


        foreach ($players as $player){

            $actual_skill_points = $this->em->getRepository(PlayerChallenge::class)->calculatePlayerActuallPoint($player);

            $player->setPlayerActualPoints($actual_skill_points);

            $this->em->flush();

            $actual_skill_points['total_point'] = $player->getTotalPoint();
            $actual_skill_points['user_id'] = $player->getId();

            print_r($actual_skill_points) ;
            echo '\r\n';
        }

    }
2) Run it
php bin/console app:actual-point-calculation-hotfix
Post Reply