【问题标题】:Magento user created attribute for products is not savedMagento 用户为产品创建的属性未保存
【发布时间】:2010-03-10 16:41:41
【问题描述】:

我现在正在与一件看似简单的事情作斗争大约两天。希望有人能帮忙。

我创建了 myUpdate EAV 属性

class Company_Module_Model_Resource_Eav_Mysql4_Setup extends Mage_Eav_Model_Entity_Setup
{
    public function getDefaultEntities()
    {
        return array(
            'catalog_product' => array(
                'entity_model'      => 'catalog/product',
                'attribute_model'   => 'catalog/resource_eav_attribute',
                'table'             => 'catalog/product',
                'additional_attribute_table' => 'catalog/eav_attribute',
                'entity_attribute_collection' => 'catalog/product_attribute_collection',
                'attributes'        => array(
                    'my_update' => array(
                        'label'             => 'My timestamp',
                        'type'              => 'datetime',
                        'input'             => 'date',
                        'default'           => '',
                        'class'             => 'validate-date',
                        'backend'           => 'eav/entity_attribute_backend_datetime',
                        'frontend'          => '',
                        'table'             => '',
                        'source'            => '',
                        'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        'visible'           => true,
                        'required'          => false,
                        'user_defined'      => true,
                        'searchable'        => false,
                        'filterable'        => false,
                        'comparable'        => false,
                        'visible_on_front'  => false,
                        'visible_in_advanced_search' => false,
                        'unique'            => false,
                        'apply_to'          => 'simple',
                    )
                )
            )
        );
    }
}

属性创建成功,安装时正确显示在属性列表中。

以后再做

// for product 1
$product->setMyUpdate($stringDate); // string this format: yyyy-MM-dd HH:mm:ss
$product->save(); // and saves without issues * in admin module

但后来当我这样做时:

$product = Mage::getModel('catalog/product')->load(1);
var_dump($product->getMyUpdate()); // returns null

不知何故,数据并没有真正保存..或者我没有正确检索它。 请就如何获取数据以及数据在数据库中的保存位置提出建议,以便我检查插入是否正确完成。

谢谢。

【问题讨论】:

    标签: magento e-commerce


    【解决方案1】:

    在数据库中,属性应该保存在由其attribute_id组织的表catalog_product_entity_datetime中。首先获取 entity_type_id(因为我的显然与您的不同):

    select entity_type_id from eav_entity_type where entity_type_code = 'catalog_product';
    

    我在最后一个上得到 4,但在适用的情况下替换。以下查询应列出所有保存的值:

    select * from catalog_product_entity_datetime where attribute_id = (select attribute_id from eav_attribute where entity_type_id = 4 and attribute_code = 'my_update');
    

    为了确保您确实已将属性添加到数据库中:

    select attribute_id from eav_attribute where entity_type_id = 4 and attribute_code = 'my_update'
    

    如果您在该空间中看到值,您的问题可能是您没有选择要加载的属性。例如,如果您从目录产品集合中加载产品,则需要一个方法来加载属性:

    $collection->addAttributeToSelect("my_update");
    

    如果您发现数据已实际保存,请提供有关您的产品加载的更多背景信息。

    希望对您有所帮助。

    谢谢!乔

    【讨论】:

    • 我有 eav 属性,但有 10(目录/产品)而不是 4(销售/订单),并且 catalog_product_entity_datetime 中没有条目。
    • 成功了。事实证明,Magento 的人对 Objects 的看法与我不同。我认为在创建产品时获得自定义字段是正常的......
    【解决方案2】:

    默认情况下不会保存您的属性。您可以使用: $product->getResource()->saveAttribute($product, "name_of_your_attribute");

    【讨论】:

    • 和保存的值?如何指定值?
    • 你必须使用 $product->setYourValue('value'); 来指定你的价值。在“saveAttribute”行之前
    • 感谢您的回复,非常感谢。
    • 我试过了,但产品中的值仍然没有更新。
    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 2014-02-10
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多