【发布时间】:2019-03-10 19:24:36
【问题描述】:
我正在尝试获取 product_categories 和过滤器的所有组合以及为每个产品选择的过滤器计数。
我可以得到组合,但是我很难得到正确的计数。 我在 Laravel 工作并使用 \DB::raw 查询。
这是我目前得到的:
\DB::raw("SELECT product_categories.*,filters.name as filter, filter_types.heading as filter_type
FROM product_categories, filters
JOIN filter_types ON filter_types.id = filters.filter_type_id
WHERE filter_types.type = 'Modules\\\DonaldRussell\\\ProductCategories\\\App\\\ProductCategory'
ORDER BY product_categories.name, filter_type
");
db 架构如下:
Schema::create('filter_types', function(Blueprint $table) {
$table->increments('id');
$table->string('type')->notNull();
$table->string('heading')->notNull();
$table->timestamps();
});
Schema::create('filters', function(Blueprint $table) {
$table->increments('id');
$table->integer('filter_type_id')->unsigned()->notNull();
$table->foreign('filter_type_id')->references('id')->on('categories_types');
$table->string('name')->notNull();
$table->integer('order')->notNull();
$table->integer('uses')->unsigned()->notNull()->default(0);
$table->timestamps();
});
Schema::create('product_categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->notNull();
$table->string('slug')->notNull();
$table->integer('order')->notnull()->unsigned()->default(0);
$table->integer('type')->notnull()->unsigned()->default(1);
$table->integer('seo')->nullable()->unsigned();
$table->foreign('seo')->references('id')->on('seos')->onDelete('cascade');
$table->timestamps();
});
Schema::create('product_category_sets', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->nullable()->unsigned();
$table->foreign('category_id')->references('id')->on('product_categories')->onDelete('set null');
$table->integer('sub_category_id')->nullable()->unsigned();
$table->foreign('sub_category_id')->references('id')->on('tags')->onDelete('set null');
$table->integer('sub_sub_category_id')->nullable()->unsigned();
$table->foreign('sub_sub_category_id')->references('id')->on('tags')->onDelete('set null');
$table->integer('product_id')->notnull()->unsigned();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->timestamps();
});
Schema::create('product_category_set_filters', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_category_set_id')->notnull()->unsigned();
$table->foreign('product_category_set_id')->references('id')->on('product_category_sets')->onDelete('cascade');
$table->integer('filter_id')->notnull()->unsigned();
$table->foreign('filter_id')->references('id')->on('filters')->onDelete('cascade');
$table->timestamps();
});
【问题讨论】: