【发布时间】:2016-04-04 11:03:55
【问题描述】:
我需要扩展 Shopware 变体模型以添加一些自定义属性,例如金属类型、宝石类型、宝石类型,这是基础文章。 这些属性将同时用于后端和前端。
我该怎么做?谢谢
【问题讨论】:
我需要扩展 Shopware 变体模型以添加一些自定义属性,例如金属类型、宝石类型、宝石类型,这是基础文章。 这些属性将同时用于后端和前端。
我该怎么做?谢谢
【问题讨论】:
根本不可能扩展 Shopware 核心模型本身。根据您尝试扩展的特定模型,有两种不同的解决方法:
如果它是您想要扩展的文章本身,您可以使用此处描述的自定义属性字段:http://community.shopware.com/Anlegen,-Anpassen-und-Ausgabe-von-Artikel-Attributen_detail_1208.html
另一种方法是编写一个插件,通过插件 install() 上的代码创建属性字段。这仅适用于具有属于实体本身的属性表的实体。例如 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);
希望这会有所帮助。
亲切的问候!
【讨论】: