【问题标题】:Symfony2 Class Brenne\BaseBundle\Entity\Websites is not a valid entity or mapped super classSymfony2 Class Brenne\BaseBundle\Entity\Websites 不是有效的实体或映射的超类
【发布时间】:2014-02-21 21:09:10
【问题描述】:

我确实有一个非常奇怪的问题,并搜索并尝试了不同的方法。正如我的标题所述,我的实体(包括所有实体!)无法正常工作 - 意味着它们无法映射。

我会很快进入重点:

扩展数组

Array ( [0] => Core [1] => date [2] => ereg [3] => libxml [4] => openssl [5] => pcre [6] => sqlite3 [7] => zlib [8] => bcmath [9] => bz2 [10] => calendar [11] => ctype [12] => curl [13] => dba [14] => dom [15] => hash [16] => fileinfo [17] => filter [18] => ftp [19] => gd [20] => gettext [21] => gmp [22] => SPL [23] => iconv [24] => session [25] => intl [26] => json [27] => ldap [28] => mbstring [29] => mcrypt [30] => mssql [31] => mysql [32] => standard [33] => PDO [34] => pdo_dblib [35] => mysqlnd [36] => pdo_sqlite [37] => pgsql [38] => apc [39] => posix [40] => Reflection [41] => imap [42] => shmop [43] => SimpleXML [44] => snmp [45] => soap [46] => sockets [47] => pdo_mysql [48] => exif [49] => sysvmsg [50] => sysvsem [51] => sysvshm [52] => tidy [53] => tokenizer [54] => wddx [55] => xml [56] => xmlreader [57] => xmlrpc [58] => xmlwriter [59] => xsl [60] => zip [61] => mysqli [62] => apache2handler [63] => Phar [64] => mhash )

到目前为止,我想出了并尝试修复:

  1. APC 开启(没有 eAcc,这主要是导致问题的原因)REF:("Class XXX is not a valid entity or mapped super class" after moving the class in the filesystem)
  2. 删除资源中的Doctrine文件夹,清除缓存。没有成功。
  3. 为配置 ORM 的实体添加了完整的命名空间 // 更改:“company”到“x/basebundle/entity/company”不起作用,因此恢复了更改。
  4. 我不使用注释,我使用 yml。
  5. 自动映射是真的!

Resources/Config/Doctrine/Websites.orm.yml 将实体类作为第一个参数:

Brenne\BaseBundle\Entity\Website:
    type: entity
    table: website
    repositoryClass: Brenne\BaseBundle\Repository\WebsiteRepository
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 110
            nullable: true
        url:
            type: string
            length: 255
            nullable: true
        logo:
            type: string
            length: 255
            nullable: true
        description:
            type: text
    oneToMany:
        assignedPing:
            targetEntity: ping
            mappedBy: website
    manyToOne:
        company:
          targetEntity: Company
          inversedBy: website

/Entity/Websites.orm.yml 将实体类作为第一个参数:

<?php

namespace Brenne\BaseBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Website
 */
class Website
{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var string
     */
    private $url;

    /**
     * @var string
     */
    private $logo;

    /**
     * @var string
     */
    private $description;

    /**
     * @var \Doctrine\Common\Collections\Collection
     */
    private $assignedPing;

    /**
     * @var \Brenne\BaseBundle\Entity\Company
     */
    private $company;

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

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

    /**
     * Set name
     *
     * @param string $name
     * @return Website
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string 
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set url
     *
     * @param string $url
     * @return Website
     */
    public function setUrl($url)
    {
        $this->url = $url;

        return $this;
    }

    /**
     * Get url
     *
     * @return string 
     */
    public function getUrl()
    {
        return $this->url;
    }

    /**
     * Set logo
     *
     * @param string $logo
     * @return Website
     */
    public function setLogo($logo)
    {
        $this->logo = $logo;

        return $this;
    }

