Symfony2 console command arguments vs options when to use what - symfony

I would like to know of any guidelines to use input argument vs input options for passing in data to the symfony console command.
http://symfony.com/doc/current/components/console/introduction.html
I think arguments are to be used when the passed data is required for the command to execute, else use options.
Can you guys throw more light on this? What's the standard ?

I would suggest use Arguments when You want to specify excecution-required values, like:
bin/console vendor:delete-entity 120
but not
bin/console vendor:delete-entity --id=120
You can use Options, when You want to alter the default execution scenario, like:
bin/console vendor:delete-entity 120 --dump-sql
or
bin/console vendor:bulk-create something --batch-size=100

Related

TYPO3 Symfony Expression: isset() for Query Parameters?

I am using several Symfony expressions in my TypoScript checking for query parameters such as this:
[request.getQueryParams()['tx_news_pi1']['news'] > 0]
do something
[END]
This is working well – as long as the query parameter exists. If it doesn’t, the following error message is written into the log file:
Unable to get an item on a non-array.
In PHP I would use isset() to check whether the query parameter exists – but I could not find a similar way for Symfony expressions in TypoScript. I have tried
[request.getQueryParams()['tx_news_pi1']['news']]
which works the same, meaning: it does what it’s supposed to do, but logs an error message if the query parameter does not exist.
Is there anything like isset() for the Symfony Expression Language in TYPO3?
The is_defined() or isset() I was looking for will be returned by the condition
[request.getQueryParams()['tx_news_pi1']]
instead of
[request.getQueryParams()['tx_news_pi1']['news']]
In my use case this would even be enough. If you need to be more precise (e.g. to differentiate between different query parameters within the same plugin), go for
[request.getQueryParams()['tx_news_pi1'] && request.getQueryParams()['tx_news_pi1']['news'] > 0]
The solution was provided as a reply to a bug report on forge.typo3.org
Try this:
[request.getQueryParams()['tx_news_pi1']['news'] = ]
do something
[END]

Symfony doctrine:fixtures:load give ContextErrorException, Notice: Undefined variable

I can't figure out what's happening here.
I'm trying to add fixtures on a symfony project, nothing strange, fixtures seems to be well formed, and the console command :
php bin/console doctrine:fixtures:load --env=test
find my fixtures but don't want to load it since it say :
# php bin/console doctrine:fixtures:load --env=test
Careful, database will be purged. Do you want to continue y/N ?y
> purging database
> loading Covoituragesimple\AnnonceBundle\DataFixtures\ORM\AppFixtures
[Symfony\Component\Debug\Exception\ContextErrorException]
Notice: Undefined variable: EjehHi1ubBYApRfaNZnEo
I have no more idea where to looks ?
What to search ? Where it comes from ?
How to solve it ?
What I tried :
I try to delete the oneToOne and oneToMany relations -->deosn't seem to change anything)
ps: I use the version of doctrine-fixtures-bundle "doctrine/doctrine-fixtures-bundle": "2.*" (i was not abble to use the last one since i use symfony-framework-bundle 3.2.13 and the last version of doctrine-fixtures ask for 3.3+).
[RESOLVED] Aoutch ! Ok my bad, my bad.
The rubber duck debbuging helped me to fix it.
$client = new Client();
$client->setName("clienttest");
$client->setEmail("clienttest#provider.com");
$client->setUsername("clienttest");
$client->setDisplayname("clienttest");
$client->setSignaturemail("blablabla.<br />");
$client->setPassword("$2y$10$EjehHi1ubBYApRfaNZnEo.aQ/aZIiTJdY2sPetVWj7P7/yQNfhe7a");
$manager->persist($client);
$manager->flush();
I used double quote instead of simple quote, so php was trying to interpret the bcrypt hash of the password and told me that the var $EjehH.... doesn't exist (which is true).
Replacing double quote with single quote solved the problem.
$client->setPassword('$2y$10$EjehHi1ubBYApRfaNZnEo.aQ/aZIiTJdY2sPetVWj7P7/yQNfhe7a');
Thanks for help.

How does one create optional command line arguments in oozie workflow xml

Please bear in mind that I'm a complete rookie with oozie. I know that one can specify command line arguments in the oozie workflow xml by using the arg tag. I wondered how it is possible to specify an optional command line argument such that oozie will not complain that a required parameter is missing if the user doesn't specify it?
Many thanks in advance. If the information I've given is not specific enough, I can provide a concrete example when I log into my work machine tomorrow. We use apache commons CLI options to parse the options.
E.g. I want to make the following argument optional:
-e${endDateTime}
In your workflow wherever you would use ${myparam}, replace it with ${firstNotNull(wf:conf('myparam'), 'mydefaultvalue')}
In theory you should be able to use a "config-default.xml" file next to your "workflow.xml" file to give default values to the params in the workflow (see https://oozie.apache.org/docs/3.2.0-incubating/WorkflowFunctionalSpec.html) but I couldn't get it working.

Set locale in Console command with listener

I want to use the strftime function during a console command using the locale definition in my configuration.yml file.
Since I don't have a request, session, etc during a console command, I really don't know how to achieve this in a good way without hacking it into the code. Right now my code looks like this:
$date = "20131210";
$dateObject = \DateTime::createFromFormat("Ymd", $date);
setlocale(LC_ALL, $this->locale);
return strftime("%e %A %G", $dateObject->getTimestamp());
What I actualy want is a listener that somehow sets my locale.
Any suggestions?
I know that question is a bit old but maybe someone will find the following helpful as it is not so obvious.
If your command class extends ContainerAwareCommand you can get the locale from configuration using $this->getContainer()->getParameter('locale') (I assume that you have locale defined as a parameter in parameters.yml.
Now the "not so obvious part": at first you should check what locales your system supports. On linux server you can check that using the following command in the console:
locale -a
It should return something similar to:
C
en_US.utf8
pl_PL.utf8
POSIX
You can only use locales that are listed by that command. If you use eg pl_PL without the .utf8 part then probably it won't work.

How to access nested parameter values in Symfony2

I created a parameter in my parameters file:
parameters:
category:
var: test
I can access this in PHP by fetching it like this:
$var = $this->container->getParameter('category');
$var = $var['var'];
But how can I access this parameter in my config.yml? For example, I want to pass this parameter to all my twig files as a global twig variable:
twig:
globals:
my_var: %category.var% # throws ParameterNotFoundException
(Sidequestion:
I assumed I could access it via getParamter('category.var'), but got an error there. Do you know a nicer way than my two-liner? $this->container->getParameter('category')['var'] works, but is a syntax error according to my IDE.)
$this->container->getParameter('category')['var']
..is actually a pretty good way to go. Which version of PHP is your IDE using for its syntax checking? Somebody please correct me, but I think this behavior was made valid in 5.3 or 5.4.

Resources