0

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 ?

pramod
  • 3
  • 5

1 Answers1

0

You can't access properties of your class from a static method, that's why you are getting this error.

If you're rewriting your application you should consider what's the real job of this class. It seems that it handles too many differents jobs :)

Mcsky
  • 1,426
  • 1
  • 10
  • 20
  • Thank you @Mcsky, the real job of this class is to use the method to execute query by connecting to concerned database. Also, I will be rewriting some more methods which will be used for custom database table column selection/insert and so on. – pramod Aug 16 '21 at 13:38