【问题标题】:Doctrine cannot map entity/repository namespace in chainDoctrine 无法在链中映射实体/存储库命名空间
【发布时间】:2017-10-04 18:20:10
【问题描述】:

我正在尝试使用教义设置 symfony,配置教义对我来说是新的。

我尝试在控制器中使用以下代码从数据库中检索实体:

$feedImport = $this->getDoctrine()->getRepository(MyClass::class);

但我不断收到MappingException 错误:

The class 'AppBundle\Entity\MyClass' was not found in the chain configured namespaces

我还尝试手动指定完全限定的类名。但这没有帮助。

但是,我查看错误消息的方式似乎表明我需要进行一些额外的配置:我需要将我的实体/存储库添加到“配置的命名空间链”。但是我很难找到这个链以及如何配置它(如果我的假设是正确的)

问题 #1:是否存在某种命名空间链?如果有在哪里可以看到如何配置的示例?

所有实体信息都在我的AppBundle 包中。请参阅下面的包结构:

...
└── AppBundle
    ├── AppBundle.php
    ├── Controller
    │   └── DefaultController.php
    ├── Entity
    │   └── MyClass.php
    ├── Repository
    │   └── MyClassRepository.php
    └── Resources
        └── config
            └── doctrine
                └── MyClass.orm.yml
...

我使用 symfony bin\console 工具生成了实体类/repository/orm.yml 文件。

如果我使用相同的 console 工具来检查学说映射信息: doctrine:mapping:info 命令我告诉我类是正确/成功映射的:

Found 1 mapped entity:
[OK]   AppBundle\Entity\MyClass

关于模型/实体配置文件的生成过程。 我使用 symfony console 工具生成了元数据 yaml 文件。通过运行命令:doctrine:mapping:import。在创建了 yml 文件之后,我使用了 'doctrine:generate:entities' 来生成实体类。

我的印象是,如果文件生成并存储在 Entity 文件夹中的工作包中,auto_mapping: true 会自动处理此映射。

这个假设正确吗?如果不是,请告诉我出了什么问题。

  • 实体文件和配置存储在工作包AppBundle

  • AppBundle 已注册并正在工作(控制器正在做他的事情)

  • 命令:doctrine:mapping:info 似乎会立即映射实体。

实体模型的完整代码:

<?php

namespace AppBundle\Entity;

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

    /**
     * @var integer
     */
    private $customerId;

    /**
     * @var integer
     */
    private $feedId;

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

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

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

    /**
     * @var integer
     */
    private $fileSize;

    /**
     * @var integer
     */
    private $totalHeaderFields;

    /**
     * @var integer
     */
    private $totalRecords;

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

    /**
     * @var \DateTime
     */
    private $datetimeCreated = 'CURRENT_TIMESTAMP';

    /**
     * @var \DateTime
     */
    private $datetimeProcessed;


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

    /**
     * Set customerId
     *
     * @param integer $customerId
     *
     * @return MyClass
     */
    public function setCustomerId($customerId)
    {
        $this->customerId = $customerId;

        return $this;
    }

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

    /**
     * Set feedId
     *
     * @param integer $feedId
     *
     * @return MyClass
     */
    public function setFeedId($feedId)
    {
        $this->feedId = $feedId;

        return $this;
    }

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

    /**
     * Set contentHash
     *
     * @param string $contentHash
     *
     * @return MyClass
     */
    public function setContentHash($contentHash)
    {
        $this->contentHash = $contentHash;

        return $this;
    }

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

    /**
     * Set headerHash
     *
     * @param string $headerHash
     *
     * @return MyClass
     */
    public function setHeaderHash($headerHash)
    {
        $this->headerHash = $headerHash;

        return $this;
    }

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

    /**
     * Set feedName
     *
     * @param string $feedName
     *
     * @return MyClass
     */
    public function setFeedName($feedName)
    {
        $this->feedName = $feedName;

        return $this;
    }

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

    /**
     * Set fileSize
     *
     * @param integer $fileSize
     *
     * @return MyClass
     */
    public function setFileSize($fileSize)
    {
        $this->fileSize = $fileSize;

        return $this;
    }

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

    /**
     * Set totalHeaderFields
     *
     * @param integer $totalHeaderFields
     *
     * @return MyClass
     */
    public function setTotalHeaderFields($totalHeaderFields)
    {
        $this->totalHeaderFields = $totalHeaderFields;

        return $this;
    }

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

    /**
     * Set totalRecords
     *
     * @param integer $totalRecords
     *
     * @return MyClass
     */
    public function setTotalRecords($totalRecords)
    {
        $this->totalRecords = $totalRecords;

        return $this;
    }

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

    /**
     * Set importStatus
     *
     * @param string $importStatus
     *
     * @return MyClass
     */
    public function setImportStatus($importStatus)
    {
        $this->importStatus = $importStatus;

        return $this;
    }

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

    /**
     * Set datetimeCreated
     *
     * @param \DateTime $datetimeCreated
     *
     * @return MyClass
     */
    public function setDatetimeCreated($datetimeCreated)
    {
        $this->datetimeCreated = $datetimeCreated;

        return $this;
    }

    /**
     * Get datetimeCreated
     *
     * @return \DateTime
     */
    public function getDatetimeCreated()
    {
        return $this->datetimeCreated;
    }

    /**
     * Set datetimeProcessed
     *
     * @param \DateTime $datetimeProcessed
     *
     * @return MyClass
     */
    public function setDatetimeProcessed($datetimeProcessed)
    {
        $this->datetimeProcessed = $datetimeProcessed;

        return $this;
    }

    /**
     * Get datetimeProcessed
     *
     * @return \DateTime
     */
    public function getDatetimeProcessed()
    {
        return $this->datetimeProcessed;
    }
}

