【问题标题】:Doctrine The target-entity cannot be found inDoctrine 找不到目标实体
【发布时间】:2017-09-23 00:56:42
【问题描述】:

尝试加载我的网页时,我在服务器上收到此错误: 在“ArcaSolutions\ListingBundle\Entity\Listing#classified”中找不到目标实体 ArcaSolutions\ListingBundle\Entity\Classified。 我毫不怀疑问题在于目标实体引用的是 ListingBundle 而不是 ClassifiedBundle,但我在我的代码中看不到任何可能犯此错误的地方。

到目前为止,我已经尝试重新启动 Apache,并清除了学说缓存。

这是 Classified.php 的代码 (MyProject\src\ArcaSolutions\ClassifiedBundle\Entity\Classified.php)

namespace ArcaSolutions\ClassifiedBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use JMS\Serializer\Annotation as Serializer;

/**
* Classified
*
* @ORM\Table(name="Classified", indexes={@ORM\Index(name="country_id", columns={"location_1"}), @ORM\Index(name="state_id", columns={"location_2"}), @ORM\Index(name="region_id", columns={"location_3"}), @ORM\Index(name="latitude", columns={"latitude"}), @ORM\Index(name="longitude", columns={"longitude"}), @ORM\Index(name="level", columns={"level"}), @ORM\Index(name="status", columns={"status"}), @ORM\Index(name="account_id", columns={"account_id"}), @ORM\Index(name="city_id", columns={"location_4"}), @ORM\Index(name="area_id", columns={"location_5"}), @ORM\Index(name="title", columns={"title"}), @ORM\Index(name="friendly_url", columns={"friendly_url"}), @ORM\Index(name="cat_1_id", columns={"cat_1_id"}), @ORM\Index(name="parcat_1_level1_id", columns={"parcat_1_level1_id"}), @ORM\Index(name="fulltextsearch_keyword", columns={"fulltextsearch_keyword"}), @ORM\Index(name="fulltextsearch_where", columns={"fulltextsearch_where"})})
* @ORM\Entity(repositoryClass="ArcaSolutions\ClassifiedBundle\Repository\ClassifiedRepository")
*/
class Classified
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer", nullable=false)
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="IDENTITY")
    * @Serializer\Groups({"classifiedDetail", "Result", "listingDetail"})
    */
    private $id;

    /**
    * @ORM\OneToMany(targetEntity="ArcaSolutions\ListingBundle\Entity\Listing", mappedBy="classified")
    * @ORM\OrderBy({"status" = "ASC"})
    * @Serializer\Groups({"classifiedDetail", "Result"})
    * @Serializer\Type("array")
    */
    private $listingArray;

    /**
    * Constructor
    */
    public function __construct()
    {
        $this->listingArray = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
    * Add listingArray
    *
    * @param \ArcaSolutions\ListingBundle\Entity\Listing $listingArray
    * @return Classified
    */
    public function addListingArray(\ArcaSolutions\ListingBundle\Entity\Listing $listingArray)
    {
        $this->listingArray[] = $listingArray;

        return $this;
    }

    /**
    * Remove listingArray
    *
    * @param \ArcaSolutions\ListingBundle\Entity\Listing $listingArray
    */
    public function removeListingArray(\ArcaSolutions\ListingBundle\Entity\Listing $listingArray)
    {
        $this->listingArray->removeElement($listingArray);
    }

    /**
    * Get listingArray
    *
    * @return \Doctrine\Common\Collections\Collection 
    */
    public function getListingArray()
    {
        return $this->listingArray;
    }
}

这里是 Listing.php 的代码 (MyProject\src\ArcaSolutions\ListingBundle\Entity\Listing.php)

namespace ArcaSolutions\ListingBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use JMS\Serializer\Annotation as Serializer;


