Magento EAV:如何硬删除属性值? [英] Magento EAV: how to hard delete an attribute value?

查看:145
本文介绍了Magento EAV:如何硬删除属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在输入详细信息之前,让我们清楚地问这个问题:

Let's ask the question clearly before entering into the details:

hard 的意思是,从数据库中删除该行,而不仅将值设置为null或为空.

By hard I mean, removing the row from the database and not only setting the value to null or empty.

我目前在Magento上遇到问题.

I'm currently confronted to a problem on Magento.

我的商店中的许多产品都具有用户定义的属性,这些属性与该产品无关.例如,假设我们有一个 bag 产品,还有诸如 t恤,连衣裙,裤子等其他产品:

Many product in my store have user defined attributes that are not related to the product. For example, let's say we have a bag product, among other products like t-shirts,dresses,pants,etc.:

我们的 bag 产品只有1个可配置属性:颜色.

Our bag product has only configurable 1 attribute: color.

我们的 T恤产品具有2个可配置的属性:颜色 tshirt_size .

Our T-Shirt product has 2 configurable attributes: color and tshirt_size.

我们的连衣裙产品具有2个可配置的属性:颜色 dress_size

Our Dress product has 2 configurable attributes: color and dress_size

我当前的问题是,当我从数据库中检索 Bag 产品时,存储在我的产品中的属性是:颜色 tshirt_size dress_size .

My current problem is that when I retrieve my Bag product from the database, the attributes stored in my product are: color, tshirt_size and dress_size.

我怀疑是因为在我们的商店中您可以更改产品的属性集(这要归功于FlagBit ChangeAttributeSet社区扩展).

I suspect that it is because on our store you can change the attribute set of a product (thanks to the FlagBit ChangeAttributeSet community extension).

我认为袋子产品是用特定的属性集创建的,然后有人将属性集更改为另一个属性集,然后又更改为另一个属性集.这将导致产品从已更改为的所有属性集中获取所有属性.但这只是一个问题,这可能是另一个原因.我目前实际上不是在寻找导致我的产品具有不相关属性的具体原因.

I think that the bag product has been created with a specific attribute set, then someone changed the attribute set to another, and another again. That would have lead the product to get ALL the attributes from ALL the attribute sets it has been changed to. But that's just a though, and it could be another reason. I'm actually currently not looking for the specific reason that led my product to have unrelated attributes.

我今天想重置我的产品.重置是指删除与我的产品无关的属性.就我的 bag 而言,这意味着删除 tshirt_size dress_size 属性. 删除"是指从对象中永久删除这些属性.

I today want to reset my products. By reset I mean removing the attributes unrelated to my product. In the case of my bag, that means removing the tshirt_size and dress_size attributes. And by removing I mean deleting forever these attributes from the object.

我的问题是,我找不到方法.我尝试将属性设置为NULL并保存产品,但是当我再次查询产品时,我仍然在数据中获得该属性,且值为空.我不希望该属性值为null,我希望该属性不存在.

My problem is that, I can't find how to do it. I tried setting the attributes to NULL and saving the product, but when I query again the product, I still get the attribute in the datas, with a null value. I don't want the attribute value to be null, I want the attribute to not exist.

这是一段代码示例,对我的尝试进行了一些解释:

Here is a sample of code that explains a bit what I tried:

$product = Mage::getModel('catalog/product')->load(1234); //Let's assume that my bag product ID is 1234

Mage::log($product->getData());

/* This last line dump all my products datas and contains among other things:

[...]=>...
[color]=>3
[tshirt_size]=>34
[dress_size]=>45
[...]=>...

*/

如果我这样做:

$product->setData('tshirt_size',null);
$product->setData('dress_size',null);
$product->save();

然后再次:

$product = Mage::getModel('catalog/product')->load(1234);
 Mage::log($product->getData());

    /* I get:

    [...]=>...
    [color]=>3
    [tshirt_size]=>null
    [dress_size]=>null
    [...]=>...

    */

我不要那个.我想要:

/*

[...]=>...
[color]=>3
[...]=>...

*/

我的数组中仍然有条目的事实意味着不会删除数据库中的实际行.我希望删除行.

The fact that I still have the entries in my array mean that the actual rows in the database are not deleted. I want the rows to be deleted.

当我这样做时,更令人沮丧的是:

Even more frustrating, when I do a:

$product->getAttributes();

它返回给我所有可以设置为我的产品的属性,没有不相关的属性-这是有道理的,因为不相关的属性不在我产品的属性集中.

It returns me all the attributes that can be set to my product without the unrelated attributes -which makes sense because the unrelated attributes are not in the attribute set of my product.

因此,问题再次是:

hard 的意思是,从数据库中删除该行,而不仅将值设置为null或为空.

By hard I mean, removing the row from the database and not only setting the value to null or empty.

感谢您的帮助!

胡格斯

仅供参考:我使用的是Magento 1.6.1.0(但它并没有真正改变任何内容)

FYI: I'm using Magento 1.6.1.0 (but it does not really change anything)

推荐答案

我终于不得不解决这个问题,尽管@vBuck已经关闭,但是我总是能够在切换属性集后查询旧数据,如果我切换回原本不应该存在的地方,我会看到旧数据.今天,我同样需要确保删除随机数据.这是我处理此主题的方法.

I finally had to tackle this, and although @vBuck was close, I always was able to query the old data after the switch in the attribute set and if I switched back I would see the old data when it shouldn't have been there. Today, I have the same need to ensure the random data is removed. Here is how I approached this topic.

为了在从一个集合切换到另一个集合时以编程方式删除属性,必须首先查看新集合的所有条目,然后获取所有属性的列表,并对照该列表进行检查(这是重要的部分)从正确的实体表中删除它.例如,当我将Western Digital 500GB HD - 7200RPMHard Drive切换到Shoes时,我想摆脱样本Magento数据中的500 GB值.为此,我必须删除存储在catalog_product_entity_text表中的条目.这意味着我必须在与产品ID相等的位置找到该条目,然后从其实体类型表中将其删除.

In order to programmatically delete attributes when you switch from one set to another you must first look at all the entries for the new set, then get a list of all attributes, check against that list and (this is the important part) remove it from the correct entity table. For example, I want to get rid of a value of 500 GB that is in the sample Magento data when I switch Western Digital 500GB HD - 7200RPM from Hard Drive to Shoes. In order to do that I must remove the entry in the catalog_product_entity_text table it's stored at. This means that I must find that entry where it's equal to the product id and then delete it from it's entity type table.

我能够做到这一点,但之后却找不到流浪数据.数据库搜索和切换回旧属性集并寻找数据均证实了这一点.您可以在这里找到所有内容

I was able to do this, and was not able to find the stray data after. This was confirmed by both a DB search and switching back to the old attribute set and looking for the data. you can find it all here

https://gist.github.com/jeremyBass/6581038#file-attribute-clean-switch-md

在CE 1.7.0.2、1.8.0.0中进行了测试

这篇关于Magento EAV:如何硬删除属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