title: ReflectionException with Symfony and Sonata, "Class does not exist"
body: <p>I'm experiencing a problem trying to fit my Entities in the Sonata Admin Bundle. I have something like 5 entities, which are finely listed and viewed in the bundle, but I cannot edit or create a new entry.</p>
  
  <p>When I try to edit or create, I get a <code>ReflectionException</code> error:</p>
  
  <blockquote>
    <p>Class does not exist</p>
  </blockquote>
  
  <p>I tried in order to solve that problem to operate on namespaces (moving controller in the same namespace that Admin files, or so) or to tweak the Admin Controller in order to tell it about my entities ("->add('individual', 'entity', array('class' => 'Platform\ProjectBundle\Entity\Individual'))" instead of ->add('individual')).</p>
  
  <p>My entity is named Biosample. Here is the Entity file:</p>
  
  <pre><code>&lt;?php
  
  namespace Platform\ProjectBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  
  /**
  * @ORM\Entity
  * @ORM\Table(name="biosample")
  */
  class Biosample
  {
  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
   protected $id;
  
  /**
   * @ORM\ManyToOne(targetEntity="Individual", inversedBy="samples")
   * @ORM\JoinColumn(name="individual_id", referencedColumnName="id")
   */
   protected $individual;
  
  /**
   * @ORM\Column(type="string", length=100)
   */
   protected $organ;
  
   /**
   * @ORM\Column(type="string", length=100)
   */
   protected $sample_name;
  
  /**
   * @ORM\Column(type="string", nullable=true)
   */
   protected $organ_location;
  
  /**
   * @ORM\Column(type="string")
   */
   protected $tissue_type;
  
  /**
   * @ORM\Column(type="string", nullable=true)
   */
   protected $tissue_subtype;
  
  /**
   * @ORM\Column(type="datetimetz")
   */
   protected $sampling_date;
  
  /**
   * @ORM\Column(type="decimal", scale=3, nullable=true)
   */
   protected $cellularity;
  
  /**
   * @ORM\Column(type="string", nullable=true)
   */
   protected $conservation;
  
  /**
   * @ORM\Column(type="text", nullable=true)
   */
   protected $description;
  
  /**
   * @ORM\ManyToMany(targetEntity="Project", mappedBy="biosamples")
   */
   protected $projects;
  
   public function __construct()
   {
        $this-&gt;projects = new ArrayCollection();
   }
  
   public function __toString()
   {
        return $this-&gt;sample_name;
   }
  
  /**
   * Get id
   *
   * @return integer 
   */
  
  public function getId()
  {
      return $this-&gt;id;
  }
  
  /**
   * Set organ
   *
   * @param string $organ
   * @return Biosample
   */
  public function setOrgan($organ)
  {
      $this-&gt;organ = $organ;
  
      return $this;
  }
  
  /**
   * Get organ
   *
   * @return string 
   */
  public function getOrgan()
  {
      return $this-&gt;organ;
  }
  
  /**
   * Set organ_location
   *
   * @param string $organLocation
   * @return Biosample
   */
  public function setOrganLocation($organLocation)
  {
      $this-&gt;organ_location = $organLocation;
  
      return $this;
  }
  
  /**
   * Get organ_location
   *
   * @return string 
   */
  public function getOrganLocation()
  {
      return $this-&gt;organ_location;
  }
  
  /**
   * Set tissue_type
   *
   * @param string $tissueType
   * @return Biosample
   */
  public function setTissueType($tissueType)
  {
      $this-&gt;tissue_type = $tissueType;
  
      return $this;
  }
  
  /**
   * Get tissue_type
   *
   * @return string 
   */
  public function getTissueType()
  {
      return $this-&gt;tissue_type;
  }
  
  /**
   * Set tissue_subtype
   *
   * @param string $tissueSubtype
   * @return Biosample
   */
  public function setTissueSubtype($tissueSubtype)
  {
      $this-&gt;tissue_subtype = $tissueSubtype;
  
      return $this;
  }
  
  /**
   * Get tissue_subtype
   *
   * @return string 
   */
  public function getTissueSubtype()
  {
      return $this-&gt;tissue_subtype;
  }
  
  /**
   * Set sampling_date
   *
   * @param \DateTime $samplingDate
   * @return Biosample
   */
  public function setSamplingDate($samplingDate)
  {
      $this-&gt;sampling_date = $samplingDate;
  
      return $this;
  }
  
  /**
   * Get sampling_date
   *
   * @return \DateTime 
   */
  public function getSamplingDate()
  {
      return $this-&gt;sampling_date;
  }
  
  /**
   * Set cellularity
   *
   * @param string $cellularity
   * @return Biosample
   */
  public function setCellularity($cellularity)
  {
      $this-&gt;cellularity = $cellularity;
  
      return $this;
  }
  
  /**
   * Get cellularity
   *
   * @return string 
   */
  public function getCellularity()
  {
      return $this-&gt;cellularity;
  }
  
  /**
   * Set conservation
   *
   * @param string $conservation
   * @return Biosample
   */
  public function setConservation($conservation)
  {
      $this-&gt;conservation = $conservation;
  
      return $this;
  }
  
  /**
   * Get conservation
   *
   * @return string 
   */
  public function getConservation()
  {
      return $this-&gt;conservation;
  }
  
  /**
   * Set description
   *
   * @param string $description
   * @return Biosample
   */
  public function setDescription($description)
  {
      $this-&gt;description = $description;
  
      return $this;
  }
  
  /**
   * Get description
   *
   * @return string 
   */
  public function getDescription()
  {
      return $this-&gt;description;
  }
  
  /**
   * Set individual
   *
   * @param \Platform\ProjectBundle\Entity\Individual $individual
   * @return Biosample
   */
  public function setIndividual(\Platform\ProjectBundle\Entity\Individual $individual = null)
  {
      $this-&gt;individual = $individual;
  
      return $this;
  }
  
  /**
   * Get individual
   *
   * @return \Platform\ProjectBundle\Entity\Individual 
   */
  public function getIndividual()
  {
      return $this-&gt;individual;
  }
  
  /**
   * Add projects
   *
   * @param \Platform\ProjectBundle\Entity\Project $projects
   * @return Biosample
   */
  public function addProject(\Platform\ProjectBundle\Entity\Project $projects)
  {
      $this-&gt;projects[] = $projects;
  
      return $this;
  }
  
  /**
   * Remove projects
   *
   * @param \Platform\ProjectBundle\Entity\Project $projects
   */
  public function removeProject(\Platform\ProjectBundle\Entity\Project $projects)
  {
      $this-&gt;projects-&gt;removeElement($projects);
  }
  
  /**
   * Get projects
   *
   * @return \Doctrine\Common\Collections\Collection 
   */
  public function getProjects()
  {
      return $this-&gt;projects;
  }
  
  /**
   * Set sample_name
   *
   * @param string $sampleName
   * @return Biosample
   */
  public function setSampleName($sampleName)
  {
      $this-&gt;sample_name = $sampleName;
  
      return $this;
  }
  
  /**
   * Get sample_name
   *
   * @return string 
   */
  public function getSampleName()
  {
      return $this-&gt;sample_name;
  }
  }`
  </code></pre>
  
  <p>Here is my BiosampleAdmin.php:</p>
  
  <pre><code>    &lt;?php
  
  namespace Platform\ProjectBundle\Controller\Admin;
  
  use Sonata\AdminBundle\Admin\Admin;
  use Sonata\AdminBundle\Form\FormMapper;
  use Sonata\AdminBundle\Datagrid\DatagridMapper;
  use Sonata\AdminBundle\Datagrid\ListMapper;
  use Sonata\AdminBundle\Show\ShowMapper;
  
  use Knp\Menu\ItemInterface as MenuItemInterface;
  
  use Platform\ProjectBundle\Entity\Biosample;
  
  class BiosampleAdmin extends Admin
  {
  
      /**
       * @param \Sonata\AdminBundle\Show\ShowMapper $showMapper
       *
       * @return void
       */
      protected function configureShowFields(ShowMapper $showMapper)
      {
          $showMapper
              -&gt;add('id')
              -&gt;add('sample_name')
              -&gt;add('individual', 'entity', array('class' =&gt; 'Platform\ProjectBundle\Entity\Individual'))
              -&gt;add('organ')
              -&gt;add('organ_location')
              -&gt;add('tissue_type')
              -&gt;add('tissue_subtype')
              -&gt;add('sampling_date')
              -&gt;add('cellularity')
              -&gt;add('conservation')
              -&gt;add('projects', 'entity', array('class' =&gt; 'Platform\ProjectBundle\Entity\Project'))
              -&gt;add('description')
          ;
      }
  
  
      /**
       * @param \Sonata\AdminBundle\Form\FormMapper $formMapper
       *
       * @return void
       */
      protected function configureFormFields(FormMapper $formMapper)
      {
          $formMapper
              -&gt;with('General')
                  -&gt;add('sample_name')
                  -&gt;add('individual')
                  -&gt;add('organ')
                  -&gt;add('organ_location')
                  -&gt;add('tissue_type')
                  -&gt;add('tissue_subtype')
              -&gt;end()
              -&gt;with('Miscelaneous')
                  -&gt;add('sampling_date')
                  -&gt;add('cellularity')
                  -&gt;add('conservation')
              -&gt;end()
              -&gt;with('Projects')
                  -&gt;add('projects')
              -&gt;end()
              -&gt;with('Description')
                  -&gt;add('description', 'sonata_type_model', array('multiple' =&gt; false))
              -&gt;end()
          ;
      }
  
      /**
       * @param \Sonata\AdminBundle\Datagrid\ListMapper $listMapper
       *
       * @return void
       */
      protected function configureListFields(ListMapper $listMapper)
      {
          $listMapper
              -&gt;addIdentifier('id')
              -&gt;add('sample_name')
              -&gt;add('individual')
              -&gt;add('projects')
              -&gt;add('organ')
              -&gt;add('tissue')
              -&gt;add('_action', 'actions', array(
                  'actions' =&gt; array(
                      'show' =&gt; array(),
                      'edit' =&gt; array(),
                      'delete' =&gt; array(),
                  )
              ))
          ;
      }
  
  
      /**
       * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $datagridMapper
       *
       * @return void
       */
      protected function configureDatagridFilters(DatagridMapper $datagridMapper)
      {
          $datagridMapper
              -&gt;add('sample_name')
              -&gt;add('individual')
              -&gt;add('projects')
          ;
      }
  }
  </code></pre>
  
  <p>Here is the admin controller:</p>
  
  <pre><code>    &lt;?php
  
  namespace Platform\ProjectBundle\Controller;
  
  use Sonata\AdminBundle\Controller\CRUDController as Controller;
  use Platform\ProjectBundle\Entity\Biosample;
  
  class BiosampleAdminController extends Controller
  {
  
  }
  </code></pre>
  
  <p>And if you need it, here my composer.json:</p>
  
  <pre><code>{
      "name": "symfony/framework-standard-edition",
      "license": "MIT",
      "type": "project",
      "description": "The \"Symfony Standard Edition\" distribution",
      "autoload": {
          "psr-0": { "": "src/", "SymfonyStandard": "app/" }
      },
      "require": {
          "php": "&gt;=5.3.3",
          "symfony/symfony": "2.6.*",
          "doctrine/orm": "~2.2,&gt;=2.2.3,&lt;2.5",
          "doctrine/dbal": "&lt;2.5",
          "doctrine/doctrine-bundle": "~1.2",
          "twig/extensions": "~1.0",
          "symfony/assetic-bundle": "~2.3",
          "symfony/swiftmailer-bundle": "~2.3",
          "symfony/monolog-bundle": "~2.4",
          "sensio/distribution-bundle": "~3.0,&gt;=3.0.12",
          "sensio/framework-extra-bundle": "~3.0,&gt;=3.0.2",
          "jms/security-extra-bundle": "~1.2",
          "ircmaxell/password-compat": "~1.0.3",
          "stof/doctrine-extensions-bundle": "~1.1@dev",
          "friendsofsymfony/user-bundle": "~1.3",
          "incenteev/composer-parameter-handler": "~2.0",
          "nervo/yuicompressor": "2.4.8",
          "sonata-project/admin-bundle": "~2.3",
          "sonata-project/doctrine-orm-admin-bundle": "~2.3",
          "sonata-project/easy-extends-bundle": "~2.1",
          "sonata-project/user-bundle": "~2.2",
          "knplabs/knp-menu-bundle": "~1.1",
          "mopa/bootstrap-bundle": "~2",
          "twbs/bootstrap-sass": "~3.3.0",
          "knplabs/knp-paginator-bundle": "dev-master",
          "knplabs/knp-menu": "~1.1",
          "craue/formflow-bundle": "~2.0"
      },
      "require-dev": {
          "sensio/generator-bundle": "~2.3"
      },
      "scripts": {
          "post-root-package-install": [
              "SymfonyStandard\\Composer::hookRootPackageInstall"
          ],
          "post-install-cmd": [
              "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles"
          ],
          "post-update-cmd": [
              "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
              "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::removeSymfonyStandardFiles"
          ]
      },
      "config": {
          "bin-dir": "bin"
      },
      "extra": {
          "symfony-app-dir": "app",
          "symfony-web-dir": "web",
          "symfony-assets-install": "relative",
          "incenteev-parameters": {
              "file": "app/config/parameters.yml"
          },
          "branch-alias": {
              "dev-master": "2.6-dev"
          }
      }
  }
  </code></pre>
  
  <p>Finally, here's my service declaration and my config.yml file:
  Service.yml:</p>
  
  <pre><code>services:
      platform.project.admin.biosample:
          class: Platform\ProjectBundle\Controller\Admin\BiosampleAdmin
          tags:
              - { name: sonata.admin, manager_type: orm, group: Project Manager, label: Biosample }
          arguments: [null, Platform\ProjectBundle\Entity\Biosample, PlatformProjectBundle:BiosampleAdmin]
  </code></pre>
  
  <p>Config.yml: </p>
  
  <pre><code>imports:
      - { resource: parameters.yml }
      - { resource: security.yml }
      - { resource: services.yml }
  
  framework:
      #esi:             ~
      translator:      { fallbacks: ["%locale%"] }
      secret:          "%secret%"
      router:
          resource: "%kernel.root_dir%/config/routing.yml"
          strict_requirements: ~
      form:            ~
      csrf_protection: ~
      validation:      { enable_annotations: true }
      templating:
          engines: ['twig']
          #assets_version: SomeVersionScheme
      default_locale:  "%locale%"
      trusted_hosts:   ~
      trusted_proxies: ~
      session:
          # handler_id set to null will use default session handler from php.ini
          handler_id:  ~
      fragments:       ~
      http_method_override: true
  
  # Twig Configuration
  twig:
      debug:            "%kernel.debug%"
      strict_variables: "%kernel.debug%"
  
  # Assetic Configuration
  assetic:
      debug:          "%kernel.debug%"
      use_controller: false
      bundles:        [ ]
      #java: /usr/bin/java
      filters:
          cssrewrite: ~
          #closure:
          #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
          #yui_css:
          #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
  
  # Doctrine Configuration
  doctrine:
      dbal:
          driver:   "%database_driver%"
          host:     "%database_host%"
          port:     "%database_port%"
          dbname:   "%database_name%"
          user:     "%database_user%"
          password: "%database_password%"
          charset:  UTF8
          # if using pdo_sqlite as your database driver:
          #   1. add the path in parameters.yml
          #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
          #   2. Uncomment database_path in parameters.yml.dist
          #   3. Uncomment next line:
          #     path:     "%database_path%"
          types:
              json: Sonata\Doctrine\Types\JsonType
      orm:
          auto_generate_proxy_classes: "%kernel.debug%"
          auto_mapping: true
  
  # Swiftmailer Configuration
  swiftmailer:
      transport: "%mailer_transport%"
      host:      "%mailer_host%"
      username:  "%mailer_user%"
      password:  "%mailer_password%"
      spool:     { type: memory }
  
  
  fos_user:
      db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
      firewall_name: secured
      user_class: Application\Sonata\UserBundle\Entity\User
      group:
          group_class: Application\Sonata\UserBundle\Entity\Group
          group_manager: sonata.user.orm.group_manager
  
      service:
          user_manager: sonata.user.orm.user_manager
  
  
  sonata_doctrine_orm_admin:
      entity_manager: ~
  
  sonata_block:
      default_contexts: [cms]
      blocks:
          sonata.admin.block.admin_list:
              contexts: [admin]
          sonata.user.block.menu: ~
          sonata.user.block.account: ~
          sonata.block.service.text: ~
  
  
  sonata_user:
      security_acl: true
      manager_type: orm
  
  mopa_bootstrap:
      form: ~
  </code></pre>
  
  <p>And last but not least: the full stack trace.</p>
  
  <pre><code>[1] ReflectionException: Class  does not exist
      at n/a
          in /var/www/Project/app/cache/dev/classes.php line 6756
  
      at ReflectionClass-&gt;__construct('')
          in /var/www/Project/app/cache/dev/classes.php line 6756
  
      at Doctrine\Common\Persistence\AbstractManagerRegistry-&gt;getManagerForClass(null)
          in /var/www/Project/vendor/sonata-project/doctrine-orm-admin-bundle/Model/ModelManager.php line 220
  
      at Sonata\DoctrineORMAdminBundle\Model\ModelManager-&gt;getEntityManager(null)
          in /var/www/Project/vendor/sonata-project/doctrine-orm-admin-bundle/Model/ModelManager.php line 54
  
      at Sonata\DoctrineORMAdminBundle\Model\ModelManager-&gt;getMetadata(null)
          in /var/www/Project/vendor/sonata-project/doctrine-orm-admin-bundle/Model/ModelManager.php line 317
  
      at Sonata\DoctrineORMAdminBundle\Model\ModelManager-&gt;getIdentifierFieldNames(null)
          in /var/www/Project/app/cache/dev/classes.php line 12663
  
      at Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList-&gt;__construct(object(ModelManager), null, null, null, null)
          in /var/www/Project/app/cache/dev/classes.php line 13690
  
      at Sonata\AdminBundle\Form\Type\ModelType-&gt;Sonata\AdminBundle\Form\Type\{closure}(object(OptionsResolver), object(SimpleChoiceList))
          in /var/www/Project/vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php line 836
  
      at Symfony\Component\OptionsResolver\OptionsResolver-&gt;offsetGet('choice_list')
          in /var/www/Project/vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php line 769
  
      at Symfony\Component\OptionsResolver\OptionsResolver-&gt;resolve(array('sonata_field_description' =&gt; object(FieldDescription), 'class' =&gt; null, 'model_manager' =&gt; object(ModelManager), 'multiple' =&gt; false, 'label_render' =&gt; false, 'label' =&gt; 'Description'))
          in /var/www/Project/vendor/symfony/symfony/src/Symfony/Component/Form/ResolvedFormType.php line 109
  
      at Symfony\Component\Form\ResolvedFormType-&gt;createBuilder(object(FormFactory), 'description', array('sonata_field_description' =&gt; object(FieldDescription), 'class' =&gt; null, 'model_manager' =&gt; object(ModelManager), 'multiple' =&gt; false, 'label_render' =&gt; false, 'label' =&gt; 'Description'))
          in /var/www/Project/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/DataCollector/Proxy/ResolvedTypeDataCollectorProxy.php line 82
  
      at Symfony\Component\Form\Extension\DataCollector\Proxy\ResolvedTypeDataCollectorProxy-&gt;createBuilder(object(FormFactory), 'description', array('sonata_field_description' =&gt; object(FieldDescription), 'class' =&gt; null, 'model_manager' =&gt; object(ModelManager), 'multiple' =&gt; false, 'label_render' =&gt; false, 'label' =&gt; 'Description'))
          in /var/www/Project/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 87
  
      at Symfony\Component\Form\FormFactory-&gt;createNamedBuilder('description', 'sonata_type_model', null, array('sonata_field_description' =&gt; object(FieldDescription), 'class' =&gt; null, 'model_manager' =&gt; object(ModelManager), 'multiple' =&gt; false, 'label_render' =&gt; false, 'label' =&gt; 'Description'))
          in /var/www/Project/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 106
  
      at Symfony\Component\Form\FormBuilder-&gt;create('description', 'sonata_type_model', array('sonata_field_description' =&gt; object(FieldDescription), 'class' =&gt; null, 'model_manager' =&gt; object(ModelManager), 'multiple' =&gt; false, 'label_render' =&gt; false, 'label' =&gt; 'Description'))
          in /var/www/Project/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 268
  
      at Symfony\Component\Form\FormBuilder-&gt;resolveChildren()
          in /var/www/Project/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 216
  
      at Symfony\Component\Form\FormBuilder-&gt;getForm()
          in /var/www/Project/app/cache/dev/classes.php line 9671
  
      at Sonata\AdminBundle\Admin\Admin-&gt;buildForm()
          in /var/www/Project/app/cache/dev/classes.php line 9930
  
      at Sonata\AdminBundle\Admin\Admin-&gt;getForm()
          in /var/www/Project/vendor/sonata-project/admin-bundle/Controller/CRUDController.php line 353
  
      at Sonata\AdminBundle\Controller\CRUDController-&gt;editAction('1')
          in  line 
  
      at call_user_func_array(array(object(CRUDController), 'editAction'), array('1'))
          in /var/www/Project/app/bootstrap.php.cache line 3022
  
      at Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(object(Request), '1')
          in /var/www/Project/app/bootstrap.php.cache line 2984
  
      at Symfony\Component\HttpKernel\HttpKernel-&gt;handle(object(Request), '1', true)
          in /var/www/Project/app/bootstrap.php.cache line 3133
  
      at Symfony\Component\HttpKernel\DependencyInjection\ContainerAwareHttpKernel-&gt;handle(object(Request), '1', true)
          in /var/www/Project/app/bootstrap.php.cache line 2377
  
      at Symfony\Component\HttpKernel\Kernel-&gt;handle(object(Request))
          in /var/www/Project/web/app_dev.php line 28
  </code></pre>