mm09.2 Magento Attribute Models

Vortrag für mm09.2 am 02.11.2009 in ffm. Einführung in die Magento Attribute Models »
Vinai Kopp

public function getOptionText($value)
{
    $optionText = parent::getOptionText($value);
    if (is_array($optionText) && isset($optionText['label']))
    {
        $optionText = $optionText['label'];
    }
    return $optionText;
}
$columnName = $this->getAttribute()->getAttributeCode();
array(
    $columnName = array(
        'type'      => 'int',
        'unsigned'  => false,
        'is_null'   => true,
        'default'   => null,
        'extra'     => null
    )
);
<?php

abstract class Mm09_SampleModule_Model_Entity_Attribute_Frontend_Sample
    extends Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
{
    public function getValue($object)
    {
        $value = parent::getValue($object)
        if (! isset($value))
        {
            $value = Mage::getStoreConfig('general/sample_module/default_value');
        }
        return $value;
    }
}
Attribute Models
mm09.2 in ffm
02.11.2009
von Vinai Kopp
Vinai Kopp
Freier Programmierer
vinai@netzarbeiter.de
+49 173 - 31 33 444
Support
Deutsches offizielles Magento Forum: http://www.magentocommerce.com/boards/viewforum/25/

IRC irc.freenode.net Channel #magento-de (deutsch) oder #magento (english)


Dokumentation?

t3n Magazin: regelmäßig Magento Artikel

Liste mit Magento-Development Blogs http://blog.velite.de/essential-magento-developer-blogs/

... Zeitlos und immer Richtig: use the Source...!

Bücher:
Online-Shops mit Magento (Roman Zenner)
Magento - Das Handbuch für Entwickler (Visions new Media Entwickler)



Kontakt
Fragen ?
eav_attribute
Source Models
Backend Models
Attribute Models
attribute_id | attribute_code | attribute_model | source_model | backend_model | frontend_model | backend_type | ...
Select/Multiselect:
Liefern der Optionen
Mage_Eav_Model_Entity_Attribute_Source_Abstract
public function  getAllOptions();
public function getOptionText($value);
public function getFlatColums();
public function getFlatIndexes();
public function getFlatUpdateSelect($store);
return array(
    array(
        'value' => '',
        'label' => Mage::helper('core')->__('--- Please Choose ---'),
    ),
    array(
        'value' => 'wert1',
        'label' => 'Option Eins'
    ),
    array(
        'value' => 'wert2',
        'label' => 'Option Zwei'
    )
);
Mage::getModel('catalog/product')->getCollection()->toOptionArray();
Optionen aus Collections
Optionen aus der Konfiguration
   <global>
        <modul>
            <fields>
                <red><label>Rotwein</label> </red>
                <white><label>Weißwein</label></white>
                <champagne><label>Sekt</label></champagne>
                <desert><label>Dessertwein</label></desert>
            </fields>
        </modul>
    </global>


if ($config = Mage::getConfig()->getNode('global/modul/fields')->asArray())
{
    if (is_array($config)) foreach ($config as $value => $data)
    {
        $this->_options[] = array(
            'value' => $value,
            'label' => $data['label'],
        );
    }
}
Optionen als Array
Der Flat Catalog
für Product und Category select/multiselect Attribute...
Default Source Models
Mage_Eav_Model_Entity_Attribute_Source_Boolean
Mage_Eav_Model_Entity_Attribute_Source_Store
Mage_Eav_Model_Entity_Attribute_Source_Table
Mage_Eav_Model_Entity_Attribute_Source_Config
Mage_Customer_Model_Customer_Attribute_Source_Group
usw...
Beispiel...
Werte via SOAP holen
Setup Script
Source Model
<?php

$this->startSetup();

$this->addAttribute('catalog_product', 'source_sample', array(
    'label' => 'xIgnite SOAP API',
    'type' => 'varchar',
    'input' => 'select',
    'source' => 'SampleModule/entity_attribute_source_xignite',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible' => 1,
    'visible_on_front' => 0,
    'required' => 0,
    'user_defined' => 1,
    'default' => ''
));

$this->endSetup();
<?php

class Mm09_SampleModule_Model_Entity_Attribute_Source_Xignite
    extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    public function getAllOptions()
    {
        if (! $this->_options)
        {
            $this->_options = array(array(
                    'value' => '',
                    'label' => Mage::helper('SampleModule')->__('--- Please Choose ---')
            ));
            $client = new SoapClient('http://www.xignite.com/xGlobalQuotes.asmx?WSDL');
            $result = $client->GetGlobalDelayedQuotes(array('Identifiers' => 'BMW.DE,MTV.L,RSL.L', 'IdentifierType' => 'Symbol'));
            foreach ($result->GetGlobalDelayedQuotesResult->GlobalQuote as $record)
            {
                $this->_options[] = array(
                    'value' => $record->Security->Symbol,
                    'label' => sprintf('%s (%s)', $record->Security->Name, $record->Security->Market)
                );
            }
        }
        return $this->_options;
    }
}
(ein unsinniges Beispiel...!)
Mage_Eav_Model_Entity_Attribute_Backend_Abstract
Datenbank
Load / Save Trigger
Beispiel - Validierung
Core Backend Models
Mage_Catalog_Model_Resource_Eav_Attribute
Mage_Eav_Model_Entity_Attribute_Abstract
Frontend Models
Name der Tabelle mit für den Wert des Attributs
EAV Attribute - backend_table der Entity . '_' . Data-Type
Für backend_type = 'static' backend_table der Entity
public function getTable();
public function getEntityIdField();
Feld zum Joinen der Value Tabelle mit der Entity Tabelle
Default: entity_id aus Mage_Eav_Model_Entity::DEFAULT_ENTITY_ID_FIELD
afterLoad();
beforeSave();
afterSave();
beforeDelete();
afterDelete();
validate();
public function
Mage_Eav_Model_Entity_Attribute_Backend_Array
Mage_Eav_Model_Entity_Attribute_Backend_Datetime
Mage_Eav_Model_Entity_Attribute_Backend_Increment
Mage_Eav_Model_Entity_Attribute_Backend_Store
Mage_Customer_Model_Customer_Attribute_Backend_Password
Mage_Catalog_Model_Product_Attribute_Backend_Media

