PDO Works for all but not in Cron Yii2 - pdo

I am working on Yii2 advanced app and I have created cron job for my need but same code inside cron works in application but not works in console cron controller.
It gives error like 'Class PDO not found'.
namespace console\controllers;
use yii\console\Controller;
class CronsController extends Controller {
public function actionIndex($id = null) {
if(isset($id)){
$command = \Yii::$app->db->createCommand("INSERT INTO table (user) VALUES (:user)");
foreach($gets as $row){
$command->bindValue(':user', $row['user']);
$command->execute();
}
}
}

first step command run
php -m | grep PDO
To see if it exists,if not exists PDO, you need install PDO extension
http://php.net/manual/en/pdo.installation.php

Related

Unable to solve "The type name 'Models' does not exist in the type 'NotificationApp'" Error after some command execution

I was running command below:
dotnet new page -n Index -na myapp.Pages.Area -o Pages/Area
ns=-na=myapp.Pages
dotnet new page -n Index $ns.Area -o Pages/Area
And it eventually caused the error:
Views/_ViewImports.cshtml(1,7): error CS0138: A 'using namespace'
directive can only be applied to namespaces; 'NotificationApp' is a
type not a namespace. Consider a 'using static' directive instead
[/home/seocliff/Desktop/dotNetProjects/NotificationApp/NotificationApp.csproj]`
Now everytime I run dotnet build or dotnet run above error keeps appearing. Any solution?
I removed the namespace lines within _ViewImports.cshtml:
#using NotificationApp
#using NotificationApp.Models
I also removed related files to "ErrorViewModel", like the following:
ErrorViewModel.cs within Views/Home/Shared
Error.cshtml within Views/Home/Shared
Also I had temporarily commented Error Action Method:
// public IActionResult Error()
// {
// return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
// }
Then I run dotnet build and then launch the app again with dotnet run. It all worked out well. I will further investigate how it cause this issue as I unable to find a proper answer

How to add new Yii webapp arguments?

Using Yii 1.1.14
For those who have customised the webapp (ie, mywebapp) command, have you, or do you know, any way on how to add more parameters to that command?
We already have the git param for example, we wish to add some interactive prompts to the user too, for example: setup the config/main.php files.
Any clue about this subject?
This is a snippet of the run() function:
public function run($args) {
$vcs = false;
if (isset($args[1])) {
if ($args[1] != 'git' && $args[1] != 'hg')
$this->usageError('Unsupported VCS specified. Currently only git and hg supported.');
$vcs = $args[1];
}
// ...
Just like you see, $args[1] must be always the VCS you use.
By logic you need to change/develop the body of run() to be adapted to the new arguments you want.
A probably call would be:
webapp ../test git setconfig
or
webapp ../test git true
The third argument will be available as $args[2].

GitLab new project setup webhook

I have GitLab setup on a server. Every time I create a new project under my user, I have to manually add a Web Hook by navigating to Project -> Settings -> Web Hooks -> Add Web Hook
Since many users will be creating projects on the hosted GitLab it will be difficult to setup Web Hooks for each project individually.
Is there a way, so that when a new project is created, it automatically (as default) sets up a Web Hook as part of the new project?
Any help is much appreciated.
You could probably find a way to automate this via the System Hooks API. Listen for the project_create hook event, then make a call to the project hook endpoint to add the one you want.
It's webhooks all the way down!
I found this script what do all you need:
recive the hook and execute the query insert of web hook
Run with php -S 0.0.0.0:8080
<?php
$allowed_host = "ip_of_your_gitlab_server";
$sql_host = "localhost_or_ip_of_your_mysql_server";
$sql_user = "..";
$sql_password = "..";
$database = "gitlabhq_production";
if ($_SERVER['REMOTE_ADDR'] == $allowed_host) { // ofc you could increase security with some kind of API key or w/e
$sql = mysqli_connect($sql_host, $sql_user, $sql_password, $database);
if (mysqli_connect_errno()) {
exit;
}
$file = fopen("/tmp/sql_error", "a"); // you can use this as debug log too ;)
$input = json_decode(file_get_contents('php://input'), true);
$pid = $input['project_id'];
if ($input['event_name'] == "project_create") {
$url = 'url_of_your_project_hook';
// this creates a project hook, specify it for your needs..
$q = mysqli_query($sql, "INSERT INTO gitlabhq_production.web_hooks (url, project_id) VALUES('$url', '$pid')");
if (!$q) fwrite($file, $sql->error);
} elseif ($input['event_name'] == "project_destroy")
$q = mysqli_query($sql, "DELETE FROM gitlabhq_production.web_hooks WHERE project_id='$pid'");
mysqli_close($sql);
} else {
// redirect?
exit;
}

Class not found in Laravel 4 even after updating in composer and start/global.php

I'm having trouble using a custom class in Laravel 4.
In a new directory app/library there is NewClass.php
Here is the class:
<?
class NewClass {
public static function jjj()
{
return 'hello';
}
}
The library folder has been registered in composer.json:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/library"
]
and also in app/start/global.php:
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/library',
));
Following this, I have done php artisan dump-autoload, clear-compiled, optimize as well as composer update in CLI. All operations were performed without error.
Now when I try to access the class - either by instantiating and calling the jjj() method or calling it statically from anywhere from the app, I get class not found exception.
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Class 'NewClass' not found
Now, what have I overlooked - why is this not working??

Symfony2 : Failed to start the session because headers have already been sent

TL;DR
Getting an error on a Linux box with Nginx / PHP-FPM stating "Failed to start the session because headers have already been sent.". Error is not occurring on Apache local machine setup
So on my local machine I have the Symfony2 app running fine. No errors are popping up. But as soon as I deploy to our Linux Server I'm getting this error when I call a certain Action within a Controller class
Failed to start the session because headers have already been sent.
In the index action I have already called
$session = $this->getRequest()->getSession();
And in another action within the same controller class I'm calling it again. The error pops up when I try a
$session->set('foo', $bar);
In my Twig I'm calling the action by a form and a button with a formaction property like so
<form id='blahblah'>
.... some fields here .....
<button type='submit' formaction='{{path('2ndAction')}}'></a>
</form>
So on my local machine, running Apache everything run fine. The Linux server is using Nginx and php-fpm and it's crashing for some reason. I checked the phpInfo() and the session auto start is set to off. Not sure if this is an Nginx/php-fpm issue or not but I thought it may be pertinent information.
Here is the Controller declaration, indexAction(), and my 2ndAction()
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\Session;
use CBSi\Utils\HTTPUtils\CURLUtil;
class StartController extends Controller
{
/**
* #var CurlUtil $curlUtil
*/
private $curlUtil;
/**
* #var AccessControl $accessControl
*/
private $accessControl;
/*placeholder for request object*/
private $requestHolder;
/**
* #Route("/path/for/action/one", name="start")
* #Template()
*/
public function indexAction()
{
$session = $this->getRequest()->getSession();
$this->curlUtil = $this->get('curlUtil');
$this->requestHolder= Request::createFromGlobals();
// Some logic is done here
return $this->render('ListEngagementBundle:Start:start.html.twig');
}
/**
* #Route("/path/to/second/action", name="2ndAction")
* #Template
*/
public function 2ndAction(){
$session = $this->getRequest()->getSession();
$this-> curlUtil = $this->get('curlUtil');
$this->requestHolder= Request::createFromGlobals();
//Some logic is done here to get the data for the session variable
$bar= logic output
$session->set('foo', $bar);
return $this->redirect($this->generateUrl('start'));
}
}
If you need more info that I can provide I will :)
So I figured it out. In the 2nd action where I was calling
$session->getRequest()->getSession();
I had to change that to
$session = new Session();
$session->start();
Go figure. :P
I've get the same error. But in my case I've placed some spaces in the AppKernel.php before the < ?php
tag. So if someone else get this error, checkout if you have some spaces or tabs before in first line of each .php file which get loaded before session get initialized.
This happens to me when in some of the scripts that anticipate $this->session->start();
there is a echo statement!
Hope this can help someone else debugging the issue
I was getting this error message every time I tried to update my database schema using symfony console and when I tried to install new dependencies using composer:
[RuntimeException]
Failed to start the session because headers have already been sent by "/var
/www/html/pulsar283/src/MyService/Local/Brasil.php" at line 153.
So, I went for check the file , and, on line 152 I found a " ?> " (php close tag ).
So, to I just remove the php close tag and the error never shown again !