【问题标题】:Model could not be loaded while executing query执行查询时无法加载模型
【发布时间】:2016-07-03 00:53:22
【问题描述】:

我有一个这样的查询:

$phsql = "
    SELECT s.id AS siteId, s.name 
    FROM site s
    INNER JOIN profiles p ON s.id = p.siteId
    INNER JOIN users_profiles up ON up.profilesId = p.id
        AND p.name = 'admin'
        AND up.usersId = 2
";

我在模型方法中转换如下:

$sites = Site::query()
            ->innerJoin('Profiles', 'Sites.id = Profiles.siteId')
            ->innerJoin('UsersProfiles', 'UsersProfiles.profilesId = Profiles.id')
            ->andWhere('Profiles.name = name')
            ->andWhere('UsersProfiles.usersId = :usersId:', ['userId' => $admin_id])->execute();

运行时会报错:

无法加载模型配置文件

请注意我在 Site 模型中运行它。

更新

我试过了:

    $sites = $this->modelsManager->createBuilder() 
    ->from('myApp\Models\Site') 
   ->innerJoin('myApp\Models\Profiles','myApp\Models\Site.id = myApp\Models\Profiles.siteId') 
->andWhere("myApp\Models\Profiles.name = 'admin' ")
 ->where("myApp\Models\UsersProfiles.profilesId = 2")
 ->getQuery()
 ->execute();

现在它给出了错误:

未知模型或别名'myApp\Models\UsersProfiles' (11),准备时:SELECT [myApp\Models\Site].* FROM [myApp\Models\Site] INNER JOIN [myApp\Models\Profiles] ON myApp \Models\Site.id = myApp\Models\Profiles.siteId WHERE myApp\Models\UsersProfiles.profilesId = 2

【问题讨论】:

  • 你在使用命名空间吗?你为什么要两次->execute();?这是复制粘贴错误吗?
  • @Timothy 复制/粘贴错误

标签: php phalcon phalcon-orm


【解决方案1】:

查看您的代码,我发现两个问题:

1) 你第二行的 ->execute() 应该抛出一个解析错误?

->innerJoin('Profiles', 'Sites.id = Profiles.siteId')->execute();

2) 你必须为你的模型添加命名空间,见下面的代码。

查询的工作示例:

Objects::query()
    ->columns([
        'Models\Objects.id AS objectID',
        'Models\ObjectLocations.id AS locationID',
        'Models\ObjectCategories.category_id AS categoryID',
    ])
    ->innerJoin('Models\ObjectLocations', 'Models\Objects.id = Models\ObjectLocations.object_id')
    ->innerJoin('Models\ObjectCategories', 'Models\Objects.id = Models\ObjectCategories.object_id')
    ->where('Models\Objects.is_active = 1')
    ->andWhere('Models\Objects.id = :id:', ['id' => 2])        
    ->execute();  

您可以在关系中添加第三个参数(别名)以减少命名空间并提高代码可读性:

->innerJoin('Models\ObjectLocations', 'loc.object_id = obj.id', 'loc');

更多信息在这里:https://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Criteria.html

另请注意: 使用 where() 和 andWhere() 会将 where 子句添加到您的查询中。在您的第一个查询示例中,子句位于第二个连接语句中,而在您的 Phalcon 查询中,where 子句被添加到整个查询中。如果您真的只希望这些条件仅用于第二个连接,请将它们添加到第二个连接参数,如下所示:

->innerJoin(
   'Models\ObjectCategories', 
   'Models\Objects.id = Models\ObjectCategories.object_id AND ... AND ... AND ...'
)

【讨论】:

  • 现在我收到消息 `Unknown model or alias 'Proj\Models\UsersProfile'
  • 您没有加入 UsersProfiles 表 ->where("myApp\Models\UsersProfiles.profilesId = 2") 这就是您收到未知错误的原因。也许这张表 Profiles 也有 profileID,你可以在哪里使用它?
  • ` 'Models\Objects.id = Models\ObjectCategories.object_id AND ... AND ... AND ...'` 这会导致扫描错误。
  • 这只是一个例子,它不是一个工作代码。你能展示你尝试了什么吗?我还建议将 QueryBuilder 用于此类任务(作为您的最后一次更新)。
  • 我知道这是一个样本。反正我想通了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-17
  • 2016-01-27
  • 2021-10-21
  • 2021-09-03
  • 1970-01-01
  • 2016-08-30
相关资源
最近更新 更多