4

So I seem to be plagued by issues that only affect production mode in Symfony and not developer mode. This time, I have a ManyToOne association and I'm trying to fetch only the entities which do not have an association (i.e. they have a NULL value in the database). This works exactly as I'd expect in dev move, but in prod mode, Doctrine throws an "unrecognized field" exception... for a field which absolutely does exist.

Here's the relevant part of the entity in question (Page.php):

/**
 * @ORM\ManyToOne(targetEntity="Project", inversedBy="pages")
 * @ORM\JoinColumn(name="project_id", referencedColumnName="ID")
 */
protected $project;

And here is the relevant line from the controller (PageController.php):

$pages = $this->getDoctrine()->getRepository('JCScopingBundle:Page')->findBy(['project' => null]);

Again, this works perfectly using app_dev.php (i.e. dev mode), but using app.php (i.e. prod mode) I keep getting the "unrecognized field" exception. What gives?

Update: I added a "weight" integer field to the same entity and that field is not recognized in prod mode either. This means I can't use prod mode, which means I can't upload my changes to the remote server. Really in a pickle here...

willherzog
  • 61
  • 1
  • 9
  • 2
    If something works in `dev` environment and not in `prod` it's being cached most of the time. Did you `cache:clear --env=prod --no-debug` ? – ccKep Nov 04 '17 at 02:37
  • Well to be honest on my local machine it's usually easier to just delete the `prod` cache folder through the GUI file system and then reload the page. I've done this more than once and gotten the same result. The last time I had a `prod`-only issue it was a bug in Symfony that was eventually fixed by an official patch. – willherzog Nov 06 '17 at 19:11
  • I just tried the commandline method and got the same result. – willherzog Nov 06 '17 at 19:17
  • Can you post the **exact, complete** error symfony throws? Also: Does this happen with custom queries aswell? (eg. `createQuery("SELECT page FROM JCScopingBundle:Page page WHERE p.project IS NULL")` ?) – ccKep Nov 06 '17 at 22:59
  • `request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\ORMException: "Unrecognized field: project" at ..\vendor\doctrine\orm\lib\Doctrine\ORM\ORMException.php line 101 {"exception":"[object] (Doctrine\\ORM\\ORMException(code: 0): Unrecognized field: project at ..\\vendor\\doctrine\\orm\\lib\\Doctrine\\ORM\\ORMException.php:101)"}` – willherzog Nov 07 '17 at 23:27
  • And yes, it is also happening with a custom query (I'm using a custom query to fetch all the pages with a weight value greater than 0). – willherzog Nov 07 '17 at 23:28
  • This is the error I get for said custom query (had to strip off the repeated portion so it wouldn't be too long for a comment): `request.CRITICAL: Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Semantical Error] line 0, col 53 near 'weight > 0 ORDER': Error: Class JordanCrown\Scoping\Entity\Page has no field or association named weight" at ..\vendor\doctrine\orm\lib\Doctrine\ORM\Query\QueryException.php line 63` – willherzog Nov 07 '17 at 23:33

2 Answers2

1

Well, low and behold, restarting the Apache service resolved the issue. Apparently that's the only way to truly clear the APCu metadata cache. I was inspired to try this based on this question/answer: Doctrine mapped field is not working

willherzog
  • 61
  • 1
  • 9
0

In my case I forgot to restart 'memcached' service.

Check metadata_cache_driver type. In my case:

doctrine:
    orm:
        metadata_cache_driver:
            type: memcached
            host: localhost
            port: 11211
            instance_class: Memcached

Because previous metadata was cached, after applying migration, doctrine used that old cached metadata, without knowing about added new field.

Gregsparrow
  • 1,332
  • 1
  • 11
  • 13
  • I use APCu for each of my cache drivers, but I haven't had this issue after applying migrations in the past. – willherzog Nov 07 '17 at 23:24
  • Commenting out the `metadata_cache_driver` line and clearing the `prod` cache again does result in the page loading without an error, but I can't figure out the right CLI command to clear the Doctrine metadata cache (it gives me the error "can't find provider" even if I specify `prod` as the environment). – willherzog Nov 07 '17 at 23:45
  • Alright `doctrine:cache:clear-metadata` seems to be the right command, but after using it and clearing the `prod` cache I'm still getting the errors. – willherzog Nov 07 '17 at 23:53