Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

php - The target-entity cannot be found in - MappingException

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:

    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:

    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:

       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;
       }

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.
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...