    /**
     * Get logo
     *
     * @return string 
     */
    public function getLogo()
    {
        return $this->logo;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Website
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Add assignedPing
     *
     * @param \Brenne\BaseBundle\Entity\ping $assignedPing
     * @return Website
     */
    public function addAssignedPing(\Brenne\BaseBundle\Entity\ping $assignedPing)
    {
        $this->assignedPing[] = $assignedPing;

        return $this;
    }

    /**
     * Remove assignedPing
     *
     * @param \Brenne\BaseBundle\Entity\ping $assignedPing
     */
    public function removeAssignedPing(\Brenne\BaseBundle\Entity\ping $assignedPing)
    {
        $this->assignedPing->removeElement($assignedPing);
    }

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

    /**
     * Set company
     *
     * @param \Brenne\BaseBundle\Entity\Company $company
     * @return Website
     */
    public function setCompany(\Brenne\BaseBundle\Entity\Company $company = null)
    {
        $this->company = $company;

        return $this;
    }

    /**
     * Get company
     *
     * @return \Brenne\BaseBundle\Entity\Company 
     */
    public function getCompany()
    {
        return $this->company;
    }
}

我不知道还有什么可以做的,希望你能想出一个主意!感谢您的宝贵时间。

【问题讨论】:

    标签: php symfony doctrine


    【解决方案1】:

    您必须为存储它们的映射文件和文件夹命名 完全按照惯例。

    ...否则在加载文件时您将面临无效的映射类元数据或错误。

    请记住,文件和文件夹在某些系统上区分大小写

    解决方案:

    应该读取映射文件:

    • Website.orm.yml
    • ...或...
    • Brenne.BaseBundle.Entity.Website.orm.yml

    它应该存在于文件夹中......

    src/Brenne/BaseBundle/Resources/config/doctrine
    

    请注意命名(configdoctrine - 小型大写字母、Website 没有 s)...


    清除您的 symfony 缓存以获得正确的环境(app/console ca:c --env=..rm -rf app/cache/*)并重新启动您的网络服务器/fpm-pool(以便从 APC 和 opcache 清除缓存的元数据)

    【讨论】:

    • 这对我有用。这似乎非常重要,首先您没有Invalid Entities,其次要注意区分大小写的文件/文件夹,因为ping@oneToMany 网站上的错误小写字母破坏了它。所以注意区分大小写的文件/文件夹!
    • 很高兴您能够解决您的问题 :) 感谢您的反馈。
    【解决方案2】:

    尝试在命名空间前使用\

    \Brenne\BaseBundle\Entity\Websites:
    type: entity
    table: websites
    repositoryClass: \Brenne\BaseBundle\Repository\WebsitesRepository
    

    【讨论】:

    • 在这种情况下没有帮助。但是,如果我在命名空间前添加了 \,它会停止在我的本地机器(Windows Xampp)上工作。无论如何,生产服务器上的错误(有和没有)是相同的。
    • 刚刚发现:我的本地主机上确实有 3 个无效实体。这会是个问题吗?老实说,不知道如何解决它们。或多或少都是这样的:Brenne\BaseBundle\Entity\Company The association Brenne\BaseBundle\Entity\Company#status refers to the owning side field Brenne\BaseBundle\Entity\Websites#website which does not exist.
    • 可能有问题
    • 您的Company 映射中需要mappedBy: companyWebsite 映射中需要inversedBy: website。拥有和反面是教义的概念,如果您认为公司拥有网站或其他方式,可能并不总是反映您的想法...inversedBy 始终是拥有方。
    • 是的。在阅读了 Doctrine 文档并对其进行了修复后,我确实发现了这一点。关于 mappedBy/inveresdBy,您是完全正确的。参考:docs.doctrine-project.org/en/2.0.x/reference/…
    【解决方案3】:

    如果这是真的你的Websites.orm.yml文件的内容,那么缩进是错误的:

    你应该像这样在Brenne\BaseBundle\Entity\Websites: 之后缩进:

    Brenne\BaseBundle\Entity\Websites:
        type: entity
        table: websites
        repositoryClass: Brenne\BaseBundle\Repository\WebsitesRepository
        id:
            id:
                type: integer
                generator: { strategy: AUTO }
        fields:
            name:
                type: string
                length: 110
                nullable: true
            url:
                type: string
                length: 255
                nullable: true
            url:
                type: string
                length: 255
                nullable: true
            description:
                type: text
        manyToOne:
            company:
                targetEntity: Company
                inversedBy: websites
                joinColumn:
                    name: company_id
                    referencedCo
    

    【讨论】:

    • 嘿,那里没有缩进。我只是对那里的 stackoverflow 编辑器有点挣扎。如另一个答案所述,我现在修复了一些实体错误。我将正确的 Website.orm.yml 编辑回第一个问题。感谢您的宝贵时间!
    猜你喜欢
    • 2011-12-21
    • 2016-01-20
    • 2013-02-05
    • 1970-01-01
    • 2018-01-15
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多