Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product relations for categories #353

Merged
merged 8 commits into from
Oct 11, 2023
1 change: 1 addition & 0 deletions config/rapidez.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
'attribute' => Rapidez\Core\Models\Attribute::class,
'product' => Rapidez\Core\Models\Product::class,
'category' => Rapidez\Core\Models\Category::class,
'category_product' => Rapidez\Core\Models\CategoryProduct::class,
'config' => Rapidez\Core\Models\Config::class,
'option_swatch' => Rapidez\Core\Models\OptionSwatch::class,
'option_value' => Rapidez\Core\Models\OptionValue::class,
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/IndexCategoriesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ 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']))
->withCount('productIndices')
->whereNotNull('url_key')
->whereNot('url_key', 'default-category')
->where('children_count', '>', 0)
->having('product_indices_count', '>', 0)
Jade-GG marked this conversation as resolved.
Show resolved Hide resolved
->get() ?? [];
}
}
25 changes: 25 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,30 @@ public function subcategories()
return $this->hasMany(self::class, 'parent_id', 'entity_id');
}

public function productIndices(): HasMany
{
return $this->hasMany(
config('rapidez.models.category_product'),
'category_id',
'entity_id'
)
->whereIn((new(config('rapidez.models.category_product')))->qualifyColumn('visibility'), [2, 4]);
}

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]);
royduin marked this conversation as resolved.
Show resolved Hide resolved
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't very DRY, is there maybe a nice way to combine productIndices into this relation?


public function rewrites(): HasMany
{
return $this
Expand Down
13 changes: 13 additions & 0 deletions src/Models/CategoryProduct.php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CategoryProduct may conflict in name as there, i personally tend to stick close to the original table name like seen here https://github.com/Genaker/laragento/tree/main/src/Models

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's tough because then we also should rename the Product and Category models to CatalogProduct and CatalogCategory. With how it's set up right now, I feel like this one specifically is more consistent with the others and if this is something we want to uphold in the future we should probably change this in a dedicated PR so it shows up in the changelog.

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');
}
}
Loading