沃梦达 / 编程问答 / php问题 / 正文

保存、组织和查询产品、选项/标签和类别

2022-01-01 5

Saving, organizing and querying products, options/tags and categories(保存、组织和查询产品、选项/标签和类别)

本文介绍了保存、组织和查询产品、选项/标签和类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,让我明确一点,我不要求任何代码;我只是想要一些关于如何实施我将要提出的问题的一般性想法/指导/意见.

First of all, let me make clear that I'm not asking for any code; I just wan't some general ideas/guidance/opinions about how could I implement what I'm about to ask.

我正在开始构建一个在线电子商务系统(Yii2 + MongoDB,所以,PHP + NoSQL),并且有两个必要条件我不完全确定如何在不造成巨大混乱的情况下实现我的代码和数据库.

I'm starting to build an online e-commerce system (Yii2 + MongoDB, so, PHP + NoSQL), and there are two requisites that I'm not entirely sure how to implement without creating a huge mess in both my code and the database.

这两个必要条件是相关的,所以我将把它们解释为一个.

Both requisites are related, so I'll explain them both as one.

与任何其他严肃的电子商务一样,它也有类别.而且,与任何其他严肃的电子商务一样,每个产品都会有 tagsoptions.让我进一步解释一下我所说的 tags/options.

As any other serious e-commerce, it would have categories. And also, as any other serious e-commerce, each product will have tags or options. Let me explain a little bit further what I call tags/options.

这些是用户在购买产品时可以选择的选项,例如颜色或尺寸、材料等.

Those are the available options that a user could select when buying a product, for example, the color or the size, the material, etc.

  1. 类别

将有多个 general 类别以及其他子类别.例如,Electronics 可以是一般类别,而子类别可以是 ComputersSmart TVs.那么,MotherboardsRAM 可以是 Computers 的子类别.

There would be multiple general categories as well as other sub-categories. For example, Electronics could be a general category and sub-categories would be Computers and Smart TVs. Then, Motherboards and RAM could be sub-categories of Computers.

这本身可以很容易地存储在数据库中,但问题就在这里:

This by itself could be easily stored in a database, but here it goes the problem:

  • 每个产品在列出它所属的任何类别或上层类别时都应该出现.这意味着如果我(作为最终用户)浏览Computers 类别中的所有项目,我应该看到属于子类别Graphic cardNVIDIA GTX670类别计算机的code>.
  • Each product should appear when listing any of the categories it belongs to, or the upper categories. That means that if I (as the final user) browse all the items in Computers category, I should see NVIDIA GTX670 which belongs to the subcategory Graphic cards of the category Computers.

我可以通过以下方式保存每个产品:

I could save each product the following way:

{
    _id: asdasfwetrw34tw34t245y45y,
    name: "NVIDIA GTX670",
    price: 99.50,
    ...
    ...
    categories: [
        "Electronics", //<-- just the ID of that group
        "Computers", //<-- just the ID of that group
        "Graphic cards" //<-- just the ID of that group
    ]
}

但是:

  • 我不确定检索某个类别的所有项目(当然还有所有子类别的项目)的查询速度有多快.
  • 我不确定该方法还有哪些其他缺点,因此,请随时推荐用于存储此内容的任何替代架构.


2. 标签/选项

这才是真正令人头疼的地方.

This is where the real headache is.

每个选项可以属于0个或多个类别和子类别,因此类别Woman fashion可以有选项sizecolor,但是类别Sunglasses(Woman fashion的子类别)可以只有color,甚至是另一组选项,与Woman fashion完全不同.

Each option could belong to 0 or more categories and subcategories, so the category Woman fashion could have the options size and color, but the category Sunglasses (subcategory of Woman fashion) could have only color, or even another set of options, completely different from Woman fashion.

此外,每个选项中的值(redgreenbluecolor 选项中)可以出现在随机类别中.所以Woman fashion会有Strawberry RedTangerine这样的颜色,而Cars会有Carboncode> 和 黑色金属.

