人与马的关系

时间:2018-10-03 04:57:42

标签: laravel eloquent relationship

我正在尝试获取三个表之间的关系。下表:

table: documentsets
docflow_documentset_id
..
..

table: subsets
docflow_subset_id
docflow_documentset_id
..
..

table: subdocuments
docflow_subdocument_id
docflow_subset_id
..
..

我试图获取属于带有中间表子集的文档集的所有子文档。

表的主键是:

docflow_documentset_id,docflow_subset_id,docflow_subdocument_id

在我的docflow_documentsets模型中,我具有以下功能:

    public function subdocuments()
    {
        return $this->belongsToMany(docflow_subdocuments::class, 'bvd_pp_prod_docflow_subsets', 'docflow_subset_id', 'docflow_subset_id');
    }

以及我的docflow_subdocuments中的以下内容:

public function documentsets()
    {
        return $this->belongsToMany(docflow_documentsets::class, 'bvd_pp_prod_docflow_subsets', 'docflow_subset_id', 'docflow_subset_id');
    }

某种程度上,根本没有调用docflow_subdocuments中的documentsets函数,这使我明白为什么发生这种情况:(

有人可以在这里帮忙吗?

编辑:

迁移:

Schema::create('bvd_pp_prod_docflow_documentsets', function (Blueprint $table) {
        $table->increments('docflow_documentset_id');
        $table->integer('docflow_id');
        $table->string('docflow_documentset_name', 50)->nullable();
        $table->string('docflow_documentset_type', 50)->nullable();
    });

Schema::create('bvd_pp_prod_docflow_subsets', function (Blueprint $table) {
        $table->increments('docflow_subset_id');
        $table->integer('docflow_documentset_id');
        $table->boolean('docflow_subset_staple');
    });

Schema::create('bvd_pp_prod_docflow_subdocuments', function (Blueprint $table) {
        $table->increments('docflow_subdocument_id');
        $table->integer('docflow_subset_id');
        $table->string('docflow_subdocument_document', 50);
        $table->string('docflow_subdocument_stamp', 50);
        $table->string('docflow_subdocument_mediatype', 50);
        $table->boolean('docflow_subdocument_duplex');
    });

2 个答案:

答案 0 :(得分:0)

您应该使用belongsTo和hasMany创建模型

在docflow_documentset中:

public function subsets(){
    return $this->hasMany(docflow_subset::class);
}

在docflow_subset中:

public function subdocuments(){
    return $this->hasMany(docflow_subdocument::class);
}

public function documentsets(){
    return $this->belongsTo(docflow_documentset::class, 'docflow_documentset_id');
}

在docflow_subdocument中:

public function subsets(){
    return $this->belongsTo(docflow_subset::class, 'docflow_subset_id');
}

答案 1 :(得分:0)

最后找到了解决方法:

import re
found = []
match = re.compile('(Mike|Zack):(\w*)')
with open('/hope/ninja/Destop/raw.twt', "r") as raw:
    for rec in raw:
        found.extend(match.find_all(rec))

print(found)
#output: [('Mike', '15'), ('Zack', '17')]

给我我想要的结果, 这是因为我在表上创建了外键吗?