/**
* Listing
*
* @ORM\Table(name="Listing", indexes={@ORM\Index(name="title", columns={"title"}), @ORM\Index(name="country_id", columns={"location_1"}), @ORM\Index(name="state_id", columns={"location_2"}), @ORM\Index(name="region_id", columns={"location_3"}), @ORM\Index(name="account_id", columns={"account_id"}), @ORM\Index(name="renewal_date", columns={"renewal_date"}), @ORM\Index(name="status", columns={"status"}), @ORM\Index(name="latitude", columns={"latitude"}), @ORM\Index(name="longitude", columns={"longitude"}), @ORM\Index(name="level", columns={"level"}), @ORM\Index(name="city_id", columns={"location_4"}), @ORM\Index(name="area_id", columns={"location_5"}), @ORM\Index(name="zip_code", columns={"zip_code"}), @ORM\Index(name="friendly_url", columns={"friendly_url"}), @ORM\Index(name="listingtemplate_id", columns={"listingtemplate_id"}), @ORM\Index(name="image_id", columns={"image_id"}), @ORM\Index(name="thumb_id", columns={"thumb_id"}), @ORM\Index(name="idx_fulltextsearch_keyword", columns={"fulltextsearch_keyword"}), @ORM\Index(name="idx_fulltextsearch_where", columns={"fulltextsearch_where"}), @ORM\Index(name="updated_date", columns={"updated"}), @ORM\Index(name="clicktocall_number", columns={"clicktocall_number"}), @ORM\Index(name="Listing_Promotion", columns={"level", "account_id", "title", "id"})})
* @ORM\Entity(repositoryClass="ArcaSolutions\ListingBundle\Repository\ListingRepository")
*/
class Listing
{
    /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer", nullable=false)
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="IDENTITY")
    * @Serializer\Groups({"listingDetail", "Result", "classifiedDetail", "dealDetail", "reviewItem"})
    */
    private $id;

    /**
    * @var integer
    *
    * @ORM\Column(name="classified_id", type="integer", nullable=true)
    */
    private $classifiedId;

    /**
    * @ORM\ManyToOne(targetEntity="ArcaSolutions\ClassifiedBundle\Entity\Classified", inversedBy="listingArray")
    * @ORM\JoinColumn(name="classified_id", referencedColumnName="id")
    * @Serializer\Groups({"listingDetail"})
    */
    private $classified;

    /**
    * Set classifiedId
    *
    * @param integer $classifiedId
    * @return Listing
    */
    public function setClassifiedId($classifiedId)
    {
        $this->classifiedId = $classifiedId;

        return $this;
    }

    /**
    * Get classifiedId
    *
    * @return integer 
    */
    public function getClassifiedId()
    {
        return $this->classifiedId;
    }

    /**
    * Set classified
    *
    * @param \ArcaSolutions\ClassifiedBundle\Entity\Classified $classified
    * @return Listing
    */
    public function setClassified(\ArcaSolutions\ClassifiedBundle\Entity\Classified $classified = null)
    {
        $this->classified = $classified;

        return $this;
    }

    /**
    * Get classified
    *
    * @return \ArcaSolutions\ClassifiedBundle\Entity\Classified 
    */
    public function getClassified()
    {
        return $this->classified;
    }

    /**
    * Add classifieds
    *
    * @param \ArcaSolutions\ClassifiedBundle\Entity\Classified $classifieds
    * @return Listing
    */
    public function addClassified(\ArcaSolutions\ClassifiedBundle\Entity\Classified $classifieds)
    {
        $this->classifieds[] = $classifieds;

        return $this;
    }

    /**
    * Remove classifieds
    *
    * @param \ArcaSolutions\ClassifiedBundle\Entity\Classified $classifieds
    */
    public function removeClassified(\ArcaSolutions\ClassifiedBundle\Entity\Classified $classifieds)
    {
        $this->classifieds->removeElement($classifieds);
    }
}

【问题讨论】:

  • 尝试删除列表中的classifiedId,因为您不需要它,这可能会造成问题
  • 你试过重启 Apache / php-fpm 吗?如果您启用了 apc 缓存,仅清除缓存文件夹是不够的。您还可以显示两个文件的完整文件名吗?
  • @kunicmarko20 删除分类 ID 没有影响,删除后仍然收到相同的错误。
  • @MaksymMoskvychev 我已经重新启动 Apache 以确保清除缓存,但仍然收到相同的错误。我还包括了 MyProject 根文件夹中的完整路径名。
  • 它在错误的地方寻找你的实体 ArcaSolutions\ListingBundle\Entity\Classified 这个实体在 ClassifiedBundle 而不是 Listing 你是否清除了你的学说缓存?

标签: php entity-framework symfony doctrine-orm orm


【解决方案1】:

您的问题与这 2 个实体无关。 看,也许您在项目中有另一个具有相同命名空间的实体? (复制粘贴问题?)重构后可能会留下一些东西。

我用你的名字创建了 2 个包,用你的内容创建了 2 个实体,它工作得很好(索引除外,它是为不存在的字段定义的)。我能够生成有效的迁移,Doctrine 也说“0 个无效实体”:

CREATE TABLE Classified (id INT AUTO_INCREMENT NOT NULL, PRIMARY 
KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = 
InnoDB;
CREATE TABLE Listing (id INT AUTO_INCREMENT NOT NULL, classified_id 
INT DEFAULT NULL, INDEX IDX_4BD7148A0B0417E (classified_id), PRIMARY 
KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = 
InnoDB;
ALTER TABLE Listing ADD CONSTRAINT FK_4BD7148A0B0417E FOREIGN KEY 
(classified_id) REFERENCES Classified (id);

【讨论】:

  • Moskvychev 它必须是这两个实体,我可以注释掉 $classified 和 $listingArray 的私有声明,并且网页加载正常。 (即 //private $classified;)
【解决方案2】:

我设法通过将 $classified 重命名为 $theclassified、将 mappedBy="classified" 更改为 mappedBy="theclassified"、重新生成两个包并更新 db 架构来消除错误。我不确定这是如何修复错误的,因为我没有在其他任何地方引用 $classified ,所以这很奇怪。此外,我现在收到一个单独的错误,我将发布一个单独的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多