The custom class dataUtils already created in symfony-1. I am trying to recreate the same class in symfony-5. when I try to access entityManager it shows "cannot use $this in a non-object context". I have attached code for reference.
<?php
namespace App\Model;
class DateUtils {
private $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
parent::__construct();
}
public static function executeQuery($connection_param_name, $query, $values = array(), $connection = null, $charset_to = null, $i = 0)
{
$em = $this->entityManager; // error (cannot use $this in a non-object context)
if (!sizeof($values)>0 and stripos(strtolower($query), "where")!==false)
{
$filesystem = new Filesystem();
$filesystem->dumpFile("log.txt", $query);
}
try {
$connection = (! $connection) ? $em->getConnection($connection_param_name) : $connection;
if ($charset_to == 'utf-8') {
self::converToUtf8 ( $connection );
}
if ($charset_to == 'latin1') {
self::converToLatin1 ( $connection );
}
if ($charset_to == 'latin2') {
self::converToLatin2 ( $connection );
}
if ($charset_to == 'latin5') {
self::converToLatin5 ( $connection );
}
$resultset = $connection->createQuery($query);
foreach ($values as $key => $value){
$resultset->bindValue($key, $value);
}
$resultset->execute();
} catch (\Doctrine\ORM\ORMException $e) {
$i++;
if((strpos($e->getMessage (), 'Deadlock') !== false || strpos($e->getMessage (), 'Lock wait timeout exceeded') !== false) && $i < 12){
$sleep = rand ( 1 , 5000 );
usleep($sleep);
return self::executeQuery($connection_param_name, $query, $values, $connection, $charset_to, $i);
}elseif(strpos($e->getMessage (), 'Base table or view not found') !== false && strpos($e->getMessage (), 'st_user_value_berechnung_tmp') !== false && $i < 11){
return self::executeQuery($connection_param_name, $query, $values, $connection, $charset_to, $i);
}else{
$sendMessage = gethostname () . ' - DataUtils:executeQuery: ' . $e->getMessage () . ' - ' . $query . ' - '.serialize($values);
SystemTrayPeer::log ( 'err', $sendMessage, 'databank-errors', 2 );
}
}
return (isset($resultset) ? $resultset : false);
}
}
I also don't know how to use BasePeer and SystemTrayPeer class, which is in symfony1, to use in symfony-5. Should I need to add DatenUtils class as a service in service.yaml ?