php app / console stops working - php

After a slight modification of my units, I wanted the update with a simple php app/console doctrine: update --force. But no action executed and in addition no response. I then did a php app/check.php meaning me no problems (Your system is ready to run Symfony2 projects). I do not understand and it doesn't provide an error. Here's what I've done:
Command: ********: ***** ProjetSymphony $ php app / console***
Answer (none): ******* **** $ ProjetSymphony***
If someone has an idea.
Screen :

Try with:
php app/console doctrine:schema:update --force
Maybe it's only a syntaxis error.

Also, if anyone tries to run php app/console in a newer symfony version (for example symfony 3.0), you will get an error: no file found because the file was moved to 'bin' folder. Now to run from the console, you have to use php bin/console instead. Just in case this change confused anyone who started to learn symfony and updated to 3.0.

I finally found my mistake. I had a command file that prevented the execution of my order (CreateUserCommand.php)
If someone wants to explain to me why this cosait file an error during the execution of my order ...
Here is the file :
<?php
namespace FP\UserBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use FOS\UserBundle\Model\User;
use FOS\UserBundle\Command\CreateUserCommand as BaseCommand;
class CreateUserCommand extends BaseCommand
{
/**
* #see Command
*/
protected function configure()
{
exit;
echo "tes";
parent::configure();
$this
->setName('fp:user:create')
->getDefinition()->addArguments(array(
new InputArgument('age', InputArgument::REQUIRED, 'The age')
))
;
}
/**
* #see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
exit;
echo "tes";
$username = $input->getArgument('username');
$email = $input->getArgument('email');
$password = $input->getArgument('password');
$age = $input->getArgument('age');
$inactive = $input->getOption('inactive');
$superadmin = $input->getOption('super-admin');
$manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
$manipulator->setAge($age);
$manipulator->create($username, $password, $email, !$inactive, $superadmin);
$output->writeln(sprintf('Created user <comment>%s</comment>', $username));
}
/**
* #see Command
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
exit;
echo "tes";
parent::interact($input, $output);
if (!$input->getArgument('age')) {
$age = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose a age:',
function($age) {
if (empty($age)) {
throw new \Exception('Lastname can not be empty');
}
return $age;
}
);
$input->setArgument('age', $age);
}
}
}

Related

Import massive csv data with symfony command too slow with doctrine

I need to import a lot of data from a csv file (45 Mo) in myqsl database with Symfony. I imported League\Csv\Reader library
I made a command with doctrine.
It works but I is very slow.
How can I accelerate this ?
I tried to :
adding : $this->em->clear() after $this->em->flush();
adding : //Disable SQL Logging: to avoid huge memory loss.
$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
.
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use App\Entity\Developer;
use App\Entity\BadgeLabel;
use Doctrine\ORM\EntityManagerInterface;
use League\Csv\Reader;
class CsvImportCommand extends Command
{
public function __construct(EntityManagerInterface $em){
parent::__construct();
$this->em = $em;
}
// the name of the command (the part after "bin/console")
protected static $defaultName = 'app:import-developpers';
protected function configure()
{
$this
// the short description shown while running "php bin/console list"
->setDescription('Import a new developper.')
// the full command description shown when running the command with
// the "--help" option
->setHelp('This command allows you to import a develpper...')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$io->title('Importation en cours');
$reader = Reader::createFromPath('%kernel.root_dir%/../src/Data/developers_big.csv')
->setHeaderOffset(0)
;
$results = $reader->getrecords();
$io->progressStart(iterator_count($results));
//Disable SQL Logging: to avoid huge memory loss.
$this->em->getConnection()->getConfiguration()->setSQLLogger(null);
foreach ($results as $row) {
$developer = $this->em->getRepository(Developer::class)
->findOneBy([
'firstName' => ($row['FIRSTNAME']),
'lastName'=> ($row['LASTNAME'])
])
;
if (null === $developer) {
$developer = new developer;
$developer
->setFirstName($row['FIRSTNAME'])
->setLastName($row['LASTNAME']);
$this->em->persist($developer);
$this->em->flush();
$this->em->clear();
}
$badgeLabel = $this->em->getRepository(BadgeLabel::class)
->findOneBy([
'name' => ($row['BADGE LABEL']),
'level'=> ($row['BADGE LEVEL'])
])
;
if (null === $badgeLabel) {
$badgeLabel = new BadgeLabel;
$badgeLabel
->setName($row['BADGE LABEL'])
->setLevel($row['BADGE LEVEL']);
$this->em->persist($badgeLabel);
$this->em->flush();
$this->em->clear();
}
$developer
->addBadgeLabel($badgeLabel);
$io->progressAdvance();
}
$this->em->flush();
$this->em->clear();
$io->progressFinish();
$io->success('Importation terminée avec succès');
}
}
The command works put its to slow. After 15 min, only 32% was updload in my Mysql database. I Expected it in 2 minutes max
Method1: (not the best)
When flush method is called, Symfony go throught all listeners. So, you could avoid to flush on each loop. You can replace each flush by this code:
if (0 === ($batchSize++ % $input->getOption('fetch'))) {
$this->entityManager->flush();
$this->entityManager->clear();
}
fetch option can be declared in configure method:
const BATCH_SIZE = 1000; // As example
/**
* Configure the command.
*/
protected function configure()
{
$this
// the short description shown while running "php bin/console list"
->setDescription('Import a new developper.')
//This option helps you to find a good value and use BATCH_SIZE constant as default
->addOption('fetch', 'f', InputArgument::OPTIONAL, 'Number of loop between each flush', self::BATCH_SIZE)
// the full command description shown when running the command with
// the "--help" option
->setHelp('This command allows you to import a develpper...')
;
Method2: More efficient
You can create a command which writes all SQL queries with update or insert in a sql file. Then, you launch a native command that read the files and execute queries.
Method3: Using DBAL
As suggested in comments, youcould use DBAL to avoid unnecessary object hydration with Doctrine.

How to create a console command in Symfony2 application

I need to create a console command for a Symfony2 application and I read docs here and here though I am not sure what of those I should follow. So this is what I did.
Create a file under /src/PDI/PDOneBundle/Console/PDOneSyncCommand.php
Write this code:
namespace PDI\PDOneBundle\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class PDOneSyncCommand extends Command
{
protected function configure()
{
$this
->setName('pdone:veeva:sync')
->setDescription('Some description');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$name = $input->getArgument('name');
if ($name) {
$text = 'Hello '.$name;
} else {
$text = 'Hello';
}
if ($input->getOption('yell')) {
$text = strtoupper($text);
}
$output->writeln($text);
}
}
Create a file under /bin
Write this code:
! /usr/bin/env php
require __ DIR __ .'/vendor/autoload.php';
use PDI\PDOneBundle\Console\Command\PDOneSyncCommand;
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new PDOneSyncCommand());
$application->run();
But when I go to console by running php app/console --shell and hit ENTER I can't see the command registered, what I am missing?
NOTE: Can someone with more experience than me format the second piece of code properly?
UPDATE 1
Ok, following suggestions and taking answer as a start point I built this piece of code:
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$auth_url = $container->get('login_uri')."/services/oauth2/authorize?response_type=code&client_id=".$container->get('client_id')."&redirect_uri=".urlencode($container->get('redirect_uri'));
$token_url = $container->get('login_uri')."/services/oauth2/token";
$revoke_url = $container->get('login_uri')."/services/oauth2/revoke";
$code = $_GET['code'];
if (!isset($code) || $code == "") {
die("Error - code parameter missing from request!");
}
$params = "code=".$code
."&grant_type=".$container->get('grant_type')
."&client_id=".$container->get('client_id')
."&client_secret=".$container->get('client_secret')
."&redirect_uri=".urlencode($container->get('redirect_uri'));
$curl = curl_init($token_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status != 200) {
die("Error: call to token URL $token_url failed with status $status, response $json_response, curl_error ".curl_error(
$curl
).", curl_errno ".curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
$access_token = $response['access_token'];
$instance_url = $response['instance_url'];
if (!isset($access_token) || $access_token == "") {
die("Error - access token missing from response!");
}
if (!isset($instance_url) || $instance_url == "") {
die("Error - instance URL missing from response!");
}
$output->writeln('Access Token ' . $access_token);
$output->writeln('Instance Url ' . $instance_url);
}
But any time I invoke the task I got this error:
[Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException]
You have requested a non-existent service "login_uri".
Why? Can't I access paramters on parameter.yml file? Where I am failing?
You are reading article about Console Component. This is slightly different than registering a command in your bundle.
First, your class should live in Namespace Command, and it must include the Command prefix in classname. You've mostly done that. I will show you a sample command to grasp the idea so you can continue working with that as a base.
<?php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
// I am extending ContainerAwareCommand so that you can have access to $container
// which you can see how it's used in method execute
class HelloCommand extends ContainerAwareCommand {
// This method is used to register your command name, also the arguments it requires (if needed)
protected function configure() {
// We register an optional argument here. So more below:
$this->setName('hello:world')
->addArgument('name', InputArgument::OPTIONAL);
}
// This method is called once your command is being called fron console.
// $input - you can access your arguments passed from terminal (if any are given/required)
// $output - use that to show some response in terminal
protected function execute(InputInterface $input, OutputInterface $output) {
// if you want to access your container, this is how its done
$container = $this->getContainer();
$greetLine = $input->getArgument('name')
? sprintf('Hey there %s', $input->getArgument('name'))
: 'Hello world called without arguments passed!'
;
$output->writeln($greetLine);
}
}
Now, running app/console hello:world' you should see a simple Hello world at your terminal.
Hope you got the idea, dont hesitate to ask if you have questions.
Edit
In Commands you cant directly access request, because of scopes. But you can pass arguments when you call your command. In my example I've registered optional argument which leads to two different outputs.
If you call your command like this app/console hello:world you get this output
Hello world called without arguments passed!
but if you provide a name like this app/console hello:world Demo you get the following result:
Hey there Demo
Following Artamiel's answer and the comments below, here what you would need to build a command run as a CRON task (at least, this is how I've done it):
First, declare your SalesforceCommand class:
<?php
class SalesforceCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('pdone:veeva:sync')
->setDescription('Doing some tasks, whatever...');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$myService = $this->getContainer()->get('my.service');
$returnValue = $myService->whateverAction();
if($returnValue === true)
$output->writeln('Return value of my.service is true');
else
$output->writeln('An error occured!');
}
}
Then, create your controller in whatever bundle you want:
<?php
namespace My\MyBundle\Service;
use Symfony\Component\HttpFoundation\RequestStack;
class ServiceController extends Controller
{
private $_rs;
public function __construct(RequestStack $rs)
{
$this->_rs = $rs;
}
public function whateverAction()
{
$request = $this->_rs->getCurrentRequest();
// do whatever is needed with $request.
return $expectedReturn ? true : false;
}
}
Finally, register your Controller as a Service in app/config/services.yml
services:
my.service:
class: My\MyBundle\Service\ServiceController
arguments: ["#request_stack"]
(as of Symfony 2.4, instead of injecting the request service, you should inject the request_stack service and access the Request by calling the getCurrentRequest() method)
You are finally able to use run it in as a CRON job by adding the following to your crontab (for it to run every minute):
* * * * * /usr/local/bin/php /path/to/your/project/app/console pdone:veeva:sync 1>>/path/to/your/log/std.log 2>>/path/to/your/log/err.log
Hope that helps!

