I have some methods which do the same thing, but with different files and entities, which need to be persisted on the database.
These methods calls an static method on each entity. This static method reads an string and return the entity object with the properties set from the string.
To automate this process I abstracted the repetitive work with a method(atualizaDB) and I call this method passing by argument the differences between each function.
The problem happens when the atualizaDB tries to call the static method, returning FatalErrorException: Error: Class not found. What can I do to correct this 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 as linhas do arquivo
for ($i=2; $i<count($arquivoTXTArr); $i++) {
// Mapea o objeto à partir da linha do 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 to include the full error returned without prepend the namespace to the 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
at ErrorHandler->handleFatal() in /Users/DennyLoko/Zend/workspaces/DefaultWorkspace/agc_pedidos/vendor/symfony/symfony/src/Symfony/Component/Debug/ErrorHandler.php line 0
at AtualizacaoController->atualizaDB() in /Users/DennyLoko/Zend/workspaces/DefaultWorkspace/agc_pedidos/src/AGC/Bundle/AGCPedidosBundle/Controller/AtualizacaoController.php line 65
at AtualizacaoController->clientesAction() in /Users/DennyLoko/Zend/workspaces/DefaultWorkspace/agc_pedidos/app/bootstrap.php.cache line 2843
at ??call_user_func_array() in /Users/DennyLoko/Zend/workspaces/DefaultWorkspace/agc_pedidos/app/bootstrap.php.cache line 2843
at HttpKernel->handleRaw() in /Users/DennyLoko/Zend/workspaces/DefaultWorkspace/agc_pedidos/app/bootstrap.php.cache line 2817
at HttpKernel->handle() in /Users/DennyLoko/Zend/workspaces/DefaultWorkspace/agc_pedidos/app/bootstrap.php.cache line 2946
at ContainerAwareHttpKernel->handle() in /Users/DennyLoko/Zend/workspaces/DefaultWorkspace/agc_pedidos/app/bootstrap.php.cache line 2248
at Kernel->handle() in /Users/DennyLoko/Zend/workspaces/DefaultWorkspace/agc_pedidos/web/app_dev.php line 28
at ??{main}() in /Users/DennyLoko/Zend/workspaces/DefaultWorkspace/agc_pedidos/web/app_dev.php line 0