Furthermore, the values inside each option (red, green, blue in the color option) could appear in random categories. So Woman fashion would have colors like Strawberry Red and Tangerine, while Cars would have Carbon and Black metallic.

此外,还有几种类型的选项:

Also, there would be several types of options:

  • 完全静态(如size,只能是SM,但不能同时是两者.无论如何,管理员不会无法编写自定义大小,例如Kind of small;他只能选择数据库中已有的内容).
  • 可以组合在一起的静态(如 colors,可以是 redgreen,或者管理员选择的颜色组合).
  • 自由输入(如 dimensionsweight,理想情况下,它是要加入的输入字段和下拉值.例如 [10] | (mg||kg|tons)[20] (cm|m|km|miles)).莉>
  • Completely static (like size, which could be only S or M, but never both. In any case, the administrator won't be able to write a custom size, like Kind of small; he would be able to just select what it's already in the database).
  • Static that can combine together (like colors, which could be red or green, or a combination of colors that the admin chooses).
  • Free-input (like dimensions or weight, that would, ideally, be input fields and dropdown values to join with. For example [10] | (mg||kg|tons) or [20] (cm|m|km|miles)).


我可以像这样保存每个选项:


I could save each option like this:

{
    option: "Color",
    type: "Static with combinations"
    values: [
        {
            value: "Red",
            categories: [
                "Sunglasses"
            ]
        },
        {
            value: "Green",
            categories: [
                "Sunglasses",
                "T-Shirts"
            ]
        },
        {
            value: "Black metallic",
            categories: [
                "Cars"
            ]
        }
    ],
    categories: [
        "Woman fashion", //<-- only the ID of this group
        "Cars" //<-- only the ID of this group
    ]
}

但是我担心单个选项会变成多大,当有 30 个类别并且选项的每个值都设置为出现在随机类别中时.
另外我只是觉得它不够干净,但也许这只是我自己.

But I'm worried about how big a single option could turn to be, when there are 30 categories and each value of the option is set to appear in random categories.
Also I just don't see it clean enough, but maybe that is just me.

无论如何,与前一点一样,请随时提出任何可以提出的建议,我将不胜感激您能给我的任何反馈.

Anyways, as with the previous point, please feel free to suggest anything to can come up with, I'll greatly appreciate any feedback that you can give me.

推荐答案

我也在运营一个电子商务网站.这是我关于如何实现您提到的功能的建议.希望有帮助.

I'm running a e-commerce website, too. Here's my advice on how I implement the features you mentioned. Hope it helps.

  • 类别

我将它们组织成一个扁平的结构,在你的情况下是:

I organize them in a flat structure, in your case it would be:

    {_id: 1, name: "Electronics", parentId: 0, idPath: "/0/1/" ...}
    {_id: 2, name: "Computers", parentId: 1, idPath: "/0/1/2/", ...}
    {_id: 3, name: "Graphic Cards", parentId: 2, idPath: "/0/1/2/3/", ...}

产品现在只需要在叶子类别中.在你的情况下:

And the product now needs to be only in the leaf categories. In your case:

    {
        _id: asdasfwetrw34tw34t245y45y,
        name: "NVIDIA GTX670",
        price: 99.50,
        ...
        ...
        categoryIds: [3]
    }

产品当然可以属于多个类别,所以categoryIds 仍然是一个数组.这是棘手的部分.当您列出 Electronics 类别时,您可以通过以下方式找到其所有子类别:

