I'm trying to figure out how to add pipe with gzip compression to the mysqldump command using Symfony Process Component. What I have, and what doesn't seem to work is:
$date = Carbon::now();
$fileName = $date->format('Y-m-d_H-i-s').'.sql';
$command = [
$this->mysqldumpPath,
'--add-drop-table',
'--skip-comments',
'--default-character-set=utf8mb4',
$this->ignoreTables(),
'--user='.$this->dbConfig['username'],
'--password='.$this->dbConfig['password'],
$this->dbConfig['database'],
];
if ($this->compress) {
$command[] = '| gzip -9';
$fileName = $fileName.'.gz';
}
$file = $this->backupPath($fileName);
$process = new Process(array_merge(
$command,
['--result-file='.$file]
));
$process->run();
The error I'm getting is
mysqldump: Couldn't find table: "| gzip -9"
The ignoreTables() method
private function ignoreTables(): ?string
{
if (empty($this->ignoreTables)) {
return null;
}
return collect($this->ignoreTables)->map(
fn(string $table) => sprintf(
'--ignore-table=%s.%s',
$this->dbConfig['database'],
$table
)
)->implode(' ');
}
Any idea how I can pass piped gzip flag to the command?
You can still build your command as a string and run it in a shell with Process::fromShellCommandline.
I think your command won't work as written, though, so I'm making the changes I think are necessary (you can also clean this up and not use arrays to build the command).
Main:
$date = Carbon::now();
$fileName = $date->format('Y-m-d_H-i-s').'.sql';
$file = $this->backupPath($fileName);
$command = [
$this->mysqldumpPath,
'--add-drop-table',
'--skip-comments',
'--default-character-set=utf8mb4',
'--user="${:USER}"',
'--password="${:PASSWORD}"',
'"${:DATABASE}"',
];
$command = array_merge($command, $this->getIgnoredTablesArguments());
if ($this->compress) {
$command[] = ' | gzip -9 > "${:OUTPUT_FILE}"';
$file .= '.gz';
} else {
$command[] = '--result-file="${:OUTPUT_FILE}"';
}
$process = Process::fromShellCommandline(implode(' ', $command));
$parameters = [
'USER' => $this->dbConfig['username'],
'PASSWORD' => $this->dbConfig['password'],
'DATABASE' => $this->dbConfig['database'],
'OUTPUT_FILE' => $file,
];
$parameters = array_merge($parameters, $this->getIgnoredTablesParameters());
$process->run(null, $parameters);
Helper methods:
private function ignoreTables(): array
{
return collect($this->ignoreTables)->map(
fn(string $table) => sprintf(
'%s.%s',
$this->dbConfig['database'],
$table
)
);
}
private function getIgnoredTablesArguments(): array
{
$arguments = [];
foreach ($this->ignoreTables as $k => $v) {
$argName = sprintf('${:IGNORE_%s}', $k);
$arguments[] = sprintf('--ignore-table="%s"', $argName);
}
return $arguments;
}
private function getIgnoredTablesParameters(): array
{
$envs = [];
foreach ($this->ignoreTables() as $k => $v) {
$argName = sprintf('IGNORE_%s', $k);
$envs = array_merge($envs, [$argName => $v]);
}
return $envs;
}
Related
I have a translation in my project for templates and routes. when the locale is set, the translations are working except the locale parameter in the links is not correct.
my default language is NL. so I have a page www.website.com/nl/contact
when I switch to language fr I go to www.website.com/fr/contact, then when I go to look in the HTML source, the link is again www.website.com/nl/contact in stead of www.website.com/fr/contact so the locale parameter in the links is not updated regarding the setLocale
my module.php
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach(
\Zend\Mvc\MvcEvent::EVENT_DISPATCH_ERROR,
function ($e) {
$application = $e->getApplication();
$match = $application->getMvcEvent()->getRouteMatch();
if (null === $match) {
$params = [
'__NAMESPACE__' => 'Application\Controller',
'controller' => 'Index',
'action' => 'not-found',
// Here you can add common params for your application routes
];
$viewModel = $e->getViewModel();
$viewModel->setTemplate('layout/website');
$routeMatch = new RouteMatch($params);
$routeMatch->setMatchedRouteName('home');
$application->getMvcEvent()->setRouteMatch(
$routeMatch
);
}
}
);
$moduleRouteListener = new ModuleRouteListener();
if (!$e->getRequest() instanceof ConsoleRequest) {
$this->setLanguage($e);
$eventManager->attach('route', array($this, 'onPreRoute'), 100);
$moduleRouteListener->attach($eventManager);
$this->initAcls($e);
if (isset($_SERVER['APP_ENV']) and $_SERVER['APP_ENV'] == 'production') {
$eventManager->attach('finish', array($this, 'outputCompress'), 100);
}
}
}
private function setLanguage($e)
{
$redirectBase = $e->getRequest()->getServer()->get('REDIRECT_BASE');
$requestUri = $e->getRequest()->getServer()->get('REQUEST_URI');
$cmsString = substr($requestUri, strlen($redirectBase), 4);
$language = 'nl';
$routeTranslation = true;
if ($requestUri != '/') {
$language = substr($requestUri, 1, 2);
}
if ($cmsString == 'cms/') {
$language = 'en-us';
$routeTranslation = false;
}
$translator = $e->getApplication()->getServiceManager()->get('MvcTranslator');
$translator->setLocale($language);
if ($routeTranslation) {
$translator->addTranslationFile('PhpArray', __DIR__.'/../language/Routes/' . $language . '.php', 'default', $language);
}
}
public function onPreRoute($e)
{
$app = $e->getTarget();
$serviceManager = $app->getServiceManager();
$serviceManager->get('router')->setTranslator($serviceManager->get('MvcTranslator'));
}
I need of you for helping to resolve my problem.
I use Symfony 4 for display some charts with the bundle google charts
Thanks you a lot
This is my function controller
/**
* #Route("/admin/test", name="statsTest")
*/
public function statsTest(){
$results = $this->getDoctrine()->getRepository(IssueNubitalk::class)
->CountIssueByPartenaire('Canal');
dump($results);
// $results = [];
foreach ($results as $key => $val) {
// dump($results);
$results[] = [
'campaign' => $val->getCampaign(),
'total' => $val->getTotal(),
];
}
dump($results);
$data = json_decode(json_encode($results), true);
$pieChart = new PieChart();
$pieChart->getData()->setArrayToDataTable($data);
$pieChart->getOptions()->setHeight(250);
$pieChart->getOptions()->setWidth(400);
$pieChart->getOptions()->getTitleTextStyle()->setBold(true);
$pieChart->getOptions()->getTitleTextStyle()->setColor('#009900');
$pieChart->getOptions()->getTitleTextStyle()->setItalic(true);
$pieChart->getOptions()->getTitleTextStyle()->setFontName('Arial');
$pieChart->getOptions()->getTitleTextStyle()->setFontSize(20);
return $this->render('backend/test.html.twig', [
'piechart' => $pieChart,
]);
}
this is my repository
public function CountIssueByPartenaire($partenaire){
return $this->createQueryBuilder('i')
->select('i.campaign')
->addSelect('COUNT(DISTINCT i.sessionId) as total')
->where('i.campaign LIKE :campaign')
->setParameter('campaign', $partenaire.'_%')
->groupBy('i.campaign')
->getQuery()
->getResult();
}
this is the output
Call to a member function getCampaign() on array
I can't get the request POST this my code
public function inscriptionAction() {
$session = $this->getRequest()->getSession();
$request = Request::createFromGlobals();
$myCat = new \ stdClass;
$privatekey=$request->request->get('privatekey');
$key=$request->request->get('key');
if ($key== 'NULL' or $privatekey == NULL ) {
return $this->render('GestionsFilmeBundle:public:identification.html.twig', array(
'message' => 'Parametres non fournies-----',));
}
}
but privatekey and key are null
If you are using the Symfony Framework you don't need to build the Request object, you can just have it automatically injected in to your action and then you can use it from there.
public function inscriptionAction(Request $request) {
$privateKey = $request->request->get('privatekey');
$key = $request->request->get('key');
if (null === $key || null === $privateKey) {
return $this->render(
'GestionsFilmeBundle:public:identification.html.twig',
array(
'message' => 'Parametres non fournies-----',
)
);
}
}
I'm using the bundle "JMSSerializerBundle" for export retrieved entity data. I can export the results as json, xml or yml successfully.
But i need also a csv export for this results. This bundle can't handle csv export. But i'm also not sure how can i convert json,xml or yml to csv, because csv is a flat file.
Have anyone solve this issue before?
Update
Now i create an solution for my problem, i'm partially satisfied with my solution, because it is not recursive. I will show it.
if ($format == 'csv') {
$json = $serializer->serialize($query, 'json', SerializationContext::create()->enableMaxDepthChecks());
return $this->toCsv(json_decode($json, true));
}
/**
* #todo write an recursive function for deeper levels
*
* #param $data
* #return array
*/
protected function toCsv($data)
{
$headers = array();
$outerCounter = 0;
foreach ($data as $key => $value) {
foreach ($value as $i => $j) {
if (!is_array($j)) {
$headers[] = $i;
$result[$outerCounter][$i] = $j;
} else {
foreach ($j as $k => $m) {
if (!is_array($m)) {
$headers[] = $i. '_'.$k;
$result[$outerCounter][$i. '_'.$k] = $m;
} else {
foreach ($m as $n => $l) {
if (!is_array($l)) {
$headers[] = $i.'_'.$k.'_'.$n;
$result[$outerCounter][$i.'_'.$k.'_'.$n] = $l;
}
}
}
}
}
}
$outerCounter++;
}
asort($headers);
$headers = array_unique($headers);
return array('headers' => $headers, 'data' => $result);
}
A simple question:
I have one form, it returns one number and I need create this number of labels in Controller.
I try:
$form2 = $this->createFormBuilder();
for($i = 0; $i < $num; $i++) {
$name = 'column'.$i;
$form2->add($name,'number');
}
$form2->getForm();
I think it should very simple, but i can't..
Yes, you can do it with an array / hash map instead of a real object.
Here is an example :
// Create the array
$dataObj = array();
$dataObj['data1'] = '';
$dataObj['data2'] = 'default';
// ... do a loop here
$dataObj['data6'] = 'Hello';
// Create the form
$formBuilder = $this->createFormBuilder($dataObj);
foreach($dataObj as $key => $val)
{
$fieldType = 'text'; // Here, everything is a text, but you can change it based on $key, or something else
$formBuilder->add($key, $fieldType);
}
$form = $formBuilder->getForm();
// Process the form
$request = $this->get('request');
if($request->getMethod() == 'POST')
{
$form->bind($request); // For symfony 2.1.x
// $form->bind($this->get('request')->request->get('form')); // For symfony 2.0.x
if($form->isValid())
{
$dataObj = $form->getData();
foreach($dataObj as $key => $val)
{
echo $key . ' = ' . $val . '<br />';
}
exit('Done');
}
}
// Render
return $this->render('Aaa:Bbb:ccc.html.twig', array(
'requestForm' => $form->createView()));