... und viele mehr
Nachladen von Werten aus
weiteren Tabellen / Dateien
Default Werte setzen
Werte aktualisieren
Daten zum Speichern formatieren
Eigene Trigger anstoßen
... Caching
... externe Systeme
... Alternative zum afterSave() im Model
Integrity Checks - darf gelöscht werden?
Cleanup
... Files löschen
... Alternative zu model_delete_after Events
... wie der Name schon sagt
public function validate($object)
{
    if (parent::validate($object))
    {
        $attrCode = $this->getAttribute()->getAttributeCode();
        $data = $object->getData($attrCode);
        if ($data)
        {
            if (! is_numeric($data))
            {
                Mage::throwException(Mage::helper('SampleModule')->__('The amount must be a numeric value'));
            }
            if ($data < 0)
            {
                Mage::throwException(Mage::helper('SampleModule')->__('The amount must greater then zero'));
            }
        }
    }
    return true;
}
public function getFlatIndexes();
für non-Select/Multiselect Attribute
getBackend()
getSource()
getFrontend()
getEntity()
Interface zu allen weiteren Models
Flexibler default-Wert
public function getDefaultValue()
<?php

abstract class Mm09_SampleModule_Model_Entity_Attribute_Sample
    extends Mage_Catalog_Model_Resource_Eav_Attribute
{
    public function getDefaultValue()
    {
        $value = Mage::getStoreConfig('general/sample_module/default_value');
        return $value;
    }
}
public function getValue();
public function getValue(Varien_Object $object)
{
    $data = '';

    if ($value = parent::getValue($object))
    {
        $data = Mage::getSingleton('core/locale')->date($value, Zend_Date::ISO_8601, null, false)
            ->toString('YYYY-MM-dd HH:ii:ss');
    }

    return $data;
}
Formatieren vor der Ausgabe
Flexibler default-Wert im Frontend
Mage_Eav_Model_Entity_Attribute_Frontend_Abstract
public function getInputRendererClass();
für komplexe Backend Interfaces
Block Klasse, Varien_Data_Form_Element_Abstract
public function getElementHtml();
eav_entity_type
entity_type_id | entity_type_code | entity_model | entity_table | attribute_model | ...
Entity Models
Mage_Core_Model_Abstract
function _construct()
{
    $this->_init('customer/customer');
}

protected function _init($resourceModel)
{
    $this->_setResourceModel($resourceModel);
}
Entity Resource Models
Mage_Eav_Model_Entity_Abstract
public function getAttribute($attribute);
public function loadAllAttributes($object=null);
public function getFlatUpdateSelect($store);
Mage::getResourceModel('eav/entity_attribute')
            ->getFlatUpdateSelect($this->getAttribute(), $store);
Varien_Db_Select das den Wert des Attributes zurückliefert.
public function getFlatColumns();
$indexName = $this->getAttribute()->getAttributeCode();
array(
    $indexName => 'attribute_column_name',
    ...
)
Flat Catalog                  
public function
Bug in Mage_Eav_Model_Entity_Attribute_Source_Abstract::getOptionText()
Behoben in Magento 1.4
... Product und Category Attribute
unique ID
<config>
    <global>
        <customer>
            <models>
                <customer>
                    <class>Mage_Customer_Model</class>
                    <resourceModel>customer_entity</resourceModel>
                </customer>
                <customer_entity>
                    <class>Mage_Customer_Model_Entity</class>
                    <entities>
                        <entity>
                            <table>customer_entity</table>
                        </entity>
                        <address_entity>
                            <table>customer_address_entity</table>
                        </address_entity>
                        <value_prefix>
                            <table>customer_entity</table>
                        </value_prefix>
                        <customer_group>
                            <table>customer_group</table>
                        </customer_group>
                    </entities>
                </customer_entity>
            </models>
        </customer>
    </global>
</config>
customer/entity
entity_table
Mage_Customer_Model_Customer
Default ist eav/entity_attribute
mapped to Mage_Customer_Model_Entity_Customer
public function getReadConnection();
public function getWriteConnection();
public function saveAttribute(Varien_Object $object, $attributeCode);
public function beginTransaction();
public function commit();
public function rollBack();
unique ID
Default eav/entity_attribute
Default eav/entity_attribute_source_config
Default eav/entity_attribute_backend_default
Default eav/entity_attribute_frontend_default
FK eav_entity.entity_type_id

Loading comments...

Please log in to add your comment.

Report abuse

More presentations by Vinai Kopp

  • Magento SOAP API

    Vinai Kopp on

    An introduction to extending the Magento SOAP API

  • Realtime Soap API EN

    Vinai Kopp on

    Implementing a (near) realtime SOAP interface between an ERP system and Magento doesn't sound too difficult – Magento already features a SOAP API out of ...

  • mm09.2 Magento Programming Introduction

    Vinai Kopp on

    Vortrag für mm09.2 am 02.11.2009 in ffm. Einführung in die Magento Modulprogrammierung

More prezis by author