感谢任何建议!

【问题讨论】:

  • 你的 PHP 版本是多少?
  • 我的服务器正在运行 PHP 7.1
  • 你能展示你的Entity类和namespace吗?
  • 这是我的实体类:文件路径:MyProject/src/AppBundle/Entity/MyClass.php` / class MyClass { /* * @var integer */ private $id; .....还有更多}`
  • 我想看看你的Entity类的所有代码

标签: php entity-framework symfony doctrine-orm


【解决方案1】:

试试这个:

$feedImport = $this->getDoctrine()->getRepository('AppBundle:MyClass');

如果你想访问特定的函数/方法

$feedImport = $this->getDoctrine()->getRepository('AppBundle:MyClass')->sampleMethod();

【讨论】:

  • 对不起,我忘了提及我已经尝试过AppBundle:MyClass 语法。也没有用。
【解决方案2】:

您的包未在 AppKernel 文件中声明,或者您的学说映射未重新调整。

您的 AppKernel 中应该有:

public function registerBundles()
{
    $bundles = array(
        ...
        new AppBundle\AppBundle(),
    );

    ....

    return $bundles;
}

默认情况下在您的 config.yml 中:

orm:
    auto_generate_proxy_classes: %kernel.debug%
    auto_mapping: true

【讨论】:

  • 我检查了AppKernel.php 和我的config.yml 的这些设置。正如你所描述的那样。
【解决方案3】:

试试这个:

/**
 *
 * @ORM\Table(name="my_table_name")
 * @ORM\Entity()
 */
class MyClass

【讨论】:

  • 然后php bin/console doctrine:schema:update --force
  • 我按照您描述的方式添加了文档块并运行了doctrine:schema:update --force 命令。命令执行正常。但仍然是同样的错误...... :(
【解决方案4】:

经过长时间的努力,我发现了导致问题的原因。 我的 symfony 环境在prod 模式下运行。我切换到dev。这一切都像一个魅力。

但我仍然想知道为什么?我检查了AppKernel.php,它看起来像:

$bundles = [
    new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
    new Symfony\Bundle\SecurityBundle\SecurityBundle(),
    new Symfony\Bundle\TwigBundle\TwigBundle(),
    new Symfony\Bundle\MonologBundle\MonologBundle(),
    new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
    new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
    new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
    new AppBundle\AppBundle(),
];

if (in_array($this->getEnvironment(), ['dev', 'test'], true)) {
    $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
    $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
    $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();

    if ('dev' === $this->getEnvironment()) {
        $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        $bundles[] = new Symfony\Bundle\WebServerBundle\WebServerBundle();
    }
}

return $bundles;

在我看来,无论我运行什么环境,都加载了学说。有人可以向我解释这种行为背后的原因吗?

编辑:

我发现了更多东西,我再次切换到prod 环境,但现在我开始打开/关闭调试。

现在我看到它在生产模式下运行良好,但只有在启用调试时才可以。为什么?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多