本文介绍了在 - MappingException 中找不到目标实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Symfony 项目,其中有 2 个实体 - Building 和 Building_type.它们与 ManyToMany 单向关联连接.因此,当我尝试访问我的控制器时,出现以下错误:

I have Symfony project, in which there are 2 entities - Building and Building_type. They are connected with ManyToMany uni-directional association. So, when I try to access my controller, I have this error:

The target-entity FarpostStoreBundleEntityBuilding_type cannot be found in 'FarpostStoreBundleEntityBuilding#building_types'.

Farpost/StoreBundle/Entity/Building.php:

Farpost/StoreBundle/Entity/Building.php:

    namespace FarpostStoreBundleEntity;

    use DoctrineORMMapping as ORM;
    use DoctrineCommonCollectionsArrayCollection;

    /**
     * @ORMEntity
     *
     */
    class Building
    {
       /**
        * @var integer
        *
        * @ORMColumn(name="id", type="integer")
        * @ORMId
        * @ORMGeneratedValue(strategy="AUTO")
        */
       protected $id;

       /**
        * @var string
        *
        * @ORMColumn(name="alias", type="string", length=255)
        */
       protected $alias;

       /**
        * @var string
        *
        * @ORMColumn(name="number", type="string", length=255)
        */
       protected $number;

       /**
        * @ORMManyToMany(targetEntity="Building_type")
        * @ORMJoinTable(name="buildings_types",
        * joinColumns={@ORMJoinColumn(name="building_id", referencedColumnName="id")},
        * inverseJoinColumns={@ORMJoinColumn(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 FarpostStoreBundleEntityBuilding_type $buildingTypes
        * @return Building
        */
       public function addBuildingType(FarpostStoreBundleEntityBuilding_type $buildingTypes)
       {
          $this->building_types[] = $buildingTypes;
          return $this;
       }

       /**
        * Remove building_types
        *
        * @param FarpostStoreBundleEntityBuilding_type $buildingTypes
        */
       public function removeBuildingType(FarpostStoreBundleEntityBuilding_type $buildingTypes)
       {
          $this->building_types->removeElement($buildingTypes);
       }

       /**
        * Get building_types
        *
        * @return DoctrineCommonCollectionsCollection
        */
       public function getBuildingTypes()
       {
          return $this->building_types;
       }

       /**
        * Add buildings_types
        *
        * @param FarpostStoreBundleEntityBuildings_types $buildingsTypes
        * @return Building
        */
       public function addBuildingsType(FarpostStoreBundleEntityBuildings_types $buildingsTypes)
       {
          $this->buildings_types[] = $buildingsTypes;
          return $this;
       }

       /**
        * Remove buildings_types
        *
        * @param FarpostStoreBundleEntityBuildings_types $buildingsTypes
        */
       public function removeBuildingsType(FarpostStoreBundleEntityBuildings_types $buildingsTypes)
       {
          $this->buildings_types->removeElement($buildingsTypes);
       }

       /**
        * Get buildings_types
        *
        * @return DoctrineCommonCollectionsCollection
        */
       public function getBuildingsTypes()
       {
          return $this->buildings_types;
       }
    }

Farpost/StoreBundle/Entity/Building_type.php:

Farpost/StoreBundle/Entity/Building_type.php:

    namespace FarpostStoreBundleEntity;

    use DoctrineORMMapping as ORM;

    /**
     *
     * @ORMEntity
     *
     */
    class Building_type
    {
       /**
        * @var integer
        *
        * @ORMColumn(name="id", type="integer")
        * @ORMId
        * @ORMGeneratedValue(strategy="AUTO")
        */
       protected $id;

       /**
        * @var string
        *
        * @ORMColumn(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/Controller/DefaultController.php:

Farpost/APIBundle/Controller/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 输出是:

Also, app/console doctrine:schema:validate output is:

    [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.

Because the name of the entity contains an underscore _ and the PSR-0 autoloader will try to find it in Farpost/StoreBundle/Entity/Building/type.php.

您需要将您的类重命名为BuildingType 并将其放入Farpost/StoreBundle/Entity/BuildingType.php

You need to rename your class to BuildingType and put it in Farpost/StoreBundle/Entity/BuildingType.php

这篇关于在 - MappingException 中找不到目标实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 17:30