【问题标题】:How to solve the error cannot redeclare class in models manager?如何解决无法在模型管理器中重新声明类的错误?
【发布时间】:2018-01-15 13:43:19
【问题描述】:

我想使用 Phalcon Php 提供的模型管理器执行查询。我有以下功能:

public function getSomeData()
{
    return Di::getDefault()->get("modelsManager")->executeQuery(
            "SELECT * FROM SomeModel WHERE id NOT IN (SELECT id FROM AnotherModel)"
        );
    }
}

执行上述函数时,出现以下错误

无法重新声明类 ParentDir\Model\SomeModel

但是,如果我为查询提供完整的命名空间,它就可以工作。

SELECT * FROM ParentDir\Model\SomeModel WHERE id NOT IN (SELECT id FROM ParentDir\Model\AnotherModel)

我在di 中注册dirsnamespaces,如下所示:

$parentDir = dirname(__DIR__);

$loader = new Loader();

$loader->registerDirs([
    $parentDir . "/app/controllers",
    $parentDir . "/app/models"
]);

 $loader->registerNamespaces([
    "ParentDir\\Model" => "$parentDir/app/models/",
    "ParentDir\\Model\\Enum" => "$parentDir/app/models/enum/",
    "ParentDir\\Controller" => "$parentDir/app/controllers/",
    "ParentDir\\Service" => "$parentDir/app/services/",
    "ParentDir\\Locale" => "$parentDir/app/locale/",
    "ParentDir\\Utility" => "$parentDir/app/utility/",
    "ParentDir\\Plugin" => "$parentDir/app/plugin/",
    "ParentDir" => "$parentDir/app/",
]);

$loader->register();

【问题讨论】:

  • "However, if I supply the full namespace to the query, it works." 就这样吧?
  • @Timothy 这样做会使查询工作。但是,我会忽略依赖注入器中存在问题的事实。官方文档不包含完整的命名空间,因为它是 modelsManager 的工作。

标签: php dependency-injection phalcon


【解决方案1】:

这不是模型管理器,你只是在某个地方用加载器加载这个类两次。或者你有两个相同的类。

【讨论】:

    【解决方案2】:

    您是否尝试过为命名空间创建别名?

    modelsManager->createQuery 失败并使用纯 sql。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-05
      • 2012-06-26
      • 2017-07-23
      • 2023-04-02
      • 2020-03-21
      相关资源
      最近更新 更多