在- MappingException中找不到目标实体


The target-entity cannot be found in - MappingException

我有Symfony项目,其中有2个实体- Building和Building_type。它们与许多许多单向关联相连。所以,当我试图访问我的控制器时,我有这个错误:

The target-entity Farpost'StoreBundle'Entity'Building_type cannot be found in 'Farpost'StoreBundle'Entity'Building#building_types'.

Farpost/StoreBundle/实体/Building.php:

    namespace Farpost'StoreBundle'Entity;
    use Doctrine'ORM'Mapping as ORM;
    use Doctrine'Common'Collections'ArrayCollection;
    /**
     * @ORM'Entity
     *
     */
    class Building
    {
       /**
        * @var integer
        *
        * @ORM'Column(name="id", type="integer")
        * @ORM'Id
        * @ORM'GeneratedValue(strategy="AUTO")
        */
       protected $id;
       /**
        * @var string
        *
        * @ORM'Column(name="alias", type="string", length=255)
        */
       protected $alias;
       /**
        * @var string
        *
        * @ORM'Column(name="number", type="string", length=255)
        */
       protected $number;
       /**
        * @ORM'ManyToMany(targetEntity="Building_type")
        * @ORM'JoinTable(name="buildings_types",
        * joinColumns={@ORM'JoinColumn(name="building_id", referencedColumnName="id")},
        * inverseJoinColumns={@ORM'JoinColumn(name="building_type_id", referencedColumnName="id")}
        * )
        */
       protected $building_types;
       public function __construct()
       {
          $this->building_types = new ArrayCollection();
       }

       /**
        * Get id
        *
        * @return integer
        */
       public function getId()
       {
          return $this->id;
       }
       /**
        * Set alias
        *
        * @param string $alias
        * @return Building
        */
       public function setAlias($alias)
       {
          $this->alias = $alias;
          return $this;
       }
       /**
        * Get alias
        *
        * @return string
        */
       public function getAlias()
       {
          return $this->alias;
       }
       /**
        * Set number
        *
        * @param string $number
        * @return Building
        */
       public function setNumber($number)
       {
          $this->number = $number;
          return $this;
       }
       /**
        * Get number
        *
        * @return string
        */
       public function getNumber()
       {
          return $this->number;
       }
       /**
        * Add building_types
        *
        * @param 'Farpost'StoreBundle'Entity'Building_type $buildingTypes
        * @return Building
        */
       public function addBuildingType('Farpost'StoreBundle'Entity'Building_type $buildingTypes)
       {
          $this->building_types[] = $buildingTypes;
          return $this;
       }
       /**
        * Remove building_types
        *
        * @param 'Farpost'StoreBundle'Entity'Building_type $buildingTypes
        */
       public function removeBuildingType('Farpost'StoreBundle'Entity'Building_type $buildingTypes)
       {
          $this->building_types->removeElement($buildingTypes);
       }
       /**
        * Get building_types
        *
        * @return 'Doctrine'Common'Collections'Collection
        */
       public function getBuildingTypes()
       {
          return $this->building_types;
       }
       /**
        * Add buildings_types
        *
        * @param 'Farpost'StoreBundle'Entity'Buildings_types $buildingsTypes
        * @return Building
        */
       public function addBuildingsType('Farpost'StoreBundle'Entity'Buildings_types $buildingsTypes)
       {
          $this->buildings_types[] = $buildingsTypes;
          return $this;
       }
       /**
        * Remove buildings_types
        *
        * @param 'Farpost'StoreBundle'Entity'Buildings_types $buildingsTypes
        */
       public function removeBuildingsType('Farpost'StoreBundle'Entity'Buildings_types $buildingsTypes)
       {
          $this->buildings_types->removeElement($buildingsTypes);
       }
       /**
        * Get buildings_types
        *
        * @return 'Doctrine'Common'Collections'Collection
        */
       public function getBuildingsTypes()
       {
          return $this->buildings_types;
       }
    }

Farpost/StoreBundle/实体/Building_type.php:

    namespace Farpost'StoreBundle'Entity;
    use Doctrine'ORM'Mapping as ORM;
    /**
     *
     * @ORM'Entity
     *
     */
    class Building_type
    {
       /**
        * @var integer
        *
        * @ORM'Column(name="id", type="integer")
        * @ORM'Id
        * @ORM'GeneratedValue(strategy="AUTO")
        */
       protected $id;
       /**
        * @var string
        *
        * @ORM'Column(name="alias", type="string", length=255)
        */
       protected $alias;
       /**
        * Get id
        *
        * @return integer
        */
       public function getId()
       {
          return $this->id;
       }
       /**
        * Set alias
        *
        * @param string $alias
        * @return Building_type
        */
       public function setAlias($alias)
       {
          $this->alias = $alias;
          return $this;
       }
       /**
        * Get alias
        *
        * @return string
        */
       public function getAlias()
       {
          return $this->alias;
       }
    }

Farpost/APIBundle/控制器/DefaultController.php:

       public function listAction($name)
       {
          $repository = $this->getDoctrine()->getManager()
             ->getRepository('FarpostStoreBundle:Building');
          $items = $repository->findAll();
          $response = new Response(json_encode($items));
          $response->headers->set('Content-Type', 'application/json');
          return $response;
       }

同样,app/console原则:schema:validate输出是:

    [Mapping]  OK - The mapping files are correct.
    [Database] OK - The database schema is in sync with the mapping files.

因为实体的名称包含下划线_, PSR-0自动加载程序将尝试在Farpost/StoreBundle/Entity/Building/type.php中找到它。

你需要重命名你的类为BuildingType,并把它放在Farpost/StoreBundle/Entity/BuildingType.php

另外,确保您的composer.json具有指向正确名称空间的正确条目。