laminas-api-tools / api-tools

Laminas API Tools module for Laminas
https://api-tools.getlaminas.org/documentation
BSD 3-Clause "New" or "Revised" License
37 stars 19 forks source link

ZF gets trapped in symfony entity error even after error is solved #65

Open michalbundyra opened 4 years ago

michalbundyra commented 4 years ago

We're building an API over a symfony2 app. The API invokes symofiny services which return symfony entities.

The thing is working ok, but after a runtime error in the symfony app, calls to API endpoints keep returning an entity annotation error even when the error is solved. After some days getting this error I think the message is actually not relevant, it was only a simple error in the entity

        {
          "file": "/var/www/my-api/public/index.php",
          "line": 38,
          "function": "run",
          "class": "Zend\\Mvc\\Application",
          "type": "->",
          "args": []
        }
      ],
      "type": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html",
      "title": "Internal Server Error",
      "status": 500,
      "detail": "[Semantical Error] The annotation \"@Doctrine\\ORM\\Mapping\\Entity\" in class App\\UserBundle\\Entity\\Usuario does not exist, or could not be auto-loaded."
    }

To reproduce this issue you can force a runtime error in the symfony entity (in my case it was using $var->attr instead of $this->attr), maybe entity needs to be annotated to reproduce same environment.

This is my fetch nethod, the one which keeps returning the error:

    public function fetch($id)
    {
        $usuario =  $this->userServiceSymfony->getUser($this->doctrine, $id);

        // return $usuario->getArrayCopy();

        return array('id' => $usuario->getId(), 
                     'nombre' => $usuario->getNombre(), 
                     'apellidos' => $usuario->getApellidos(),
                     'fechanacimiento' => $usuario->getFechanacimiento(),
                     'telefono' => $usuario->getTelefono(),
                     'campaignid' => $usuario->getCampaignid(),
                     'email' => $usuario->getEmail(),
                     'lastlogin' => $usuario->getLastLogin(),
                     );
    }

Yesterday I was only able to recover from this error with a hard reset of a very previous commit, today, after some improvements I found the error again.


Originally posted by @nzurita at https://github.com/zfcampus/zf-apigility/issues/60

michalbundyra commented 4 years ago

So far we found out we can get around this using this in /public/index.php:

use \Doctrine\Common\Annotations\AnnotationReader;

AnnotationReader::addGlobalIgnoredName('App\UserBundle\Entity\Usuario');

We guess we'll need to add this for all entities that will be used, so anyway it would be nice that someone takes care of this issue (just to remember, there is no annotation error)

EDIT: actually this doesn't work, the problem persists


Originally posted by @nzurita at https://github.com/zfcampus/zf-apigility/issues/60#issuecomment-49547527

michalbundyra commented 4 years ago

In case this is helpful to someone else, I came across this thread when I had a similar issue with annotations not being recognized. I found the problem to be that my AppKernel that was bootstrapped in the Apigility app was assuming a production environment (following this tutorial), so it was likely that my production cache was out of sync with my development cache. To resolve this, I made the AppKernel environment in Apigility match the environment of the existing Symfony2 app, and the error went away. I posted more detail about this in this thread.


Originally posted by @sgilberg at https://github.com/zfcampus/zf-apigility/issues/60#issuecomment-90951068