php - Call static method from variable class -


i have methods same thing, different files , entities, need persisted on database.

these methods calls static method on each entity. static method reads string , return entity object properties set string.

to automate process abstracted repetitive work method(atualizadb) , call method passing argument differences between each function.

the problem happens when atualizadb tries call static method, returning fatalerrorexception: error: class not found. can correct error?

thanks.

<?php  namespace agc\bundle\agcpedidosbundle\controller;  use symfony\bundle\frameworkbundle\controller\controller; use symfony\component\httpfoundation\response;  class atualizacaocontroller extends controller {     private function atualizadb($arquivo, $entidade)     {         // arquivo de atualização         $arquivotxt = rtrim($this->container->getparameter('files_dir'), ' '.directory_separator).directory_separator.$arquivo.'.txt';          // verifica se o arquivo existe         if (!is_file($arquivotxt)) {             return new response('erro: arquivo "'.$arquivotxt.'" não encontrado!', 404, array('content-type'=>'text/plain'));         } else {             // abrimos o arquivo             $arquivotxtarr = file($arquivotxt);              // verifica se conseguiu abrir o arquivo             if ($arquivotxtarr === false) {                 return new response('erro: falha ao abrir o arquivo "'.$arquivotxt.'".', 500, array('content-type'=>'text/plain'));             } else {                 try {                     // percorre linhas arquivo                     ($i=2; $i<count($arquivotxtarr); $i++) {                         // mapea o objeto à partir da linha arquivo                         $registroobj = $entidade::fromtxt($arquivotxtarr[$i], $this->getdoctrine());                          // procura o registro no banco de dados                         $registrofromdb = $this->getdoctrine()->getrepository('agcpedidosbundle:'.$entidade)->findoneby(array('codigo'=>$registroobj->getcodigo()));                          // achou o registro?                         if ($registrofromdb === null) {                             // adiciona um novo registro ao banco de dados                             $this->getdoctrine()->getmanager()->persist($registroobj);                         } else {                             // atualiza o registro no banco de dados                             $registrofromdb->updateobject($registroobj);                              // persiste os dados                             $this->getdoctrine()->getmanager()->persist($registrofromdb);                         }                     }                      // da commit nos dados                     $this->getdoctrine()->getmanager()->flush();                      // responde com sucesso                     return new response('ok', 200, array('content-type'=>'text/plain'));                 } catch (\exception $e) {                     throw new \exception($e->getmessage(), $e->getcode(), $e->getprevious());                 }             }         }     }       public function clientesaction()     {         return $this->atualizadb('nclientes', 'cliente');     }      public function fornecedoresaction()     {         return $this->atualizadb('nfornecedores', 'fornecedor');     }      public function gruposaction()     {         return $this->atualizadb('ngrupo', 'grupo');     }      public function pedidosaction()     {         return $this->atualizadb('npedidos', 'pedido');     }      public function vendedoresaction()     {         return $this->atualizadb('nvendedores', 'vendedor');     } } 

edited include full error returned without prepend namespace class name:

1/1 fatalerrorexception: error: class 'cliente' not found in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/src/agc/bundle/agcpedidosbundle/controller/atualizacaocontroller.php line 33 in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/src/agc/bundle/agcpedidosbundle/controller/atualizacaocontroller.php line 33 @ errorhandler->handlefatal() in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/vendor/symfony/symfony/src/symfony/component/debug/errorhandler.php line 0 @ atualizacaocontroller->atualizadb() in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/src/agc/bundle/agcpedidosbundle/controller/atualizacaocontroller.php line 65 @ atualizacaocontroller->clientesaction() in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/app/bootstrap.php.cache line 2843 @ ??call_user_func_array() in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/app/bootstrap.php.cache line 2843 @ httpkernel->handleraw() in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/app/bootstrap.php.cache line 2817 @ httpkernel->handle() in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/app/bootstrap.php.cache line 2946 @ containerawarehttpkernel->handle() in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/app/bootstrap.php.cache line 2248 @ kernel->handle() in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/web/app_dev.php line 28 @ ??{main}() in /users/dennyloko/zend/workspaces/defaultworkspace/agc_pedidos/web/app_dev.php line 0 

this namespace issue. when using variables class names, need provide full class name including namespace.

assuming cliente, fornecedor, grupo, etc classes in same namespace (probably agc\bundle\agcpedidosbundle\entity), can away this...

private function atualizadb($arquivo, $entidade) {     $entidade = '\\agc\\bundle\\agcpedidosbundle\\entity\\'.$entidade;      // , rest... } 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -