Mysql 如何仅显示每个属性
我正试图用PRESTASHOP创建一个光学商店,但我面临一个问题。我在产品和客户表中创建了4个新列(左眼屈光度、右眼屈光度、桥长、腿长),在两个表中都是相同的 我想做的是,当商店装载产品时,比较这些变量,如果它们相同,则显示产品。这是为了尝试向客户过滤眼镜,只向客户展示与其兼容的眼镜 原始查询是下一个查询:Mysql 如何仅显示每个属性,mysql,filter,prestashop,prestashop-1.6,Mysql,Filter,Prestashop,Prestashop 1.6,我正试图用PRESTASHOP创建一个光学商店,但我面临一个问题。我在产品和客户表中创建了4个新列(左眼屈光度、右眼屈光度、桥长、腿长),在两个表中都是相同的 我想做的是,当商店装载产品时,比较这些变量,如果它们相同,则显示产品。这是为了尝试向客户过滤眼镜,只向客户展示与其兼容的眼镜 原始查询是下一个查询: $sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) AS quantity'.
$sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) AS quantity'.(Combination::isFeatureActive() ? ', IFNULL(product_attribute_shop.id_product_attribute, 0) AS id_product_attribute,
product_attribute_shop.minimal_quantity AS product_attribute_minimal_quantity' : '').', pl.`description`, pl.`description_short`, pl.`available_now`,
pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, image_shop.`id_image` id_image,
il.`legend` as legend, m.`name` AS manufacturer_name, cl.`name` AS category_default,
DATEDIFF(product_shop.`date_add`, DATE_SUB("'.date('Y-m-d').' 00:00:00",
INTERVAL '.(int)$nb_days_new_product.' DAY)) > 0 AS new, product_shop.price AS orderprice
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p
ON p.`id_product` = cp.`id_product`
'.Shop::addSqlAssociation('product', 'p').
(Combination::isFeatureActive() ? ' LEFT JOIN `'._DB_PREFIX_.'product_attribute_shop` product_attribute_shop
ON (p.`id_product` = product_attribute_shop.`id_product` AND product_attribute_shop.`default_on` = 1 AND product_attribute_shop.id_shop='.(int)$context->shop->id.')':'').'
'.Product::sqlStock('p', 0).'
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
ON (product_shop.`id_category_default` = cl.`id_category`
AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
ON (p.`id_product` = pl.`id_product`
AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').')
LEFT JOIN `'._DB_PREFIX_.'image_shop` image_shop
ON (image_shop.`id_product` = p.`id_product` AND image_shop.cover=1 AND image_shop.id_shop='.(int)$context->shop->id.')
LEFT JOIN `'._DB_PREFIX_.'image_lang` il
ON (image_shop.`id_image` = il.`id_image`
AND il.`id_lang` = '.(int)$id_lang.')
LEFT JOIN `'._DB_PREFIX_.'manufacturer` m
ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE product_shop.`id_shop` = '.(int)$context->shop->id.'
AND cp.`id_category` = '.(int)$this->id
.($active ? ' AND product_shop.`active` = 1' : '')
.($front ? ' AND product_shop.`visibility` IN ("both", "catalog")' : '')
.($id_supplier ? ' AND p.id_supplier = '.(int)$id_supplier : '');
我试图修改它,但我不太清楚如何修改。接着,我做了错事。我已将下一个左连接添加到查询中
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON pa.`id_product` = p.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute` attr ON attr.`id_attribute` = pac.`id_attribute`
LEFT JOIN `'._DB_PREFIX_.'attribute_lang` attr_lang ON (attr_lang.`id_attribute` = pac.`id_attribute` AND attr_lang.`id_lang` = '.(int)$id_lang.')LEFT JOIN `'._DB_PREFIX_.'attribute_group` attr_group ON attr_group.`id_attribute_group` = attr.`id_attribute_group`
LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` attr_group_lang ON attr_group_lang.`id_attribute_group` = attr.`id_attribute_group`
谢谢你的建议
编辑:
产品的新字段是prestashop中的功能(product table中没有),很抱歉这个错误
我把prestashop的数据模型放在这里供你参考
编辑2:
我现在正试图通过使用一个模块来实现这一点,因此我的模块的php文件包含下一个代码。我遵循了CategoryController.php中的代码,但是如果不满足条件,我不知道如何删除产品
<?php
if (!defined('_PS_VERSION_'))
exit;
class glassOptics extends Module
{
/* @var boolean error */
protected $_errors = false;
public function __construct()
{
$this->name = 'glassOptics';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'MAOL';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('glassOptics');
$this->description = $this->l('...');
}
public function install()
{
if (!parent::install() OR
!$this->veopticasCustomerDB('add') OR
!$this->veopticasProductDB('add') OR
!$this->registerHook('hookActionProductListOverride')
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall() OR !$this->veopticasCustomerDB('remove') OR !$this->veopticasProductDB('remove'))
return false;
return true;
}
public function veopticasCustomerDB($method)
{
switch ($method) {
case 'add':
$sql = 'CREATE TABLE IF EXISTS `'._DB_PREFIX_.'customer_optics_data` (
`id_customer` int(10) UNSIGNED NOT NULL,
`left_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000',
`right_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000',
`bridge` decimal(20,6) NOT NULL DEFAULT '0.000000',
`leg` decimal(20,6) NOT NULL DEFAULT '0.000000',
`glass_width` decimal(20,6) NOT NULL DEFAULT '0.000000',
`glass_height` decimal(20,6) NOT NULL DEFAULT '0.000000'
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';
break;
case 'remove':
$sql = 'DROP TABLE IF EXISTS `'._DB_PREFIX_ . 'customer_optics_data`';
break;
}
if(!Db::getInstance()->Execute($sql))
return false;
return true;
}
public function veopticasProductDB($method)
{
switch ($method) {
case 'add':
$sql = 'CREATE TABLE IF EXISTS `'._DB_PREFIX_.'product_optics_data` (
`id_product` int(10) UNSIGNED NOT NULL,
`left_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000',
`right_dioptrics` decimal(20,6) NOT NULL DEFAULT '0.000000',
`bridge` decimal(20,6) NOT NULL DEFAULT '0.000000',
`leg` decimal(20,6) NOT NULL DEFAULT '0.000000',
`glass_width` decimal(20,6) NOT NULL DEFAULT '0.000000',
`glass_height` decimal(20,6) NOT NULL DEFAULT '0.000000'
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8';
break;
case 'remove':
$sql = 'DROP TABLE IF EXISTS `'._DB_PREFIX_ . 'product_optics_data`';
break;
}
if(!Db::getInstance()->Execute($sql))
return false;
return true;
}
public function hookActionProductListOverride($params)
{
$customer_settings = glassOptics::getCustomerSettings($this->context->customer);
if ($customer_settings) {
// Inform the hook was executed
$params['hookExecuted'] = true;
// Filter products here, you are now overriding the default
// functionality of CategoryController class.
// You can see blocklayered module for more details.
if ((isset($this->context->controller->display_column_left) && !$this->context->controller->display_column_left)
&& (isset($this->context->controller->display_column_right) && !$this->context->controller->display_column_right))
return false;
global $smarty;
if (!Configuration::getGlobalValue('PS_LAYERED_INDEXED'))
return;
$categories_count = Db::getInstance()->getValue('
SELECT COUNT(*)
FROM '._DB_PREFIX_.'layered_category
WHERE id_category = '.(int)Tools::getValue('id_category', Tools::getValue('id_category_layered', Configuration::get('PS_HOME_CATEGORY'))).'
AND id_shop = '.(int) Context::getContext()->shop->id
);
if ($categories_count == 0)
return;
// List of product to overrride categoryController
$params['catProducts'] = array();
$selected_filters = $this->getSelectedFilters();
$filter_block = $this->getFilterBlock($selected_filters);
$title = '';
if (is_array($filter_block['title_values']))
foreach ($filter_block['title_values'] as $key => $val)
$title .= ' > '.$key.' '.implode('/', $val);
$smarty->assign('categoryNameComplement', $title);
$this->getProducts($selected_filters, $params['catProducts'], $params['nbProducts'], $p, $n, $pages_nb, $start, $stop, $range);
// Need a nofollow on the pagination links?
$smarty->assign('no_follow', $filter_block['no_follow']);
foreach ($params['nbProducts'] as $product) {
$product_settings = glassOptics::getProductSettings($product);
if($product_settings){
$same_bridge = ($product_settings->bridge == $customer_settings->bridge ? true : false);
$same_leg = ($product_settings->leg == $customer_settings->leg ? true : false);
$same_glass_width = ($product_settings->glass_width == $customer_settings->glass_width ? true : false);
$same_glass_heigth = ($product_settings->glass_heigth == $customer_settings->glass_heigth ? true : false);
}
}
}
}
}
我建议采用不同的方法,并为此使用一个模块。您可以创建一个模块,在安装时创建一个表,类似于customer\u optics\u data
。表结构可以如下所示:
CREATE TABLE `'._DB_PREFIX_.'customer_optics_data` (
`id_customer` int(10) UNSIGNED NOT NULL,
`left_eye_diopter` int(10) UNSIGNED NOT NULL,
`right_eye_diopter` int(10) UNSIGNED NOT NULL,
`bridge_length` decimal(20,6) NOT NULL DEFAULT '0.000000',
`leg_length` decimal(20,6) NOT NULL DEFAULT '0.000000'
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;
然后,您的模块将钩住actionProductListOverride
hook,您将在这里执行检查:
public function hookActionProductListOverride($params)
{
$customer_settings = MyDiopterModuleHelperClass::getCustomerSettings($this->context->customer);
if ($customer_settings) {
$params['hookExecuted'] = true;
// Filter products here, you are now overriding the default
// functionality of CategoryController class.
// You can see blocklayered module for more details.
}
}
该模块将有一个助手类MyDiopterModuleHelpClass
,用于向customer\u optics\u data
表注册和获取数据。
通过这种方式,您不会覆盖核心,您的更新仍将正常运行,最糟糕的情况是,如果挂钩突然从未来版本的PrestaShop中移除,这是不可能的
该模块还将采用以下挂钩:
displayCustomerIdentityForm
-在My Personal information
中显示其他字段。这是您的客户为模块输入信息的地方
actionObjectCustomerAddAfter
-在这里,您可以从$\u POST
获取数据并将其保存在模块的表中
actionObjectCustomerUpdateAfter
-如果客户更改了数据,您可以在此处更新数据,如果由于某种原因数据不存在,则插入数据
或者,您也可以将模块挂接到
displayAdminCustomersForm
-在后台办公室的customers
表单中显示其他字段。我建议采用不同的方法并使用模块。您可以创建一个模块,在安装时创建一个表,类似于customer\u optics\u data
。表结构可以如下所示:
CREATE TABLE `'._DB_PREFIX_.'customer_optics_data` (
`id_customer` int(10) UNSIGNED NOT NULL,
`left_eye_diopter` int(10) UNSIGNED NOT NULL,
`right_eye_diopter` int(10) UNSIGNED NOT NULL,
`bridge_length` decimal(20,6) NOT NULL DEFAULT '0.000000',
`leg_length` decimal(20,6) NOT NULL DEFAULT '0.000000'
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8;
然后,您的模块将钩住actionProductListOverride
hook,您将在这里执行检查:
public function hookActionProductListOverride($params)
{
$customer_settings = MyDiopterModuleHelperClass::getCustomerSettings($this->context->customer);
if ($customer_settings) {
$params['hookExecuted'] = true;
// Filter products here, you are now overriding the default
// functionality of CategoryController class.
// You can see blocklayered module for more details.
}
}
该模块将有一个助手类MyDiopterModuleHelpClass
,用于向customer\u optics\u data
表注册和获取数据。
通过这种方式,您不会覆盖核心,您的更新仍将正常运行,最糟糕的情况是,如果挂钩突然从未来版本的PrestaShop中移除,这是不可能的
该模块还将采用以下挂钩:
displayCustomerIdentityForm
-在My Personal information
中显示其他字段。这是您的客户为模块输入信息的地方
actionObjectCustomerAddAfter
-在这里,您可以从$\u POST
获取数据并将其保存在模块的表中
actionObjectCustomerUpdateAfter
-如果客户更改了数据,您可以在此处更新数据,如果由于某种原因数据不存在,则插入数据
或者,您也可以将模块挂接到
displayAdminCustomersForm
-在后台办公室的customers
表单中显示其他字段。请显示数据库架构。@davidstrachan我刚刚添加了数据模型以了解关系。我已经给需要连接的表上色了。你不应该弄乱核心数据库结构,这会给你的更新带来麻烦。为什么不能简单地添加两个附加表,如customer\u diopter
和product\u diopter
,然后使用内置的覆盖机制来改变选择产品的方式?或者甚至更好-将产品的屈光度
全部去掉,用功能/属性替换,并构建一个简单的过滤器,从客户的屈光度
表中提取客户的值,然后选择具有匹配功能的产品?@Eihwaz你能给我解释一下你的想法吗?现在,我愿意接受任何解决这个问题的方法。我需要能够添加新产品并修改此参数。@MAOL我将添加一个答案,因为它太长,不适合评论,请给我一分钟。请显示数据库架构。@davidstrachan我刚刚添加了数据模型以了解关系。我已经给需要连接的表上色了。你不应该弄乱核心数据库结构,这会给你的更新带来麻烦。为什么不能简单地添加两个附加表,如customer\u diopter
和product\u diopter
,然后使用内置的覆盖机制来改变选择产品的方式?或者甚至更好-将产品_diopter
全部去掉,用功能/属性替换,并构建一个简单的过滤器,从您那里获取客户的价值