Subscribe To RSS Feed


Mike D's SharpBlog

Just another Sharpdot weblog

Archive for April, 2009

Transfering Magento Database

- Wednesday, April 8th, 2009 -

*Note: The Best way to migrate the database is to do it command line, using mysqldump.

I have Had a few problems when transferring a Magento database. Usually when pushing a development site to Production. Importing the database sometimes throws foreign key constraint errors. What I have found is that if you use phpmyadmin to export the database you need to check the box next to disable foreign key checks. Then you can import this database with out getting the constraint errors. Exporting the database through Magento’s built in functionality does this automatically.

The next issue I have run into is that( I believe, but don’t quote me ) mysql versions prior to 4.1 don’t automatically add the commands to allow inserting primary keys with an id of 0. This will cause Magento to throw an error. The way I have found to fix this is by manually editing a few db entries to set them to an id of 0. The tables I found that need to be edited are: core_store, core_website, core_store_group, and customer_group. I believe it will allays be the last entry that needs to be set to zero, but if in doubt compare it against the old database.

Hope this helps someone else, and let me know of any other work-arrounds to this issue.

Tags: ,
Posted in magento | 2 Comments »

Magento: Getting product attributes values and labels

- Monday, April 6th, 2009 -

I have found that it is very useful to be able to get attributes from the system and use them in places other than a products category page. I always forget the exact syntax to use so, this is going to be my unofficial cheat sheet.

This is how to get a drop down lists options. I don’t think it will work for a mulit-select attribute. I stick the value/label pairs into an array to use how I please.

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
$attrubuteArray[$option['value']] = $option['label'];
}

I had a trickier time getting values for a multi-select attribute. I don’t think that this is the best method, but it is one that worked for me. First the multi-select attribute must be set to be used in the advanced search. You can set this in the manage attributes area.

$attributes = Mage::getModel('catalogsearch/advanced')->getAttributes();
$attributeArray=array();
foreach($attributes as $a){
if($a->getAttributeCode() == 'desired_attribute_code'){
foreach($a->getSource()->getAllOptions(false) as $option){
$attributeArray[$option['value']] = $option['label'];
}
}
}

If you only need to retrieve a value for a product you can try this way

//Referenced from /app/code/core/Mage/Eav/Model/Config/php @ line 443
$_product->getResource()->getAttribute('club_type')->getFrontend()->getValue($_product)

I added this code snip it below to possibly answer a question that was posted. The code below will get an attribure collection. Set {entityType} to 4 to get attributes for products. You can remove the “setCodeFilters” line if you want to get all the attributes. To get anything really useful you will probably need to get the resulting attribute ids and do someting with them, like use them in a filter for the products collection or something.

//
$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter({entityType})
->setCodeFilter($attributes)
->addSetInfo()
->getData();

Entity Type Id’s
$entityType is an integer id for what type of entity the attribute is associated to. If you look at the “eav_attribute” table you will see that each attribute has an entity_type_id.
1 = Customer Entity
2 = Shipping Entity (I believe)
3 = Category Entity
4 = Product Entity

The Following code was added to answer a question in the replies:
To make a product attribute avaliable when getting a product collection(such as on a category page or search results page) you can add some code the a config.xml file that instructs Magento to load allways the attribute when a product collection is loaded.

How to add anĀ  attribute to be loaded whenever a product collection is loaded:
This can go in any config.xml file. I would recommend putting it in a custom module rather than one from the core code. Just replace “attribute_name” with the attribute code of the attribute you are trying to add.

<frontend>
<product>
<collection>
<attributes>
<attribute_name/>
</attributes>
</collection>
</product>
</frontend>

Tags:
Posted in magento | 32 Comments »