Pregunta

Estoy intentando añadir un atributo EAV llamado "vendorping" a la / el modelo de ventas order_shipment. Para lograr esto, he creado el siguiente archivo en mi módulo:

// app\code\local\Jb\Vendorping\sql\vendorping_setup\mysql4-install-0.1.0.php

$this->startSetup();

$sql = 'SELECT entity_type_id FROM `'.$this->getTable('eav_entity_type').'` WHERE entity_type_code = \'shipment\'';
$row = Mage::getSingleton('core/resource')
    ->getConnection('core_read')
    ->fetchRow($sql);
$entityTypeId = $row['entity_type_id'];

$c = array(
    'entity_type_id'  => $entityTypeId,
    'attribute_code'  => 'vendorping',
    'backend_type'    => 'int',
    'frontend_input'  => 'text',
    'is_global'       => '1',
    'is_visible'      => '0',
    'is_required'     => '0',
    'is_user_defined' => '0',
    'frontend_label'  => 'Vendor Confirmed',
    );
$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($c['entity_type_id'], $c['attribute_code'])
    ->setStoreId(0)
    ->addData($c)
    ->save();

$this->endSetup();

Ahora bien, esto está funcionando bien - se agrega correctamente este atributo:

mysql> mysql> SELECT * FROM eav_attribute WHERE attribute_code LIKE 'vendorping';
+--------------+----------------+----------------+-----------------+---------------+--------------+---------------+----------------+----------------+------------------+----------------+--------------+-------------+-----------------+---------------+-----------+------+
| attribute_id | entity_type_id | attribute_code | attribute_model | backend_model | backend_type | backend_table | frontend_model | frontend_input | frontend_label   | frontend_class | source_model | is_required | is_user_defined | default_value | is_unique | note |
+--------------+----------------+----------------+-----------------+---------------+--------------+---------------+----------------+----------------+------------------+----------------+--------------+-------------+-----------------+---------------+-----------+------+
|          127 |              8 | vendorping     | NULL            | NULL          | int          | NULL          | NULL           | text           | Vendor Confirmed | NULL           | NULL         |           0 |               0 | NULL          |         0 |      |
+--------------+----------------+----------------+-----------------+---------------+--------------+---------------+----------------+----------------+------------------+----------------+--------------+-------------+-----------------+---------------+-----------+------+
1 row in set (0.00 sec)

Pero si me quedo esta acción del controlador, me parece que no puede guardar con éxito el nuevo atributo:

// app\code\local\Jb\Vendorping\controllers\IndexController.php ===

class Jb_Vendorping_IndexController extends Mage_Core_Controller_Front_Action 
{
    public function pingAction()
    {
        // Get shipment
        $shipmentId = 1; // Set manually for now
        $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);
        var_dump($shipment->getOrder()->getShippingDescription());
            // Outputs:
            // string(17) "Flat Rate - Fixed" [So the shipment exists]

        // Save "vendorping" field and save
        $shipment->setVendorping(1);
        $shipment->save();

        // Reload shipment from database
        $shipment = Mage::getModel('sales/order_shipment')->load($shipmentId);

        // Check "vendorping" field
        var_dump($shipment->getVendorping());
            // Outputs:
            // NULL [Why??]
    }
}
¿Fue útil?

Solución 2

Esto funcionó:

// ModuleNamespace/ModuleName/sql/vendorping_setup/mysql4-install-0.1.0.php

$this->startSetup();

if (version_compare(Mage::getVersion(), '1.4.1.0', '>=')) { // If sales data is stored flat
    $w = $this->_conn;
    $w->addColumn($this->getTable('sales_flat_shipment'), 'vendorping', 'int');
} else {
    $eav = new Mage_Eav_Model_Entity_Setup('sales_setup');
    $eav->addAttribute('shipment', 'vendorping', array('type' => 'int'));
}

$this->endSetup();

Otros consejos

La adición de una entidad a un modelo EAV necesita algo más que añadir una fila a la tabla eav_entity_type. Recursos de instalación de EAV (las clases que se ejecutan las secuencias de comandos del instalador) tienen un método installEntities que se encarga de esto para usted. Lo mejor es tratar todo el asunto como un cuadro negro a menos que realmente desea trazar todo que está pasando. añadiendo al azar de datos y tablas de todo el sistema de EAV hasta que algo funciona casi siempre conduce a un sistema que se ha roto de alguna manera sutil. Es similar a tocar el violín directamente con los valores de memoria en la memoria RAM.

Mi artículo sobre modelos EAV debe cubrir lo que necesita saber. Si sigue teniendo problemas después de eso, volver con preguntas específicas.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top