belongsToMany, hybrid relation

别说谁变了你拦得住时间么 提交于 2019-12-06 13:35:51

问题


I have 2 simple models. First, one is called Builds (mysql) and the second one is called SlotOptions (mongodb). Each build can have 5 assigned slots. Based on 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"}})

Table. 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 - is a string that contains a hero name, it's not an ID in fact.

Example:

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

Table. Builds (mysql)

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

This is the build class, and usage example.

 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);

When it comes to the second query it should not insert the build id there {"$in":[37]} just all the slot names, from tb build_slot_option slot_option_id column. For example {"$in":['Hero1','Hero2']}

来源:https://stackoverflow.com/questions/55148861/belongstomany-hybrid-relation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!