代码之家  ›  专栏  ›  技术社区  ›  Kavvson Empcraft

亲缘关系

  •  0
  • Kavvson Empcraft  · 技术社区  · 7 年前

    我有两个简单的模型。首先,一个称为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"}})
    

    桌子。 内置插槽选项 (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 | 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   |
    +------+------+------------------------+
    

    这是build类和用法示例。

     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 插槽选项 列。例如 {"$in":['Hero1','Hero2']}

    0 回复  |  直到 7 年前
    推荐文章