Skip to content

Commit

Permalink
Wrap raw query parts (#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
indykoning authored Oct 10, 2023
1 parent 6a56d13 commit 3b04145
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 42 deletions.
12 changes: 6 additions & 6 deletions src/Models/OptionSwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public static function getCachedSwatchValues(): array

return Cache::rememberForever('swatchvalues', function () use ($swatchAttributes) {
return self::select('eav_attribute.attribute_code')
->selectRaw('JSON_OBJECTAGG(eav_attribute_option_value.option_id, JSON_OBJECT(
"label", COALESCE(eav_attribute_option_value_store.value, eav_attribute_option_value.value),
"sort_order", eav_attribute_option.sort_order,
"value", eav_attribute_option_value.option_id,
"swatch", eav_attribute_option_swatch.value
)) as options')
->selectRaw('JSON_OBJECTAGG(`eav_attribute_option_value`.`option_id`, JSON_OBJECT(
"label", COALESCE(`eav_attribute_option_value_store`.`value`, `eav_attribute_option_value`.`value`),
"sort_order", `eav_attribute_option`.`sort_order`,
"value", `eav_attribute_option_value`.`option_id`,
"swatch", `eav_attribute_option_swatch`.`value`
)) as `options`')
->join('eav_attribute_option', function ($query) use ($swatchAttributes) {
$query->on('eav_attribute_option.option_id', '=', 'eav_attribute_option_swatch.option_id')
->whereIn('eav_attribute_option.attribute_id', $swatchAttributes);
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected static function booting()
"item_id", quote_item.item_id,
"product_id", quote_item.product_id,
"sku", quote_item.sku,
"name", quote_item.name,
"name", quote_item.`name`,
"image", product.thumbnail,
"url_key", product.url_key,
"qty", quote_item.qty,
Expand Down
49 changes: 25 additions & 24 deletions src/Models/Scopes/Attribute/OnlyProductAttributesScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,45 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Support\Facades\DB;

class OnlyProductAttributesScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
$builder
->selectRaw('
eav_attribute.attribute_id AS id,
COALESCE(eav_attribute_label.value, frontend_label, eav_attribute.attribute_code) AS name,
eav_attribute.attribute_code AS code,
backend_type AS type,
frontend_input AS input,
source_model,
IF(eav_attribute.attribute_code IN ("price", "tax_class_id"), 0, is_searchable) AS search,
search_weight,
is_filterable AS filter,
is_comparable AS compare,
used_in_product_listing AS listing,
used_for_sort_by AS sorting,
is_visible_on_front AS productpage,
is_html_allowed_on_front AS html,
IF(source_model NOT LIKE "Magento%", 0, GREATEST(
->select(
'eav_attribute.attribute_id as id',
DB::raw('COALESCE(eav_attribute_label.value, frontend_label, eav_attribute.attribute_code) AS `name`'),
'eav_attribute.attribute_code AS code',
'backend_type AS type',
'frontend_input AS input',
'source_model',
DB::raw('IF(eav_attribute.attribute_code IN ("price", "tax_class_id"), 0, is_searchable) AS `search`'),
'search_weight',
'is_filterable AS filter',
'is_comparable AS compare',
'used_in_product_listing AS listing',
'used_for_sort_by AS sorting',
'is_visible_on_front AS productpage',
'is_html_allowed_on_front AS html',
DB::raw('IF(source_model NOT LIKE "Magento%", 0, GREATEST(
IF(backend_type = "static", 1, 0),
is_used_for_promo_rules,
used_in_product_listing,
used_for_sort_by,
IF(eav_attribute.attribute_code IN ("status", "required_options", "tax_class_id", "weight"), 1, 0)
)) AS flat,
EXISTS(
)) AS `flat`'),
DB::raw('EXISTS(
SELECT 1
FROM catalog_product_super_attribute
WHERE catalog_product_super_attribute.attribute_id = eav_attribute.attribute_id
) AS super,
additional_data->>"$.swatch_input_type" = "text" AS text_swatch,
additional_data->>"$.swatch_input_type" = "visual" AS visual_swatch,
additional_data->>"$.update_product_preview_image" = 1 AS update_image,
position
')
) AS `super`'),
DB::raw('additional_data->>"$.swatch_input_type" = "text" AS `text_swatch`'),
DB::raw('additional_data->>"$.swatch_input_type" = "visual" AS `visual_swatch`'),
DB::raw('additional_data->>"$.update_product_preview_image" = 1 AS `update_image`'),
'position'
)
->join('catalog_eav_attribute', 'eav_attribute.attribute_id', '=', 'catalog_eav_attribute.attribute_id')
->leftJoin('eav_attribute_label', function ($join) {
$join->on('eav_attribute.attribute_id', '=', 'eav_attribute_label.attribute_id')
Expand Down
5 changes: 3 additions & 2 deletions src/Models/Scopes/Product/WithProductAttributesScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function apply(Builder $builder, Model $model)
$model->getQualifiedCreatedAtColumn(),
]);

$grammar = $builder->getQuery()->getGrammar();
foreach ($attributes as $attribute) {
$attribute = (object) $attribute;

Expand All @@ -45,7 +46,7 @@ public function apply(Builder $builder, Model $model)
} else {
if ($attribute->input == 'select') {
$builder
->selectRaw('COALESCE(ANY_VALUE(' . $attribute->code . '_option_value_' . config('rapidez.store') . '.value), ANY_VALUE(' . $attribute->code . '_option_value_0.value)) AS ' . $attribute->code)
->selectRaw('COALESCE(ANY_VALUE(' . $grammar->wrap($attribute->code . '_option_value_' . config('rapidez.store') . '.value') . '), ANY_VALUE(' . $grammar->wrap($attribute->code . '_option_value_0.value') . ')) AS ' . $grammar->wrap($attribute->code))
->leftJoin(
'catalog_product_entity_' . $attribute->type . ' AS ' . $attribute->code,
function ($join) use ($builder, $attribute) {
Expand All @@ -68,7 +69,7 @@ function ($join) use ($attribute) {
);
} else {
$builder
->selectRaw('COALESCE(ANY_VALUE(' . $attribute->code . '_' . config('rapidez.store') . '.value), ANY_VALUE(' . $attribute->code . '_0.value)) AS ' . $attribute->code)
->selectRaw('COALESCE(ANY_VALUE(' . $grammar->wrap($attribute->code . '_' . config('rapidez.store') . '.value') . '), ANY_VALUE(' . $grammar->wrap($attribute->code . '_0.value') . ')) AS ' . $grammar->wrap($attribute->code))
->leftJoin(
'catalog_product_entity_' . $attribute->type . ' AS ' . $attribute->code . '_' . config('rapidez.store'),
function ($join) use ($builder, $attribute) {
Expand Down
3 changes: 2 additions & 1 deletion src/Models/Scopes/Product/WithProductChildrenScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ public function apply(Builder $builder, Model $model)
return $attribute['super'];
}), 'code');

$grammar = $builder->getQuery()->getGrammar();
$superAttributesSelect = '';
foreach ($superAttributes as $superAttribute) {
$superAttributesSelect .= '"' . $superAttribute . '", children.' . $superAttribute . ',';
$superAttributesSelect .= '"' . $superAttribute . '", ' . $grammar->wrap('children.' . $superAttribute) . ',';
}

$stockQty = config('rapidez.expose_stock') ? '"qty", children_stock.qty,' : '';
Expand Down
17 changes: 9 additions & 8 deletions src/Models/Scopes/Product/WithProductSuperAttributesScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ public function apply(Builder $builder, Model $model)
return $attribute['super'];
}), 'code', 'id');

$grammar = $builder->getQuery()->getGrammar();
foreach ($superAttributes as $superAttributeId => $superAttribute) {
$query = DB::table('catalog_product_super_link')
->selectRaw('JSON_OBJECTAGG(' . $superAttribute . ', JSON_OBJECT(
"sort_order", option.sort_order,
"label", ' . $superAttribute . '_value,
"value", ' . $superAttribute . '
)) AS ' . $superAttribute)
->selectRaw('JSON_OBJECTAGG(' . $grammar->wrap($superAttribute) . ', JSON_OBJECT(
"sort_order", `option`.`sort_order`,
"label", ' . $grammar->wrap($superAttribute . '_value') . ',
"value", ' . $grammar->wrap($superAttribute) . '
)) AS ' . $grammar->wrap($superAttribute))
->join($model->getTable() . ' AS children', 'children.entity_id', '=', 'catalog_product_super_link.product_id')
->join('catalog_product_super_attribute', function ($join) use ($superAttributeId) {
$join->on('catalog_product_super_attribute.product_id', '=', 'catalog_product_super_link.parent_id')
Expand All @@ -38,12 +39,12 @@ public function apply(Builder $builder, Model $model)

$query = DB::table('catalog_product_super_attribute')
->selectRaw('JSON_OBJECTAGG(eav_attribute.attribute_id, JSON_OBJECT(
"code", attribute_code,
"label", COALESCE(NULLIF(value, ""), frontend_label),
"code", `attribute_code`,
"label", COALESCE(NULLIF(`value`, ""), `frontend_label`),
"text_swatch", additional_data->>"$.swatch_input_type" = "text",
"visual_swatch", additional_data->>"$.swatch_input_type" = "visual",
"update_image", additional_data->>"$.update_product_preview_image" = 1
)) AS super_attributes')
)) AS `super_attributes`')
->join('eav_attribute', 'eav_attribute.attribute_id', '=', 'catalog_product_super_attribute.attribute_id')
->join('catalog_eav_attribute', 'catalog_eav_attribute.attribute_id', '=', 'catalog_product_super_attribute.attribute_id')
->leftJoin('catalog_product_super_attribute_label', function ($join) {
Expand Down

0 comments on commit 3b04145

Please sign in to comment.