-->

symfony2 is not a valid entity or mapped super cla

2019-04-17 09:22发布

问题:

I have a symfony2 app, in my local pc with ubuntu and works well, but in server with CentOS and Cpanel not works, I obtained this error:

Class "Propa\PageBundle\Entity\Page" is not a valid entity or mapped super class.

CRITICAL - Doctrine\ORM\Mapping\MappingException: Class "Propa\PageBundle\Entity\Page" is not a valid entity or mapped super class. (uncaught exception) at /home/estudi83/domains/serverprova.com.es/propa/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php line 216

I prove:

$page=$em->getRepository('PropaPageBundle:Page')->findOneBy(array('codi'=>'0001'));

And obtain this error: Unknown Entity namespace alias 'PropaPageBundle'.

And I prove:

 $page=$em->getRepository('Propa\PageBundle\Entity\Page')->findOneBy(array('codi'=>'0001'));

And obtain same error: Class "Propa\PageBundle\Entity\Page" is not a valid entity or mapped super class.

This is the Entity:

<?php
namespace Propa\PageBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Propa\PageBundle\Util;
use Gedmo\Mapping\Annotation as Gedmo;




/**
 * @ORM\Entity(repositoryClass="Propa\PageBundle\Repository\PageRepository")
 * @Gedmo\TranslationEntity(class="Propa\PageBundle\Entity\Translation\PageTranslation")
 * @ORM\HasLifecycleCallbacks
 * @Vich\Uploadable
 */

class Page {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    protected $id;

I had proved:

auto_mapping: true

and

auto_mapping: false
mappings:
    PageBundle: ~

But always obtain the same error.

What Can I do?

UPDATE: Resolved the problem was in eAccelerator with symfony2 annotations, I'm uninstalled eAccelerator and installed APC and works fine!!

回答1:

Finally, it may be a cache optimizer issue. If you are using eAccelerator, then you will have problems, probably with Doctrine. The reason is eAccelerator removes annotations from the code! You can try APC which performs well with doctrine.

Read apc vs eaccelerator vs xcache


Have a look at

http://you.site.name/config.php

It shows you the requirements that should be present to make your application work.

AND

please expose your page entity class.


There may be many reasons for this error and your question was already asked here on SO "Class XXX is not a valid entity or mapped super class" after moving the class in the filesystem

https://github.com/symfony/symfony/issues/4554


$page=$em->getRepository('PageBundle:Page')->findOneBy(array('codi'=>'0001'));

change it to

 $page=$em->getRepository('PropaPageBundle:Page')->findOneBy(array('codi'=>'0001'));

or

 $page=$em->getRepository('Propa\PageBundle\Entity\Page')->findOneBy(array('codi'=>'0001'));

and see if it works.