我有一个这样的查询:
$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 其中 myApp\Models\UsersProfiles.profilesId = 2