Magento Module DB Setup Script not running - magento

Running Magento 1.9.0.2, I'm trying to add 2 new checkbox fields into the Address form via Module.
Problem: The database script is NOT executed (note the die() function on the first line)
Symptoms:
My module does not exist in core_resources table
database does not get updated
module is listed & enabled in System > Configuration > Advanced
log enabled, folder {base_dir}/var/log/ is 777, no log produced
I'm installing the module; is uploading the files to the folder & refresh the cache & frontend once triggers the install?
Question: What did I miss?
Here is the {base_dir}/app/code/local/Tdg/Check/etc/config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Tdg_Check>
<version>1.0.1</version>
</Tdg_Check>
</modules>
<admin>
<fieldsets>
<customer_dataflow>
<chk_commercial><billing>1</billing><shipping>1</shipping></chk_commercial>
<chk_residential><billing>1</billing><shipping>1</shipping></chk_residential>
</customer_dataflow>
</fieldsets>
</admin>
<global>
<models>
<check>
<class>Tdg_Check_Model</class>
</check>
</models>
<resources>
<check_setup>
<setup>
<module>Tdg_Check</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</check_setup>
<check_write>
<connection>
<use>core_write</use>
</connection>
</check_write>
<check_read>
<connection>
<use>core_read</use>
</connection>
</check_read>
</resources>
<fieldsets>
<sales_copy_order_billing_address>
<chk_commercial><to_order>*</to_order></chk_commercial>
<chk_residential><to_order>*</to_order></chk_residential>
</sales_copy_order_billing_address>
<sales_copy_order_shipping_address>
<chk_commercial><to_order>*</to_order></chk_commercial>
<chk_residential><to_order>*</to_order></chk_residential>
</sales_copy_order_shipping_address>
<sales_convert_quote_address>
<chk_commercial><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></chk_commercial>
<chk_residential><to_order_address>*</to_order_address><to_customer_address>*</to_customer_address></chk_residential>
</sales_convert_quote_address>
<sales_convert_order_address>
<chk_commercial><to_quote_address>*</to_quote_address></chk_commercial>
<chk_residential><to_quote_address>*</to_quote_address></chk_residential>
</sales_convert_order_address>
<customer_address>
<chk_commercial><to_quote_address>*</to_quote_address></chk_commercial>
<chk_residential><to_quote_address>*</to_quote_address></chk_residential>
</customer_address>
<checkout_onepage_billing>
<chk_commercial><to_customer>*</to_customer></chk_commercial>
<chk_residential><to_customer>*</to_customer></chk_residential>
</checkout_onepage_billing>
</fieldsets>
</global>
</config>
And the SQL Update file {base_dir}/app/code/Tdg/Check/sql/check_setup/mysql4-install-1.0.1.php:
<?php
die('Installing Module');
/* #var $installer Mage_Customer_Model_Entity_Setup */
$installer = $this;
$installer->startSetup();
/* #var $addressHelper Mage_Customer_Helper_Address */
$addressHelper = Mage::helper('customer/address');
$store = Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID);
/* #var $eavConfig Mage_Eav_Model_Config */
$eavConfig = Mage::getSingleton('eav/config');
// update customer address user defined attributes data
$attributes = array(
'chk_commercial' => array(
'label' => 'Chk Commercial',
'type' => 'int',
'input' => 'checkbox',
'default' => 0,
'is_user_defined' => 1,
'is_system' => 0,
'is_visible' => 1,
'sort_order' => 140,
'is_required' => 1,
'multiline_count' => 0,
'validate_rules' => array(
'max_text_length' => 1,
'min_text_length' => 1
),
),
'chk_residential' => array(
'label' => 'Chk Residential',
'type' => 'int',
'input' => 'checkbox',
'default' => 0,
'is_user_defined' => 1,
'is_system' => 0,
'is_visible' => 1,
'sort_order' => 141,
'is_required' => 1,
'multiline_count' => 0,
'validate_rules' => array(
'max_text_length' => 1,
'min_text_length' => 1
),
),
);
foreach ($attributes as $attributeCode => $data) {
$attribute = $eavConfig->getAttribute('customer_address', $attributeCode);
$attribute->setWebsite($store->getWebsite());
$attribute->addData($data);
$usedInForms = array(
'adminhtml_customer_address',
'customer_address_edit',
'customer_register_address'
);
$attribute->setData('used_in_forms', $usedInForms);
$attribute->save();
}
$installer->run("
ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `chk_commercial` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `chk_commercial` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_quote_address')} ADD COLUMN `chk_residential` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
ALTER TABLE {$this->getTable('sales_flat_order_address')} ADD COLUMN `chk_residential` VARCHAR(1) CHARACTER SET utf8 DEFAULT NULL AFTER `fax`;
");
$installer->endSetup();
?>
Last, it's the Module definition file Tdg_Check.xml:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Tdg_Check>
<active>true</active>
<codePool>local</codePool>
</Tdg_Check>
</modules>
</config>

In Magento module entry stored in code field like below:
yourmodulename_setup
Check for record like check_setup in code field of core_resource table.
So for run install script again you have to find module record in module and remove that record.
It will automatically run the script again.

Related

magento adding custom column to eav_attribute table

I want adding my custom field(column in database) to table eav_attribute
this is my configuration models and resource in t
<models>
<mycompany_filters>
<class>Mycompany_Filters_Model</class>
<resourceModel>mycompany_filters_resource</resourceModel>
</mycompany_filters>
<mycompany_filters_resource>
<class>Mycompany_Filters_Model_Resource</class>
</mycompany_filters_resource>
</models>
<resources>
<mycompany_filters_setup>
<module>Mage_Eav</module>
<module>Mage_Eav_Model_Entity_Setup</module>
</mycompany_filters_setup>
</resources>
directory tree
sql
-- mycompany_filters_setup
-- install-0.1.4.php
and content file install-0.1.4.php
<?php
$installer = $this;
/* #var $installer Mage_Eav_Model_Entity_Setup */
$installer->startSetup();
$installer->getConnection()->addColumn(
$installer->getTable('eav/attribute'),
"is_used_in_category",
"TINYINT( 1 ) UNSIGNED NOT NULL DEFAULT '0'"
);
$installer->endSetup();
but this doesn't work. In table core_resource not create string resource for mycompany_filters and in table eav_attribute don't created new column.
Error logs: system.log and exception.log apache2 log is empty
Main question why is doesn't work?
I would like to point out a few things I think is wrong here.
First your config.xml:
<models>
<mycompany_filters>
<class>Mycompany_Filters_Model</class>
<resourceModel>mycompany_filters_resource</resourceModel>
</mycompany_filters>
<mycompany_filters_resource>
<class>Mycompany_Filters_Model_Resource</class>
</mycompany_filters_resource>
</models>
<resources>
<mycompany_filters_setup>
<module>Mage_Eav</module>
<module>Mage_Eav_Model_Entity_Setup</module>
</mycompany_filters_setup>
</resources>
Should really be:
<models>
<mycompany_filters>
<class>Mycompany_Filters_Model</class>
<resourceModel>mycompany_filters_resource</resourceModel>
</mycompany_filters>
<mycompany_filters_resource>
<class>Mycompany_Filters_Model_Resource</class>
</mycompany_filters_resource>
</models>
<resources>
<mycompany_filters_setup>
<setup>
<module>Mage_Eav</module>
<class>Mage_Eav_Model_Entity_Setup</class>
</setup>
<!-- You could add these as well
<connection>
<use>core_setup</use>
</connection>
-->
</mycompany_filters_setup>
<!-- If you add the connection tag above, you should add these as well
<filters_write>
<connection>
<use>core_write</use>
</connection>
</filters_write>
<filters_read>
<connection>
<use>core_read</use>
</connection>
</filters_read>
-->
</resources>
Essentially, you are missing the <setup> tag that should wrap the resources <module> and <class> tags.
The contents of install-0.1.4.php should instead be:
$installer = $this;
$installer->startSetup();
$table = $installer->getTable('eav/attribute');
$installer->getConnection()
->addColumn($table, 'is_used_in_category', array(
'type' => 'could be Varien_Db_Ddl_Table::TYPE_TEXT or any you want it should be',
'nullable' => true, //could be true or false
'default' => null, //default value for this attribute
'unique' => true, //could be true or false
'comment' => 'Your comment here. Not important'
));
$installer->endSetup();
In your config.xml, make sure the version declared is same as 0.1.4. Clear your CACHE after these changes and run your application.
Hope it helps.

Magento - Add field to actions in Catalog Price Rules

I want to add a custom field to the actions tab in the Catalog Price Rules section.
This is what I did:
I added these lines to the file app\code\core\Mage\Adminhtml\Block\Promo\Catalog\Edit\Tab\Actions.php
$fieldset->addField('custom_field', 'select', array(
'label' => 'Custom Field',
'title' => 'Custom Field',
'name' => 'custom_field',
'options' => array(
'1' => Mage::helper('catalogrule')->__('Yes'),
'0' => Mage::helper('catalogrule')->__('No'),
),
));
I changed the version to 1.6.0.4 in this file: app\code\core\Mage\CatalogRule\etc\config.xml
<Mage_CatalogRule>
<version>1.6.0.4</version>
</Mage_CatalogRule>
I created new file with the name app\code\core\Mage\CatalogRule\sql\catalogrule_setup\upgrade-1.6.0.3-1.6.0.4.php
$installer->startSetup();
$ruleProductTable = $installer->getTable('catalogrule/rule');
$columnOptions = array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_SMALLINT,
'UNSIGNED' => true,
'NULLABLE' => false,
'DEFAULT' => 0,
'COMMENT' => 'Custom Field',
);
$installer->getConnection()->addColumn($ruleProductTable, 'custom_field', $columnOptions);
$installer->endSetup();
Then, I logged in to the admin panel and tried to edit one of the catalog price rules.
I saw my new field in the actions tab - great!
But when I click "Save", the changes that I made in that field, was not saved.
I logged in to the phpmyadmin and went to catalogrule table.
In this table I can see the new field custom_field, with the value 0 (default) - so It really didn't save the changed.
My question is what I did wrong?
Why the changed in the custom_field didn't saved in the DB?
Thanks
I'm not sure why it isn't working for you my guess is your installer isn't being called. I managed the modification through a module and local edits to future proof the change.
Directory structure:
/app/code/local/custom/promo
/app/code/local/custom/promo/etc/
/app/code/local/custom/promo/sql
/app/code/local/custom/promo/sql/promo_setup
/app/code/core/local/Adminhtml/Block/Promo/Catalog/Edit/Tab
add file /app/code/local/custom/promo/etc/config.xml
<?xml version="1.0"?>
<config>
<modules>
<Custom_Promo>
<version>1.0</version>
</Custom_Promo>
</modules>
<global>
<resources>
<promo_setup>
<setup>
<module>Custom_Promo</module>
<class>Mage_Catalog_Model_Resource_Setup</class>
</setup>
</promo_setup>
</resources>
</global>
</config>
add file /app/code/local/custom/promo/sql/promo_setup/install-1-0.php
/* #var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->startSetup();
$ruleProductTable = $installer->getTable('catalogrule/rule');
$columnOptions = array(
'TYPE' => Varien_Db_Ddl_Table::TYPE_BOOLEAN,
'NULLABLE' => false,
'COMMENT' => 'Custom Promo',
);
$installer->getConnection()->addColumn($ruleProductTable, 'custom_promo', $columnOptions);
$installer->endSetup();
Clear caches and verify the column is added by looking at the catalog_rule table
SELECT * FROM catalogrule;
Finally take a copy of /app/code/core/Mage/Adminhtml/Block/Promo/Catalog/Edit/Tab/Main.php and create the same file /app/code/core/local/Adminhtml/Block/Promo/Catalog/Edit/Tab/Main.php
Edit the file and add the field before $form->setValues($model->getData());
//Custom Promo
$fieldset->addField('custom_promo', 'select', array(
'label' => 'Custom Promo',
'title' => 'Custom Promo',
'name' => 'custom_promo',
'options' => array(
'1' => Mage::helper('catalogrule')->__('Yes'),
'0' => Mage::helper('catalogrule')->__('No'),
),
));

How to display Custom order attribute in Magento back end

I'm using Magento 1.7 - I am fairly new to Magento module development.
I have created a new Order custom EAV attribute called Deliverydate which the customer will enter on the Shopping Cart page. I created a custom module and installer script, and can see it in the eav_attribute table.
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('order', 'deliverydate', array(
'position' => 1,
'input' => 'text',
'backend_type' => 'varchar',
'type' => 'varchar',
'label' => 'Choose delivery date',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'global' => 1,
'visible_on_front' => 1,
));
$installer->endSetup();
But I've read many sources and can't figure out how to
1) Save the value entered by the customer.
2) Retrieve the value for display in the Admin.
I have read on these tutorials about Observers but can't seem to get one to work:
Here's my config.xml file
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Deliverydate>
<version>0.1.0</version>
</Mycompany_Deliverydate>
</modules>
<global>
<resources>
<deliverydate_setup>
<setup>
<module>Mycompany_Deliverydate</module>
<class>Mycompany_Deliverydate_Model_Resource_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</deliverydate_setup>
</resources>
<events>
<checkout_type_onepage_save_order>
<observers>
<Deliverydate_observer>
<type>singleton</type>
<class>deliverydate/observer</class>
<method>Deliverydate</method>
</Deliverydate_observer>
</observers>
</checkout_type_onepage_save_order>
</events>
</global>
</config>
And my Observer.php file, most of which I borrowed from another tutorial / question on this site.
class Mycompany_Deliverydate_Model_Observer {
public function Deliverydate($observer) {
$event = $observer->getEvent();
$order = $event->getOrder();
$order->setDeliverydate(Mage::app()->getRequest()->getPost('delivery_date'));
}
}
I don't think this is being entered at all - I put in a throw new Exception and went through an entire order without ever seeing the exception. In any case I am not sure I'm checking the right event - checkout_type_onepage_save_order - when is this fired? And I don't know where the setDeliverydate() function would be defined but as I said this sort of comes from another source and it wasn't defined there either.
And where does the value entered get stored?
Please check this following post, It would help you. If it doesn't uplift you from this issue, please comment here
http://chillydraji.wordpress.com/2014/03/03/how-to-create-new-attribute-to-order-in-magento/

Magento: can't set the default value of custom order attribute using installation script

I've added custom attribute to orders using mysql4-install-1.0.0.php in my module:
$installer = $this;
$installer->startSetup();
$installer->addAttribute('order', 'custom_status', array(
'type' => 'varchar',
'label' => 'My Status',
'note' => '',
'default' => "my_default_value",
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
'unique' => false
));
It works - when I look at sales_flat_order table, I see new varchar field *custom_status* in the table. But the default value is NULL instead of "my_default_value" as expected there. Any ideas, why?
PS. Installation script is really executed, I reset all to initial state each time.
UPD. config.xml
<resources>
<mymodule_setup>
<setup>
<module>Company_Mymodule</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</mymodule_setup>
***
Magento Sales module has pseudo emulation of old EAV functionality. It takes only type property from your array to create a column in the database. Also it uses "grid" property for determining is it required to make the same column in grid representation table.
BTW, previous sales module that was based on EAV, was not using default, label, required and other properties as well. If you want to set a default value for this attribute you need create an observer for order before save event and set this data in it if field is empty.
Make sure you have it like this in your config.xml:
<resources>
<customattr_setup>
<setup>
<module>Yourcompany_ModuleName</module>
<class>Mage_Sales_Model_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</customattr_setup>
</resources>
The <class>Mage_Sales_Model_Mysql4_Setup</class> is extremely important!
Then in the install script mysql4-install-0.1.0.php:
<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute(
'order',
'customattr',
array(
'type' => 'float', // or whatever you want here...
'grid' => false
)
);
$installer->endSetup();
?>

Magento 1.4.2 : errors when saving custom customer Attributes

I can't save my custom customer attributes:
My Magento backend is blocked and I get this errors :
2011-08-03T12:27:36+00:00 ERR (3): Warning: include(Mage\Customer\Model\Attribute\Data\.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\xampp\htdocs\magento_test142_3\lib\Varien\Autoload.php on line 93
2011-08-03T12:27:36+00:00 ERR (3): Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'Mage\Customer\Model\Attribute\Data\.php' for inclusion (include_path='C:\xampp\htdocs\magento_test142_3\app\code\local;C:\xampp\htdocs\magento_test142_3\app\code\community;C:\xampp\htdocs\magento_test142_3\app\code\core;C:\xampp\htdocs\magento_test142_3\lib;.;C:\xampp\php\PEAR') in C:\xampp\htdocs\magento_test142_3\lib\Varien\Autoload.php on line 93
This is my module code:
in file mysql4-install-0.1.0.php:
<?php
$installer=new Mage_Customer_Model_Entity_Setup ('core_setup');
$installer->startSetup();
$installer->addAttribute('customer', 'kd_nr', array(
'label' => 'Kundennummer',
'visible' => true,
'required' => false,
'position' => 20,
));
Mage::getSingleton( 'eav/config' )
->getAttribute( 'customer', 'kd_nr' )
->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
->save();
$installer->endSetup();
and here is my config.xml file:
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Newattcustomer>
<version>0.1.1</version>
</Mycompany_Newattcustomer>
</modules>
<global>
<resources>
<newattcustomer_setup>
<setup>
<module>Mycompany_Newattcustomer</module>
</setup>
<connection>
<use>core_setup</use>
</connection>
</newattcustomer_setup>
</resources>
<fieldsets>
<customer_account>
<kd_nr><create>1</create><update>1</update></kd_nr>
</customer_account>
</fieldsets>
</global>
</config>
I use Magento 1.4.2
Please help to solve this problem.
Thanks a lot.
[edit]
Thanks a lot for help, the code of Nasaralla seems to be working, but I discover that my problem come from a YES/NO select: Here is the code for the select box:
$installer->addAttribute('customer', 'in_search', array(
'label' => 'Appear in search',
'type' => 'int',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
'visible' => true,
'required' => false,
));
Mage::getSingleton( 'eav/config' )
->getAttribute( 'customer', 'in_search' )
->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
->save();
It will be very helpful if anyone help me to solve the really problem
[/edit]
Get rid of this part in install script
Mage::getSingleton( 'eav/config' )
->getAttribute( 'customer', 'kd_nr' )
->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
->save();
In order to call eav_setup you do
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
and then you can updateAttribute like:
$setup->updateAttribute ($entityTypeId,$id,$field,$value = null,$sortOrder=null)
You can get the list of methods you can call in here:
Freegento-Eav-Model-entity-setup
Therefore your code, I think, will be replaced by:
$setup->updateAttribute('customer', 'kd_nr', 'used_in_forms', 'adminhtml_customer');
Ok if the above didn't work at all ... I think lets revert back to your original script and I think I can spot some fixes:
<?php
$installer=new Mage_Customer_Model_Entity_Setup ('core_setup');
$installer->startSetup();
$installer->addAttribute('customer', 'kd_nr', array(
'label' => 'Kundennummer',
'is_visible' => 1,
'is_required' => 0,
'sort_order' => 20,
));
$attr = Mage::getSingleton( 'eav/config' )->getAttribute( 'customer', 'kd_nr' );
$attr->setData( 'used_in_forms', array( 'adminhtml_customer' ) )
$attr->save();
$installer->endSetup();
For details check the upgrade script mysql4-data-upgrade-1.4.0.0.7-1.4.0.0.8.php at core/Mage/Customer

Resources