Domanda

I got through this tutorial regarding Magento Setup Resources.

At some point I saw that it supports more database backends, but in the mentioned tutorial is described only the way you can create tables.

I would need some resources/examples regarding methods for inserting (agnostic script) data into previous created tables.

È stato utile?

Soluzione

Like all things Magento (and open source), your best bet is to look at how other developers, specifically the Magento core developers, do the same thing. Here's a few examples

#File: app/code/core/Mage/Tax/sql/tax_setup/mysql4-upgrade-0.7.2-0.7.3.php
$installer = $this;
/* @var $installer Mage_Core_Model_Resource_Setup */

$installer->startSetup();

if (!$installer->getConnection()->fetchOne("select * from {$this->getTable('tax_class')} where `class_name`='Shipping' and `class_type`='PRODUCT'")) {
    $installer->run("
        insert  into {$this->getTable('tax_class')} (`class_name`,`class_type`) values ('Shipping','PRODUCT');
    ");
}

$installer->endSetup();

...

#File: app/code/core/Mage/Sales/sql/sales_setup/mysql4-upgrade-0.8.29-0.9.0.php
$installer->getConnection()->insert($installer->getTable('sales_flat_quote'), $quoteData);

You're basically running raw SQL against the database, so it'll be on you (i.e. the developer's responsibility) to keep it generic enough to run in the various SQL (MySQL, SQL Server, Oracle, etc.) environments.

I wouldn't recommend using standard Magento models in the setup resources. The context Magento runs those migration scripts in doesn't match the context that models normally run in. You may be able to get away with it, but using a normal Magento model in your setup resource script may break if the model makes assumptions about a fully bootstrapped Magento system.

You can find more examples yourself with a little searching. If you're command line savvy try this

$ cd app/code/core/Mage
$ find . -type f -wholename '*/sql/*'  | xargs grep -i -r insert 
$ find . -type f -wholename '*/sql/*'  | xargs ack -i insert       #if you use ack

Altri suggerimenti

You should use corresponding model to insert data. Example from app/code/core/Mage/Dataflow/data/dataflow_setup/data-install-1.6.0.0.php:

$dataflowData = array(
    array(
        'name'         => 'Export All Products',
        'actions_xml'  => '<action type="catalog/convert_adapter_product" method="load">\r\n    <var name="store"><![CDATA[0]]></var>\r\n</action>\r\n\r\n<action type="catalog/convert_parser_product" method="unparse">\r\n    <var name="store"><![CDATA[0]]></var>\r\n</action>\r\n\r\n<action type="dataflow/convert_mapper_column" method="map">\r\n</action>\r\n\r\n<action type="dataflow/convert_parser_csv" method="unparse">\r\n    <var name="delimiter"><![CDATA[,]]></var>\r\n    <var name="enclose"><![CDATA["]]></var>\r\n    <var name="fieldnames">true</var>\r\n</action>\r\n\r\n<action type="dataflow/convert_adapter_io" method="save">\r\n    <var name="type">file</var>\r\n    <var name="path">var/export</var>\r\n    <var name="filename"><![CDATA[export_all_products.csv]]></var>\r\n</action>\r\n\r\n',
        'gui_data'     => 'a:5:{s:4:"file";a:7:{s:4:"type";s:4:"file";s:8:"filename";s:23:"export_all_products.csv";s:4:"path";s:10:"var/export";s:4:"host";s:0:"";s:4:"user";s:0:"";s:8:"password";s:0:"";s:7:"passive";s:0:"";}s:5:"parse";a:5:{s:4:"type";s:3:"csv";s:12:"single_sheet";s:0:"";s:9:"delimiter";s:1:",";s:7:"enclose";s:1:""";s:10:"fieldnames";s:4:"true";}s:3:"map";a:3:{s:14:"only_specified";s:0:"";s:7:"product";a:2:{s:2:"db";a:0:{}s:4:"file";a:0:{}}s:8:"customer";a:2:{s:2:"db";a:0:{}s:4:"file";a:0:{}}}s:7:"product";a:1:{s:6:"filter";a:8:{s:4:"name";s:0:"";s:3:"sku";s:0:"";s:4:"type";s:1:"0";s:13:"attribute_set";s:0:"";s:5:"price";a:2:{s:4:"from";s:0:"";s:2:"to";s:0:"";}s:3:"qty";a:2:{s:4:"from";s:0:"";s:2:"to";s:0:"";}s:10:"visibility";s:1:"0";s:6:"status";s:1:"0";}}s:8:"customer";a:1:{s:6:"filter";a:10:{s:9:"firstname";s:0:"";s:8:"lastname";s:0:"";s:5:"email";s:0:"";s:5:"group";s:1:"0";s:10:"adressType";s:15:"default_billing";s:9:"telephone";s:0:"";s:8:"postcode";s:0:"";s:7:"country";s:0:"";s:6:"region";s:0:"";s:10:"created_at";a:2:{s:4:"from";s:0:"";s:2:"to";s:0:"";}}}}',
        'direction'    => 'export',
        'entity_type'  => 'product',
        'store_id'     => 0,
        'data_transfer'=> 'file'
    ),
    // ... 
);

foreach ($dataflowData as $bind) {
    Mage::getModel('dataflow/profile')->setData($bind)->save();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top