Using Symfony Console Commands

Symfony Console Commands

It can be used in a Symfony console application or in a web application. In symfony, Symfony Console component allows us to create commands.

It is basically used to perform such tasks which takes quite a long time, as from user point of view, once a user perform a batch job, expects the response asap. Also, it avoids Response Timeout.

Lets take an example:

Suppose, you want to remove a user from multiple tasks (say 5- 10 tasks) assigned to him. For this, we need to remove the user from task table as set user_id = null. It takes no time to update such small amount of rows. But what if we need to update 1000+ rows, that will take quite a long time for sure. In such scenario, we use Symfony Console Command, which updates the 1000+ rows in background and giving user response during execution.

Calling Command From Controller

This calls ‘uvdesk:ticket:unassign-discharged-agent 12345‘ command where 12345 is supposed user id.


namespace Webkul\UserBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

class UserController extends BaseController

    public function removeUserAction($userId)
        $userData = //check whether user exixts or not 
        if (!is_null($userData)) {
            $output = array();

            // Calling command from controller

            // for development environment
            $command = 'nohup php ../app/console uvdesk:task:unassign-discharged-agent' . $userData->getId() . ' > /dev/null 2>&1 & echo $!';

            // for production environment
            $command = 'nohup php ../app/console uvdesk:task:unassign-discharged-agent --env prod ' . $userData->getId() . ' > /dev/null 2>&1 & echo $!';

            exec($command, $output);
            $this->addFlash('success', $this->translate('Success ! User removed successfully.'));
            return $this->redirect($this->generateUrl('user_list'));






Commands are defined in classes extending Command. 

In below code, configure() is used to set name, description and argument passed to the command (say user_id) which is further used in execute() where main logic is implemented.

intialize() method is executed before the execute() method. Its main purpose is to initialise variables used in the rest of the command methods.


namespace Webkul\TaskBundle\Command;

use Doctrine\ORM\Query;
use Symfony\Component\HttpFoundation\Request;
use Webkul\TaskBundle\Entity\Task;
use Webkul\UserBundle\Entity\UserData;
use Webkul\UserBundle\Entity\User;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;

class RemovingDischargedAgentCommand extends ContainerAwareCommand
    private $container;
	private $entityManager;

	protected function configure()
        $this->setDescription('Removes discharged agent from tasks');
        $this->addArgument('userId', InputArgument::REQUIRED);

	protected function initialize(InputInterface $input, OutputInterface $output)
		$this->container = $this->getContainer();
        $this->entityManager = $this->container->get('doctrine.orm.entity_manager');

	protected function execute(InputInterface $input, OutputInterface $output)
        $userId = $input->getArgument('userId');
        $tickets = $this->entityManager->createQueryBuilder()
            ->from(Task::class, 'task')
            ->where('task.user = :agent')
            ->setParameter('agent', $userId)

            foreach($tickets as $ticket){


The above code executes in background untill it removes user_id from given 1000+ rows but success response is given earlier to the user.

I’ve explored this while contributing to Symfony based project UVdesk, there are a lot more things to learn and you could also contribute to an enterprise-level open source helpdesk.

Thanks for reading me. I hope this blog would help you with a better understanding of the Symfony Console Command. Please share your reviews on this, which will support me to write more.

Until next time. 👋


Category(s) Symfony UVdesk
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.