Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
php arch. made to orer module
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

So far so good, but it looks like the mysql4-install-0.1.0.php file isn’t getting used!

I was wondering if it was outdated (I’m assuming so).

app/etc/module/Fido_All.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Fido_Mto>
            <
active>true</active>
            <
codePool>local</codePool>
        </
Fido_Mto>
    </
modules>
</
config>

app\code\local\Fido\Mto\etc\config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Fido_Mto>
            <
version>0.1.0</version>
            <
depends></depends>
        </
Fido_Mto>
    </
modules>
    
    <global>
        <
models>
            <
mto>
                <class>
Fido_Mto_Model</class>
            </
mto>
        </
models>
        <
blocks>
            <
mto>
                <class>
Fido_Mto_Block</class>
            </
mto>
        </
blocks>
        <
resources>
            <
mto_setup>
                <use>
default_setup</use>
            </
mto_setup>
            <
mto_write>
                <use>
default_write</use>
            </
mto_write>
            <
mto_read>
                <use>
default_read</use>
            </
mto_read>
        </
resources>
    </global>
    
    <
adminhtml>
        <
menu></menu>
        <
acl></acl>
        <
events></events>
        <
translate></translate>
    </
adminhtml>
    
    <
frontend>
        <
routers></routers>
        <
events></events>
        <
translate></translate>
        <
layout></layout>
    </
frontend>
    
    <default>
        <
config_vars></config_vars>
    </default>
</
config>

app\code\local\Fido\Mto\sql\mto_setup\mysql4-install-0.1.0.php

<?php
echo 'installing';
$read Mage::getSingleton('core/session')
    ->
getConnection('core_read');

//retrieve $quote_type_id and $order_type_id from database
$eid $read->fetchRow('select entity_type_id from eav_entity_type where entity_type_code = "quote_item"');
$quote_type_id $eid['entity_type_id'];

$eid $read->fetchRow('select entity_type_id from eav_entity_type where entity_type_code = "order_item"');
$order_type_id $eid['entity_type_id'];

$installer $this;
$installer->startSetup();

//values needed for new eav_attribute record
$c = array(    'entity_type_id'    =>    $quote_type_id,
            
'attribute_code'    =>    'user_input',
            
'backend_type'        =>    'varchar',
            
'frontend_input'    =>    'text',
            
'is_global'            =>    '1',
            
'is_visible'        =>    '0',
            
'is_required'        =>    '0',
            
'is_user_defined'    =>    '1');
            
$v = array(    'entity_type_id'    =>    $order_type_id,
            
'attribute_code'    =>    'user_input',
            
'backend_type'        =>    'varchar',
            
'frontend_input'    =>    'text',
            
'is_global'            =>    '1',
            
'is_visible'        =>    '0',
            
'is_required'        =>    '0',
            
'is_user_defined'    =>    '1');
            
//create a new attribute object for order items and quote items
//repeat for each store ID you have
$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($c['entity_type_id'])
                    ->
setStoreId(0)
                    ->
addData($c);
$attribute->save();

$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($v['entity_type_id'])
                    ->
setStoreId(0)
                    ->
addData($v);
$attribute->save();

$installer->endSetup();

app\code\local\Fido\Mto\Block\Product\View.php

<?php

