PhalconPHP - 获取具有多个关系的特定对象
PhalconPHP - get specific object with multiple relations
我有一个模型,称之为 Robot,它与其他模型有多个多对多关系 - Part、Country 和 Permision。关系模型是 RobotsParts、RobotsCountries 和 RobotsPermissions。
每个机器人可以有多个或没有链接到它们的部件、国家和权限。
要让所有的机器人都拥有特定的部分,PhalconPHP 让这一切变得简单。 (当然,在模型中正确设置了别名)。
$part = Part::findFirstByName("arm");
$robotsWithPart = $part->robots;
同样的事情适用于某个国家的机器人:
$country = Country::findFirstByCode("HR");
$robotsWithCountry = $country->robots;
但是如何才能只获得具有特定部位、国家和许可的机器人呢?
我有过徒劳的尝试,例如:
$country = Country::findFirstByCode("HR");
$part = Part::findFirstByName("arm");
$robots = $country->getRobots([
'conditions' => "(partId = :pid:)",
'bind' => [
'pid' => $part->id
]
]);
但是,partId 当然不会被识别,因为它不属于任何选定的模型;
您可以使用 $model->getRelated('model', $parameters = [])
选项。
$parameters = []
的工作方式与您通常查询模型的方式相同。 IE;它采用参数 order
、limit
、conditions
、...
$country = Country::findFirstByCode("HR");
$part = Part::findFirstByName("arm");
$robots = $country->getRelated('Robot', ['partId = ' . $part->id]);
您可以在 the documentation
中找到它
更新
这听起来不可能。您将必须在 Robot
模型上调用自定义查询。像这样:
$result = Robot::query()
->join('Country', 'Country.id = Robot.countryId')
->join('Part', 'Part.robotId = Robot.id')
->where('Country.code = :code:')
->where('Part.name = :name:')
->bind(['code' => 'HR', 'name' => 'arm'])
->execute();
如果您愿意,也可以使用查询生成器。
我有一个模型,称之为 Robot,它与其他模型有多个多对多关系 - Part、Country 和 Permision。关系模型是 RobotsParts、RobotsCountries 和 RobotsPermissions。
每个机器人可以有多个或没有链接到它们的部件、国家和权限。
要让所有的机器人都拥有特定的部分,PhalconPHP 让这一切变得简单。 (当然,在模型中正确设置了别名)。
$part = Part::findFirstByName("arm");
$robotsWithPart = $part->robots;
同样的事情适用于某个国家的机器人:
$country = Country::findFirstByCode("HR");
$robotsWithCountry = $country->robots;
但是如何才能只获得具有特定部位、国家和许可的机器人呢?
我有过徒劳的尝试,例如:
$country = Country::findFirstByCode("HR");
$part = Part::findFirstByName("arm");
$robots = $country->getRobots([
'conditions' => "(partId = :pid:)",
'bind' => [
'pid' => $part->id
]
]);
但是,partId 当然不会被识别,因为它不属于任何选定的模型;
您可以使用 $model->getRelated('model', $parameters = [])
选项。
$parameters = []
的工作方式与您通常查询模型的方式相同。 IE;它采用参数 order
、limit
、conditions
、...
$country = Country::findFirstByCode("HR");
$part = Part::findFirstByName("arm");
$robots = $country->getRelated('Robot', ['partId = ' . $part->id]);
您可以在 the documentation
中找到它更新
这听起来不可能。您将必须在 Robot
模型上调用自定义查询。像这样:
$result = Robot::query()
->join('Country', 'Country.id = Robot.countryId')
->join('Part', 'Part.robotId = Robot.id')
->where('Country.code = :code:')
->where('Part.name = :name:')
->bind(['code' => 'HR', 'name' => 'arm'])
->execute();
如果您愿意,也可以使用查询生成器。