1

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
3
  • How about you provide the full error message and the code around the location of the error. Commented Mar 24, 2014 at 22:51
  • check $entidade variable i think object not correctly passed use serialize and unserialize Commented Mar 24, 2014 at 22:51
  • @Phil I've added the full error message as requested. Commented Mar 25, 2014 at 13:36

1 Answer 1

2

This is a namespace issue. When using variables for class names, you need to provide the full class name including namespace.

Assuming all your Cliente, Fornecedor, Grupo, etc classes are in the same namespace (probably AGC\Bundle\AGCPedidosBundle\Entity), you can probably get away with something like this...

private function atualizaDB($arquivo, $entidade) {
    $entidade = '\\AGC\\Bundle\\AGCPedidosBundle\\Entity\\'.$entidade;

    // and the rest...
}
Sign up to request clarification or add additional context in comments.

6 Comments

This doesn't worked. That was the error message right now. Class 'AGC\Bundle\AGCPedidosBundle\Entity\\AGC\Bundle\AGCPedidosBundle\Entity\Cliente' does not exist
@Danniel could you edit your question to include any updated code?
thanks for the help. I have just changed what you have suggested. I have prepended the namespace to $entidade.
@DannielMagno Like I said, update the code in your question. The error message makes it look like your prepending the namespace incorrectly and / or twice
thanks for the reply. I revised the code, using your instructions, and it worked very well. Thanks for the help.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.