如何使用安装脚本为客户创建多选属性?


9

我正在关注许多文章,但无法创建多选择属性。而且我得到以下错误。

a:5:{i:0; s:50:“属性” exinent1“找不到源模型”“”; i:1; s:3008:“#0 C:\ wamp \ www \ magento8 \ app \ code \核心。

我的脚本是:

<?php
$installer = $this;
$installer->startSetup();

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute('customer', 'exinent1', array(
    'input'         => 'multiselect', 
    'type'          => 'varchar', 
    'backend'       => 'eav/entity_attribute_backend_array',
    'label'         => 'exinent1',
    'visible'       => 1,
    'required'      => 0,
    'user_defined'  => 1,
    'option'        => array (
                'value' => array('optionone' => array('First Option'),
                         'optiontwo' => array('Second Option'),
                         'optionthree' => array('Third Option'),
                         )
                        ),

));

$setup->addAttributeToGroup(
 $entityTypeId,
 $attributeSetId,
 $attributeGroupId,
 'exinent1',
 '100'
);

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'exinent1');
$oAttribute->setData('used_in_forms', array('adminhtml_customer')); 
$oAttribute->save();

$setup->endSetup();

和config.xml是:

<?xml version="1.0"?>
<config>
    <modules>
        <Exinent_Cusatt>
            <version>3.1.5.6</version>
        </Exinent_Cusatt>
    </modules>
    <global>
    <resources>
    <exinent_cusatt_setup>
        <setup>
            <module>Exinent_Cusatt</module>
        </setup>
    </exinent_cusatt_setup>
</resources>
    </global>
</config>

您必须设置EAV资源模型...
Elavarasan 2014年

你可以参考任何文章吗?
sivakumar 2014年

Answers:


10

我认为您犯了很多错误..

等/ config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Exinent_Cusatt>
      <version>0.1.0</version>
    </Exinent_Cusatt>
  </modules>
  <global>
    <helpers>
      <cusatt>
        <class>Exinent_Cusatt_Helper</class>
      </cusatt>
    </helpers>
    <models>
      <cusatt>
        <class>Exinent_Cusatt_Model</class>
        <resourceModel>cusatt_mysql4</resourceModel>
      </cusatt>
    </models>
    <resources>
      <customerattribute1404212233_setup>
        <setup>
          <module>Exinent_Cusatt</module>
          <class>Mage_Customer_Model_Entity_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </customerattribute1404212233_setup>
      <customerattribute1404212233_write>
        <connection>
          <use>core_write</use>
        </connection>
      </customerattribute1404212233_write>
      <customerattribute1404212233_read>
        <connection>
          <use>core_read</use>
        </connection>
      </customerattribute1404212233_read>
    </resources>
  </global>
</config> 

Helper / Data.php:

<?php
class Exinent_Cusatt_Helper_Data extends Mage_Core_Helper_Abstract
{
}

模式/ Eav /实体/属性/源/客户选项14042122330.php

