php - 找不到目标实体 - MappingException

标签 php symfony doctrine-orm

我有 Symfony 项目,其中有 2 个实体 - Building 和 Building_type。它们与 ManyToMany 单向关联相连。因此,当我尝试访问我的 Controller 时,我遇到了这个错误:

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

Farpost/StoreBundle/Entity/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/Entity/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/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 doctrine: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

关于php - 找不到目标实体 - MappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24745924/

相关文章:

javascript - Vuejs 动态切换类在 Laravel Blade 模板中不起作用

php - Mysql SELECT查询HAVING与INNER JOIN WHERE相结合不显示所有请求的数据

symfony - Doctrine2 findBy() 按连接表字段排序

使用 Capifony 部署 Symfony2 项目时出现 PHP fatal error

symfony - 调用未定义函数 Doctrine\ORM\Mapping\Driver\simplexml_load_file()

php - 如何在 Laravel 中翻译路由?

php - Symfony2 : How to inject ALL parameters in a service?

php - Symfony2 - 使用 Doctrine 存储库的选择约束

symfony2 邻接列表模型与 Forms 一起使用

php - 将变量发送到另一个应用程序并从中获取响应?