includesToMany,混合关系

时间:2019-03-13 18:23:18

标签: php laravel jenssegers-mongodb

我有2个简单的模型。首先,一个叫做Builds(mysql),第二个叫做SlotOptions(mongodb)。每个版本可以分配5个插槽。基于https://stackoverflow.com/a/55132863/2513428

select * from `builds` where `builds`.`id` = 37 limit 1
SlotOptions.find({"heroName":{"$in":[37]}},{"typeMap":{"root":"array","document":"array"}})

表。 build_slot_option (mysql)

+----------------+--------+----------------------------------------+
|      Name      |  Type  |                  Desc                  |
+----------------+--------+----------------------------------------+
| slot_option_id | char50 | // FK, Name of slot option f.e "Hero1" |
| build_id       | int    | // FK, Build id                        |
| pos            | int    | // Number 1-5                          |
+----------------+--------+----------------------------------------+

slot_option_id-是包含英雄名称的字符串,实际上不是ID。

示例

+----------------+----------+-----+
| slot_option_id | build_id | pos |
+----------------+----------+-----+
| Hero1          |       37 |   1 |
| Hero2          |       37 |   2 |
| Hero3          |       37 |   3 |
| Hero4          |       37 |   4 |
| Hero5          |       37 |   5 |
+----------------+----------+-----+

表。 构建(mysql)

+------+------+------------------------+
| Name | Type |          Desc          |
+------+------+------------------------+
| id   | int  | // PK, of Builds Table |
| ...  | ...  | // and other columns   |
+------+------+------------------------+

这是构建类和用法示例。

 class BuildDB extends Model
{
    use HybridRelations;
    protected $connection = 'mysql';
    public function slots()
    {
        return $this->belongsToMany(
            SlotOptions::class, 'build_slot_option', 'build_id', 'slot_option_id'
        );
    }
}

BuildDB::with('slots')->find(5);

对于第二个查询,它应该不插入构建ID ,仅在{"$in":[37]}中插入来自tb build_slot_option slot_option_id的所有插槽名称列。例如{"$in":['Hero1','Hero2']}

0 个答案:

没有答案