<?php
class Exinent_Cusatt_Model_Eav_Entity_Attribute_Source_Customeroptions14042122330 extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    /**
     * Retrieve all options array
     *
     * @return array
     */
    public function getAllOptions()
    {
        if (is_null($this->_options)) {
            $this->_options = array(

                array(
                    "label" => Mage::helper("eav")->__("Myoption-1"),
                    "value" =>  1
                ),

                array(
                    "label" => Mage::helper("eav")->__("Myoption-2"),
                    "value" =>  2
                ),

                array(
                    "label" => Mage::helper("eav")->__("Myoption-3"),
                    "value" =>  3
                ),

                array(
                    "label" => Mage::helper("eav")->__("Myoption-4"),
                    "value" =>  4
                ),

            );
        }
        return $this->_options;
    }

    /**
     * Retrieve option array
     *
     * @return array
     */
    public function getOptionArray()
    {
        $_options = array();
        foreach ($this->getAllOptions() as $option) {
            $_options[$option["value"]] = $option["label"];
        }
        return $_options;
    }

    /**
     * Get a text for option value
     *
     * @param string|integer $value
     * @return string
     */
    public function getOptionText($value)
    {
        $options = $this->getAllOptions();
        foreach ($options as $option) {
            if ($option["value"] == $value) {
                return $option["label"];
            }
        }
        return false;
    }

    /**
     * Retrieve Column(s) for Flat
     *
     * @return array
     */
    public function getFlatColums()
    {
        $columns = array();
        $columns[$this->getAttribute()->getAttributeCode()] = array(
            "type"      => "tinyint(1)",
            "unsigned"  => false,
            "is_null"   => true,
            "default"   => null,
            "extra"     => null
        );

        return $columns;
    }

    /**
     * Retrieve Indexes(s) for Flat
     *
     * @return array
     */
    public function getFlatIndexes()
    {
        $indexes = array();

        $index = "IDX_" . strtoupper($this->getAttribute()->getAttributeCode());
        $indexes[$index] = array(
            "type"      => "index",
            "fields"    => array($this->getAttribute()->getAttributeCode())
        );

        return $indexes;
    }

    /**
     * Retrieve Select For Flat Attribute update
     *
     * @param int $store
     * @return Varien_Db_Select|null
     */
    public function getFlatUpdateSelect($store)
    {
        return Mage::getResourceModel("eav/entity_attribute")
            ->getFlatUpdateSelect($this->getAttribute(), $store);
    }
}

sql / customerattribute1404212233_setup / mysql4-install-0.1.0.php

<?php
$installer = $this;
$installer->startSetup();


$installer->addAttribute("customer", "exinent",  array(
    "type"     => "text",
    "backend"  => "",
    "label"    => "exinent1",
    "input"    => "multiselect",
    "source"   => "cusatt/eav_entity_attribute_source_customeroptions14042122330",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => ""

    ));

        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "exinent");


$used_in_forms=array();

$used_in_forms[]="adminhtml_customer";
        $attribute->setData("used_in_forms", $used_in_forms)
        ->setData("is_used_for_customer_segment", true)
        ->setData("is_system", 0)
        ->setData("is_user_defined", 1)
        ->setData("is_visible", 1)
        ->setData("sort_order", 100)
        ;
        $attribute->save();



$installer->endSetup();

就是这样。只需启用您的模块声明您的代码池,并且不要与数字混淆。. <customerattribute1404212233_setup> 对于您的属性标识,它只是随机的no,以防止重复输入。..干杯..!


实际上,我的multiselect属性是货运条款。并且我正在创建一个包含两列实体的模块(货运条款,启用/禁用)。因此我需要将此实体映射到已创建的货运条款属性选项。请为此提供一些指导。
sivakumar 2014年

1

嗨,Elavarasan和Sivakumar,

在静态中调用选项代码不是一个好主意。您只需要使用Eav表进行提取

$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('customer','exinent1');
 $collection =Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setPositionOrder('asc')
                ->setAttributeFilter($attributeId)
                ->setStoreFilter(0)
                ->load();

Surcemodel getAllOptions()应该是

 public function getAllOptions()
    {

 if (is_null($this->_options)) {

        attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('customer','attribute_name');
 $collection =Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setPositionOrder('asc')
                ->setAttributeFilter($attributeId)
                ->setStoreFilter(0)
                ->load();
        $this->_options = $collection->toOptionArray()

    }
    }

实际上,我的multiselect属性是运输条款。并且我正在创建一个模块(使用最终模块创建器),该模块包含具有两列的实体(运输条款,enable.disable)。因此我需要将此实体映射到已创建的运输条款属性选项。请为此提供一些指导。
sivakumar 2014年

我告诉你。需要一些更改Elavaras的答案。只需更改源模型代码中的代码
Amit Bera

好的,谢谢。例如,获取所有选项都是动态的(从装运条款表中获取选项)。在安装脚本后,我禁用了一些记录(装运条款)。因此它将反映在客户帐户信息中吗?
sivakumar 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.