Skip to content

Commit

Permalink
Products relation from categories (rapidez#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jade-GG authored Oct 11, 2023
1 parent 5c096b4 commit eeae34b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion config/rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@
'attribute' => Rapidez\Core\Models\Attribute::class,
'product' => Rapidez\Core\Models\Product::class,
'category' => Rapidez\Core\Models\Category::class,
'oauth_token' => Rapidez\Core\Models\OauthToken::class,
'category_product' => Rapidez\Core\Models\CategoryProduct::class,
'customer' => Rapidez\Core\Models\Customer::class,
'config' => Rapidez\Core\Models\Config::class,
'oauth_token' => Rapidez\Core\Models\OauthToken::class,
'option_swatch' => Rapidez\Core\Models\OptionSwatch::class,
'option_value' => Rapidez\Core\Models\OptionValue::class,
'product_image' => Rapidez\Core\Models\ProductImage::class,
Expand Down
5 changes: 2 additions & 3 deletions src/Commands/IndexCategoriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ public function handle(): int

public function getCategories()
{
return config('rapidez.models.category')::query()
->select((new (config('rapidez.models.category')))->qualifyColumns(['entity_id', 'name', 'url_path', 'children_count']))
return config('rapidez.models.category')::select((new (config('rapidez.models.category')))->qualifyColumns(['entity_id', 'name', 'url_path']))
->whereNotNull('url_key')
->whereNot('url_key', 'default-category')
->where('children_count', '>', 0)
->has('products')
->get() ?? [];
}
}
16 changes: 16 additions & 0 deletions src/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Rapidez\Core\Models\Scopes\IsActiveScope;
use Rapidez\Core\Models\Traits\HasAlternatesThroughRewrites;

Expand Down Expand Up @@ -65,6 +66,21 @@ public function subcategories()
return $this->hasMany(self::class, 'parent_id', 'entity_id');
}

public function products(): HasManyThrough
{
return $this
->hasManyThrough(
config('rapidez.models.product'),
config('rapidez.models.category_product'),
'category_id',
'entity_id',
'entity_id',
'product_id'
)
->withoutGlobalScopes()
->whereIn((new(config('rapidez.models.category_product')))->qualifyColumn('visibility'), [2, 4]);
}

public function rewrites(): HasMany
{
return $this
Expand Down
13 changes: 13 additions & 0 deletions src/Models/CategoryProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rapidez\Core\Models;

class CategoryProduct extends Model
{
protected $primaryKey = 'entity_id';

public function getTable()
{
return 'catalog_category_product_index_store' . config('rapidez.store');
}
}

0 comments on commit eeae34b

Please sign in to comment.