- MappingException中找不到目标实体 [英] The target-entity cannot be found in - MappingException

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

问题描述

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

 目标实体Farpost\StoreBundle\Entity Farpost\StoreBundle\Entity\Building#build_types中找不到\Building_type。 

Farpost / StoreBundle / Entity / Building.php:


 命名空间Farpost\StoreBundle\Entity; 

使用Doctrine\ORM\Mapping作为ORM;
使用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();
}


/ **
*获取ID
*
* @return integer
* /
public function getId()
{
return $ this-> id;
}

/ **
*设置别名
*
* @param string $ alias
* @return构建
* /
public function setAlias($ alias)
{
$ this-> alias = $ alias;
return $ this;
}

/ **
*获取别名
*
* @return string
* /
public function getAlias )
{
return $ this->别名;
}

/ **
*设置号码
*
* @param string $ number
* @return构建
* /
public function setNumber($ number)
{
$ this-> number = $ number;
return $ this;
}

/ **
*获取号码
*
* @return string
* /
public function getNumber )
{
return $ this-> number;
}

/ **
*添加building_types
*
* @param \Farpost\StoreBundle\Entity\Building_type $ buildingTypes
* @return建立
* /
public function addBuildingType(\Farpost\StoreBundle\Entity\Building_type $ buildingTypes)
{
$ this-> building_types [] = $ buildingTypes;
return $ this;
}

/ **
*删除building_types
*
* @param \Farpost\StoreBundle\Entity\Building_type $ buildingTypes
* /
public function removeBuildingType(\Farpost\StoreBundle\Entity\Building_type $ buildingTypes)
{
$ this-> building_types-> removeElement($ buildingTypes );
}

/ **
*获取building_types
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getBuildingTypes()
{
return $ this-> building_types;
}

/ **
*添加buildings_types
*
* @param \Farpost\StoreBundle\Entity\Buildings_types $ buildingsTypes
* @return构建
* /
public function addBuildingsType(\Farpost\StoreBundle\Entity\Buildings_types $ buildingsTypes)
{
$ this-> buildings_types [] = $ buildingsTypes;
return $ this;
}

/ **
*删除buildings_types
*
* @param \Farpost\StoreBundle\Entity\Buildings_types $ buildingsTypes
* /
public function removeBuildingsType(\Farpost\StoreBundle\Entity\Buildings_types $ buildingsTypes)
{
$ this-> buildings_types-> removeElement($ buildingsTypes );
}

/ **
*获取buildings_types
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getBuildingsTypes()
{
return $ this-> buildings_types;
}
}

Farpost / StoreBundle / Entity / Building_type.php:


 命名空间Farpost\StoreBundle\Entity; 

使用Doctrine\ORM\Mapping作为ORM;

/ **
*
* @ ORM\Entity
*
* /
class Building_type
{
/ **
* @var整数
*
* @ 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;

/ **
*获取id
*
* @return integer
* /
public function getId()
{
return $ this-> id;
}

/ **
*设置别名
*
* @param string $ alias
* @return Building_type
* /
public function setAlias($ alias)
{
$ this-> alias = $ alias;
return $ this;
}

/ **
*获取别名
*
* @return string
* /
public function getAlias )
{
return $ this->别名;
}
}

Farpost / APIBundle / Controller / DefaultController.php: / p>

  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  - 映射文件是正确的。 
[数据库] OK - 数据库模式与映射文件同步。


解决方案

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

您需要将您的课程重命名为 BuildingType 并将其放在 Farpost / StoreBundle / Entity /BuildingType.php


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

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.

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