class Fido_Mto_Block_Product_View extends Mage_Catalog_Block_Product_View
{

    
protected function _prepareLayout()
    
{
    
        $inputBlock 
$this->getLayout()->addBlock('core/template''user_input')
                    ->
setTemplate('mto/user_input.phtml');
        
$this->setChild('user_input'$inputBlock);
        
        return 
parent::_prepareLayout();
    
    
}

}
[
/code[

app
/design/frontend/default/fido/layout/catalog.xml
[code]
<!-- change made to catalog.xml- ->
<
reference name="content">
           <!-- <
block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml"> -->
            <
block type="mto/product_view" name="product.info" template="catalog/product/view.phtml">

My phtml files are edited and created accordingly - they work fine..

I get no errors but I don’t see the items created in the mysql4-install file. It doesn’t look like it’s ever called. I can check the eav_attribute table (where I believe they should be stored) but they don’t appear to be there.

BTW, when I say “they” I am referring to the “user_input” attribute:

$c = array(    'entity_type_id'    =>    $quote_type_id,
            
'attribute_code'    =>    'user_input',
            
'backend_type'        =>    'varchar',
            
'frontend_input'    =>    'text',
            
'is_global'            =>    '1',
            
'is_visible'        =>    '0',
            
'is_required'        =>    '0',
            
'is_user_defined'    =>    '1');
            
$v = array(    'entity_type_id'    =>    $order_type_id,
            
'attribute_code'    =>    'user_input',
            
'backend_type'        =>    'varchar',
            
'frontend_input'    =>    'text',
            
'is_global'            =>    '1',
            
'is_visible'        =>    '0',
            
'is_required'        =>    '0',
            
'is_user_defined'    =>    '1');
 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

To clarify - What is the key to getting the install script triggered? Just the version? (Or is my code in that file totally wrong).

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

I’ve made progress with this road block - Did some investigating:

in my sql install file:

Change:

$read Mage::getSingleton('core/session')  //really dumb mistake on my part
    
->getConnection('core_read');

//and

$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($c['entity_type_id']//forgot a parameter
                    
->setStoreId(0)
                    ->
addData($c);
$attribute->save();

$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($v['entity_type_id']//forgot a parameter
                    
->setStoreId(0)
                    ->
addData($v);
to
$read Mage::getSingleton('core/resource')
    ->
getConnection('core_read');

$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($c['entity_type_id']$c['attribute_code'])
                    ->
setStoreId(0)
                    ->
addData($c);
$attribute->save();

$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($v['entity_type_id']$v['attribute_code'])
                    ->
setStoreId(0)
                    ->
addData($v);

Additionally, change config.xml <resource> area to this:

<resources>
            <
mto_setup>
                <
setup>
                    <
module>Fido_Mto</module>
                </
setup>
                <
connection>
                    <use>
core_setup</use>
                </
connection>
            </
mto_setup>
            <
mto_write>
                <
connection>
                    <use>
core_write</use>
                </
connection>
            </
mto_write>
            <
mto_read>
                <
connection>
                    <use>
core_read</use>
                </
connection>
            </
mto_read>
        </
resources>
 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

A better way of adding attributes is to use an EAV setup class.

config.xml

<config>
    <global>
        <
resources>
            <
mto_setup>
                <
setup>
                    <
moduleFido_Mto</module>
                    <class>
Fido_Mto_Model_Mysql4_Setup</class>
                </
setup>
                <
connection>
                    <use>
core_setup</use>
                </
connection>
            </
mto_setup>
        </
resources>
    </global>
</
config>

Fido_Mto_Model_Mysql4_Setup

class Fido_Mto_Model_Mysql4_Setup extends Mage_Sales_Model_Mysql4_Setup
{
    
protected $resourcePreviousState null;

    public function 
startSetup()
    
{
        $this
->resourcePreviousState Mage::registry('resource');
        
Mage::unregister('resource');
        
Mage::register('resource'true);

        return 
parent::startSetup();
    
}

    
public function endSetup()
    
{
        Mage
::unregister('resource');
        if(
$this->resourcePreviousState !== null)
        
{
            Mage
::register('resource'$this->resourcePreviousState);
        
}

        
return parent::endSetup();
    
}

    
public function getDefaultEntities()
    
{
        
return array(
            
'quote_item' => array(
                
'entity_model'  => 'sales/quote_item',
                
'table'         => 'sales/quote_item',
                
'attributes'    => array(
                    
'user_input' => array(
                        
'group'             => 'My Attribute Group Name (id looked up via name)',
                        
'type'              => 'varchar',
                        
'input'             => 'text',
                        
'label'             => 'User Input',
                        
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        
'visible'           => false,
                        
'required'          => false,
                        
'user_defined'      => true
                    
),
                ),
            ),
            
'order_item' => array(
                
'entity_model'  => 'sales/order_item',
                
'table'         => 'sales/order_entity',
                
'attributes'    => array(
                    
'user_input' => array(
                        
'group'             => 'My Attribute Group Name (id looked up via name)',
                        
'type'              => 'varchar',
                        
'input'             => 'text',
                        
'label'             => 'User Input',
                        
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        
'visible'           => false,
                        
'required'          => false,
                        
'user_defined'      => true
                    
),
                ),
            ),
        );
    
}
}

mysql4-install-0.1.0.php

/* add this */
$installer->installEntities();
/* right before this */
$installer->endSetup();

If you look at how Magento installs it’s own attributes, you will see this is what they do.

EDIT: Fixed the ‘extends’ statment to use the Mage_Sales_Model_Mysql4_Setup object due to the usage of EAV and Flat tables at the same time.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

If you want to see how Magento does this look here:


/magento/app/code/core/Mage/Sales/etc/config.xml
/magento/app/code/core/Mage/Sales/Model/Mysql4/Setup.php
/magento/app/code/core/Mage/Sales/sql/sales_setup/mysql4-install-0.7.0.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

One more thing, be careful with some things.  Looking closer at Mage_Sales I notice that it actually has some ‘flat’ tables.  To make my code work correctly, you would need to extend Mage_Sales_Model_Mysql4_Setup instead of Mage_Eav_Model_Entity_Setup.  The reason for this is that Mage_Sales_Model_Mysql4_Setup overrides some functions to implement ‘flat’ tables.

NOTE: by flat table I mean a non-EAV table with one column per attribute.  Varien added this for performance reasons (i think).

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

<3

Thanks so much for the info — I actually “fixed” my issues by looking at the Mage/Catalog setup (I’ll checkout Sales also!)

I’m assuming ‘getDefaultEntities()’ get’s called to return those arrays withing the installEntities() function?

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

ugh, so the Sales tables are all flat tables now rather than EAV tables. (I’m just catching up to the real implications behind the changes to the Sales table [making them flat tables])

Tables:
sales_flat_order_item
sales_flat_quote
sales_flat_quote_address
sales_flat_quote_address_item
sales_flat_quote_item
sales_flat_quote_item_option
sales_flat_quote_payment
sales_flat_quote_shipping_rate

These don’t have the usual “text”, “varchar” and so on tables (EAV structure) as Lee pointed out - which means my code to create a quote item attribute is creating an attribute in the eav_attribute table as it should, but the attribute’s value within a sales quote item has nowhere to go in the database (which sucks).

This presents an interesting problem as the quote item is based on a flat table while the order item is based on EAV, and so I can’t just extend Mage_Sales_Model_Mysql4_Setup as it looks like I need that class to manage the flat tables and Mage_Eav_Model_Entity_Setup to manage the EAV tables :(

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Actually, if you extend Mage_Sales_Model_Mysql4_Setup you will be fine (in this case) because it only applies the flat table structure to a few entities that it knows about.  Since Quotes and Orders live in the same module, it already knows which tables use EAV and which tables use the Flat Model.

So, If you use the code samples I provided and extend Mage_Sales_Model_Mysql4_Setup instead of Mage_Eav_Model_Entity_Setup you will be good.

I’m changing the samples above to use Mage_Sales_Model_Mysql4_Setup for you and anyone else who stumbles upon this code.  wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Mage_Sales_Model_Mysql4_Setup recognizes the following entities as being flat tables instead of EAV based tables

protected $_flatEntityTables = array(
        
'quote'             => 'sales_flat_quote',
        
'quote_item'        => 'sales_flat_quote_item',
        
'quote_address'     => 'sales_flat_quote_address',
        
'quote_address_item'=> 'sales_flat_quote_address_item',
        
'quote_address_rate'=> 'sales_flat_quote_shipping_rate',
        
'quote_payment'     => 'sales_flat_quote_payment',
        
'order_item'        => 'sales_flat_order_item',
    );

You are adding attributes to the ‘quote_item’ and ‘order_item’ entities.

So, in your case the two attributes you are adding would both be added to the flat table structure.  Basically it just adds a column onto each table for you.

The important thing to realize though is that by extending Mage_Sales_Model_Mysql4_Setup you do not care because that class knows to treat attributes for the listed entities differently than unlisted entities.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

Thank you again for your help with this! You’re contributions are invaluable.

I’ll report back on my success (and write it all up in a cohesive post for everyone) - hopefully soon.

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

Update:

The new install code appears to be working. The user_input for the quote item is being saved to the database but I’m not sure that the order_item attribute was created successfully.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Can you post the code from your Setup class?

 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

<?php
class Fido_Mto_Model_Mysql4_Setup extends Mage_Sales_Model_Mysql4_Setup
{
    
protected $resourcePreviousState NULL;

    public function 
startSetup()
    
{
        $this
->resourcePreviousState Mage::registry('resource');
        
Mage::unregister('resource');
        
Mage::register('resource'true);

        return 
parent::startSetup();
    
}

    
public function endSetup()
    
{
        Mage
::unregister('resource');
        if(
$this->resourcePreviousState !== null)
        
{
            Mage
::register('resource'$this->resourcePreviousState);
        
}

        
return parent::endSetup();
    
}

    
public function getDefaultEntities()
    
{
        
return array(
            
'quote_item' => array(
                
'entity_model'  => 'sales/quote_item',
                
'table'         => 'sales/quote_item',
                
'attributes'    => array(
                    
'user_input' => array(
                        
'group'             => 'General',
                        
'type'              => 'varchar',
                        
'input'             => 'text',
                        
'label'             => 'User Input',
                        
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        
'visible'           => false,
                        
'required'          => false,
                        
'user_defined'      => true
                    
),
                ),
            ),
            
'order_item' => array(
                
'entity_model'  => 'sales/order_item',
                
'table'         => 'sales/order_entity',
                
'attributes'    => array(
                    
'user_input' => array(
                        
'group'             => 'General',
                        
'type'              => 'varchar',
                        
'input'             => 'text',
                        
'label'             => 'User Input',
                        
'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
                        
'visible'           => false,
                        
'required'          => false,
                        
'user_defined'      => true
                    
),
                ),
            ),
        );
    
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
nikefido
Guru
 
Avatar
Total Posts:  481
Joined:  2008-07-11
New Haven, CT
 

<?php
$installer 
$this;
/* var $installer Fido_Mto_Model_Mysql4_Setup */
$installer->startSetup();

$installer->installEntities();

$installer->endSetup();
 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

EDIT: removed because I’m an idiot

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2