The product can be in multiple categories of course, so the categoryIds remains an array. Here's the tricky part. When you list the Electronics category, you can find all its subcategories by:

    db.categories.find({idPath: /^/0/1
版权声明:本站部分内容来源互联网,如果文章中所涉及的图片或者文字内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!
沃梦达教程 本文标题为:保存、组织和查询产品、选项/标签和类别 复制本文链接 var clipboard = new Clipboard('.itemCopy'); clipboard.on('success', function(e) { if (e.trigger.disabled == false || e.trigger.disabled == undefined) { e.trigger.innerHTML = "<i class='iconfont icon-attachment'></i>链接复制成功"; e.trigger.disabled = true; setTimeout(function() { e.trigger.innerHTML = "<i class='iconfont icon-attachment'></i>复制本文链接"; e.trigger.disabled = false; }, 2000); } }); clipboard.on('error', function(e) { e.trigger.innerHTML = "链接复制失败"; }); 上一篇: Yii2:使用带分页的 Pjax POST 方法在 Gridview 中搜索 下一篇: Yii2:Jui Auto Complete Widget 怎么做? 基础教程推荐 学习HTML 学习jQuery 学习Laravel 学习CSS3 学习Vue.js 学习Bootstrap5 学习ThinkPHP 学习AJAX 猜你喜欢 没有作曲家的 PSR4 自动加载 2022-01-01 PHP Count 布尔数组中真值的数量 2021-01-01 从 PHP 中的输入表单获取日期 2022-01-01 正确分离 PHP 中的逻辑/样式 2021-01-01 带有通配符的 Laravel 验证器 2021-01-01 Laravel 仓库 2022-01-01 SoapClient 设置自定义 HTTP Header 2021-01-01 如何定位 php.ini 文件 (xampp) 2022-01-01 Mod使用GET变量将子域重写为PHP 2021-01-01 Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
最新文章 2023-09-26 PHP中如何设置Cookie变量? 2023-09-25 PHP中如何判断当前用户是否已经登录? 2023-09-25 PHP中如何销毁当前用户的会话? 2023-09-25 PHP中如何删除当前用户的会话变量? 2023-09-25 PHP中如何设置当前用户的会话变量? 热门文章 预留广告位,火热招商中! 基础教程 学习JavaScript 学习Python 学习Android 学习HTML5 热门标签 dedecms 织梦 php 正则表达式 匹配 video 跨域问题 区块链 定时任务 资源映射 设计模式 代理模式 EasyExcel 运行原理 归并排序 排序算法 希尔排序 快速排序 冒泡排序 经典算法 Nacos 对象转换 Springboot mysql 远程连接 过滤器 监听器 多进程 图片上传 无刷新 wushuaxi wusu wus 加载 url编码 jdk Socket 循环语句 对象池
沃梦达教程网是一个专门为菜鸟打造的前端开发和软件编程的学习网站,可以为编程者和程序员提供海量技术文档,以便编程初学者快速入门,提升开发技术水平和工作效率。 编程基础 HTML/CSS JAVASCRIPT PHP JAVA ASP.NET Python 编程教程 编程基础IT编程编程问答实例代码 © 2023-2024 沃梦达 版权所有并保留所有权 网站地图 ICP备案号:粤ICP备14083021号 网站首页 HTML/CSS 菜单 Layui Yii2 网站首页HTML/CSS HTMLHTML5CSSCSS3SassLayuiBootstrap3Bootstrap4Bootstrap5AppMLFont AwesomeFoundation5 JAVASCRIPT JavaScriptjQueryAngularJSAngularJS2Vue.jsVue3ReactTypeScriptEasyUINode.jsAJAXJSONEchartsHighcharts PHP PHPLaravelCakePHPCodeIgniterZendSymfonyYii2PhalconThinkPHPSmarty JAVA JAVASpringHibernateStrutsPlayGWTJspMavenServlet ASP.NET ASP.NETC#Web PagesRazorMVCWeb Forms Python PythonPython 3Django Go GoDockerBeegoBuffaloEchoGinIrisRevel Ruby C C++ Perl Lua Rust Scala VB 移动端 AndroidSwiftionicKotlinHarmonyOS
.night .wmd-logo-nav-night{ background: url(https://www.womengda.net/xwassets/images/flogo.png) no-repeat!important; background-size: 150px auto; } .night .wmd-app-logo2{ background: url(https://www.womengda.net/xwassets/images/flogo.png) no-repeat!important; background-size: 92px auto!important; } #canvas { position: absolute; left: 0; top: 0; }