Symfony2 command change environment

I want to set up an order allowing me to make clear: cache test mode, then do a drop database, drop scheama, add scheme, add fixtures in test mode.
class BaseCommand extends \Symfony\Component\Console\Command\Command {
//put your code here
protected function configure()
{
$this
->setName('mycommand:test')
->setDescription('Launch test')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$command_first_migration = $this->getApplication()->find('cache:clear');
$arguments_first_migration = array(
'command' => 'cache:clean',
'--env' => 'test'
);
$input_first_migration = new ArrayInput($arguments_first_migration);
try {
$returnCode = $command_first_migration->run($input_first_migration, $output);
} catch (\Doctrine\DBAL\Migrations\MigrationException $ex) {
echo "MigrationExcepion !!!! ";
}
}
}
but I have this result :
clearing the case for the dev environment with debug true
How to pass the test in dev environment?
thank you
You can't set the --env=test since the Kernel and the environment are already created when you run php app/console mycommand:test.
The only way is to specify the env when you run your command :
php app/console mycommand:test --env=test

Running console command from a Symfony 2 test case

Is there a way to run a console command from a Symfony 2 test case? I want to run the doctrine commands for creating and dropping schemas.
This documentation chapter explains how to run commands from different places. Mind, that using exec() for your needs is quite dirty solution...
The right way of executing console command in Symfony2 is as below:
Option one
use Symfony\Bundle\FrameworkBundle\Console\Application as App;
use Symfony\Component\Console\Tester\CommandTester;
class YourTest extends WebTestCase
{
public function setUp()
{
$kernel = $this->createKernel();
$kernel->boot();
$application = new App($kernel);
$application->add(new YourCommand());
$command = $application->find('your:command:name');
$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));
}
}
Option two
use Symfony\Component\Console\Input\StringInput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
class YourClass extends WebTestCase
{
protected static $application;
public function setUp()
{
self::runCommand('your:command:name');
// you can also specify an environment:
// self::runCommand('your:command:name --env=test');
}
protected static function runCommand($command)
{
$command = sprintf('%s --quiet', $command);
return self::getApplication()->run(new StringInput($command));
}
protected static function getApplication()
{
if (null === self::$application) {
$client = static::createClient();
self::$application = new Application($client->getKernel());
self::$application->setAutoExit(false);
}
return self::$application;
}
}
P.S. Guys, don't shame Symfony2 with calling exec()...
The docs tell you the suggested way to do it. The example code is pasted below:
protected function execute(InputInterface $input, OutputInterface $output)
{
$command = $this->getApplication()->find('demo:greet');
$arguments = array(
'command' => 'demo:greet',
'name' => 'Fabien',
'--yell' => true,
);
$input = new ArrayInput($arguments);
$returnCode = $command->run($input, $output);
// ...
}
Yes, if your directory structure looks like
/symfony
/app
/src
then you would run
phpunit -c app/phpunit.xml.dist
from your unit tests you can run php commands either by using
passthru("php app/console [...]") (http://php.net/manual/en/function.passthru.php)
exec("php app/console [...]") (http://www.php.net/manual/en/function.exec.php)
or by putting the command in back ticks
php app/consode [...]
If you are running the unit tests from a directory other than symofny, you'll have to adjust the relative path to the app directory for it to work.
To run it from the app:
// the document root should be the web folder
$root = $_SERVER['DOCUMENT_ROOT'];
passthru("php $root/../app/console [...]");
The documentation has been updated since my last answer to reflect the proper Symfony 2 way of calling an existing command:
http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command

Dump database data using Doctrine 2

Is it possible to dump a database using doctrine 2? I have read that symfony has a library which extends doctrine to do it but How could I use it in my zendframework project with Bisna Doctrine 2 Integration?
For Symfony2:
Type
php app/console doctrine:schema:create --dump-sql
in the command line
This is an old thread but I was just doing something similar in Symfony and decided to develop an actual command for it. That's more of a Symfony way of doing it and gives you more control on the output as well as allowing you access to the parameters, so you don't have to parse Yaml using bash script :)
namespace Fancy\Command;
use Fancy\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
class DatabaseDumpCommand extends AbstractCommand
{
/** #var OutputInterface */
private $output;
/** #var InputInterface */
private $input;
private $database;
private $username;
private $password;
private $path;
/** filesystem utility */
private $fs;
protected function configure()
{
$this->setName('fancy-pants:database:dump')
->setDescription('Dump database.')
->addArgument('file', InputArgument::REQUIRED, 'Absolute path for the file you need to dump database to.');
}
/**
* #param InputInterface $input
* #param OutputInterface $output
* #return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->database = $this->getContainer()->getParameter('database_name') ;
$this->username = $this->getContainer()->getParameter('database_user') ;
$this->password = $this->getContainer()->getParameter('database_password') ;
$this->path = $input->getArgument('file') ;
$this->fs = new Filesystem() ;
$this->output->writeln(sprintf('<comment>Dumping <fg=green>%s</fg=green> to <fg=green>%s</fg=green> </comment>', $this->database, $this->path ));
$this->createDirectoryIfRequired();
$this->dumpDatabase();
$output->writeln('<comment>All done.</comment>');
}
private function createDirectoryIfRequired() {
if (! $this->fs->exists($this->path)){
$this->fs->mkdir(dirname($this->path));
}
}
private function dumpDatabase()
{
$cmd = sprintf('mysqldump -B %s -u %s --password=%s' // > %s'
, $this->database
, $this->username
, $this->password
);
$result = $this->runCommand($cmd);
if($result['exit_status'] > 0) {
throw new \Exception('Could not dump database: ' . var_export($result['output'], true));
}
$this->fs->dumpFile($this->path, $result);
}
/**
* Runs a system command, returns the output, what more do you NEED?
*
* #param $command
* #param $streamOutput
* #param $outputInterface mixed
* #return array
*/
protected function runCommand($command)
{
$command .=" >&1";
exec($command, $output, $exit_status);
return array(
"output" => $output
, "exit_status" => $exit_status
);
}
}
and AbstractCommand is just a class that extends symfony's ContainerAwareCommand:
namespace Fancy\Command;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
abstract class AbstractCommand extends ContainerAwareCommand
{
}
Doctrine has no database-dump feature.
I agree it would be nice, but it's also not the ORM's goal.
You could dump the database using
a PHP script
a system mysqldump
phpMyAdmin
Here's an article explaining those solutions.
I created a small script that read the parameters from app/config/parameters.yml and output all the data from a MySQL database to a file (with current datetime used as name).
Save this in the root of your Symfony project (e.g. mysqldump.sh):
#!/bin/bash
# See http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in/23905052#23905052
ROOT=$(readlink -f $(dirname "$0"))
cd $ROOT
# Get database parameters
dbname=$(grep "database_name" ./app/config/parameters.yml | cut -d " " -f 6)
dbuser=$(grep "database_user" ./app/config/parameters.yml | cut -d " " -f 6)
dbpassword=$(grep "database_password" ./app/config/parameters.yml | cut -d " " -f 6)
filename="$(date '+%Y-%m-%d_%H-%M-%S').sql"
echo "Export $dbname database"
mysqldump -B "$dbname" -u "$dbuser" --password="$dbpassword" > "$filename"
echo "Output file :"
ls -lh "$filename"
Result when running the script:
$ bash mysqldump.sh
Export […] database
Warning: Using a password on the command line interface can be insecure.
Output file :
-rw-rw-r-- 1 […] […] 1,8M march 1 14:39 2016-03-01_14-39-08.sql
Depend on your database. if you use mysql, create a php command to utilise mysqldump
like running this
mysqldump -u YourUser -p YourDatabaseName > wantedsqlfile.sql
For a more generic doctrine way:
protected function execute(InputInterface $input, OutputInterface $output)
{
$conn = $this->getDoctrineConnection('default');
$path = $input->getArgument('filepath');
if (! is_dir(dirname($path))) {
$fs = new Filesystem();
$fs->mkdir(dirname($path));
}
$cmd = sprintf('mysqldump -u %s --password=%s %s %s > %s',
$conn->getUsername(),
$conn->getPassword(),
$conn->getDatabase(),
implode(' ', ['variables', 'config']),
$path
);
exec($cmd, $output, $exit_status);
}

Categories