扩展 Shopware 模型

Extends Shopware Models

我需要扩展 Shopware 变体模型以添加一些自定义属性,例如金属类型、宝石类型和宝石,这是基础物品。 这些属性将在后端和前端中使用。

我该怎么做?谢谢

扩展 Shopware 核心模型本身是根本不可能的。根据您尝试扩展的特定模型,某些解决方法有两种不同的方法:

  1. 如果您想要扩展的是文章本身,您可以使用此处所述的自定义属性字段:http://community.shopware.com/Anlegen,-Anpassen-und-Ausgabe-von-Artikel-Attributen_detail_1208.html

  2. 另一种方法是编写一个插件,您可以在插件 install() 上通过代码创建属性字段。这仅适用于具有属于实体本身的属性 table 的实体。例如 s_order 和 s_order_attributes

对于第二种方法,在插件的 Bootstrap.php 中创建一个方法,如下所示,并在插件的 install() 方法中调用该方法:

public function installOrderAttributes()
{
    Shopware()->Models()->addAttribute(
        's_order_attributes', 
        'ordermod',
        'Random1',
        'DECIMAL(12,4)',
        false,
        0.0000);
    Shopware()->Models()->addAttribute(
        's_order_attributes',
        'ordermod',
        'Random2',
        'DECIMAL(12,4)',
        false,
        0.0000);

    $metaDataCacheDoctrine = Shopware()->Models()->getConfiguration()->getMetadataCacheImpl();
    $metaDataCacheDoctrine->deleteAll();

    Shopware()->Models()->generateAttributeModels(array('s_order_attributes'));
}

/engine/Shopware/Components/Model/ModelManager.php 中的 addAttribute() 函数具有以下签名:

/**
 * Shopware helper function to extend an attribute table.
 *
 * @param string $table Full table name. Example: "s_user_attributes"
 * @param string $prefix Column prefix. The prefix and column parameter will be the column name. Example: "swag".
 * @param string $column The column name
 * @param string $type Full type declaration. Example: "VARCHAR( 5 )" / "DECIMAL( 10, 2 )"
 * @param bool $nullable Allow null property
 * @param null $default Default value of the column
 * @throws \InvalidArgumentException
 */
public function addAttribute($table, $prefix, $column, $type, $nullable = true, $default = null);

希望这会有所帮助。

亲切的问候!