To change the attribute type you can create a set-up script and call the following snippet:
$installer->updateAttribute('catalog_product', 'your_attribute_code', array(
'type' => 'varchar',
'input' => 'text',
));
But this will not copy over the currently set values on the products. I am not sure what the best way to cover this would be. Though I can give you the following information:
catalog_product_entity_int
: will store the id of the options selected,
catalog_product_entity_varchar
: is where the varchar attribute values should be stored,
eav_attribute_option
: this will give you the link from options to attributes,
eav_attribute_option_value
: this will give you the text value for each of the attribute's options,
From this you should be able to copy all the attribute values from the integer values stored in catalog_product_entity_int
and make them the relevant text values stored in catalog_product_entity_varchar
Have a look at the methods Mage_Core_Model_Resource_Db_Collection_Abstract::addExpressionFieldToSelect()
for standard models:
/**
* Add attribute expression (SUM, COUNT, etc)
* Example: ('sub_total', 'SUM({{attribute}})', 'revenue')
* Example: ('sub_total', 'SUM({{revenue}})', 'revenue')
* For some functions like SUM use groupByAttribute.
*
* @param string $alias
* @param string $expression
* @param array $fields
* @return Mage_Core_Model_Resource_Db_Collection_Abstract
*/
public function addExpressionFieldToSelect($alias, $expression, $fields)
And Mage_Eav_Model_Entity_Collection_Abstract::addExpressionAttributeToSelect()
for EAV models:
/**
* Add attribute expression (SUM, COUNT, etc)
*
* Example: ('sub_total', 'SUM({{attribute}})', 'revenue')
* Example: ('sub_total', 'SUM({{revenue}})', 'revenue')
*
* For some functions like SUM use groupByAttribute.
*
* @param string $alias
* @param string $expression
* @param string $attribute
* @return Mage_Eav_Model_Entity_Collection_Abstract
*/
public function addExpressionAttributeToSelect($alias, $expression, $attribute)
Usage
$collection->addExpressionFieldToSelect("page_view", "sum({{page_view}})", ["page_view"]);
// ^ alias ^ expression ^ used fields
$collection->getSelect()->group("main_table.entity_id");
or
$collection->addExpressionAttributeToSelect("page_view", "sum({{page_view}})", "page_view");
// ^ alias ^ expression ^ used attribute
$collection->getSelect()->group("main_table.entity_id");
Note that the method suggested in the doc block, groupByAttribute()
is only used to group by EAV attributes, not static columns like the entity id.
Best Answer
Both methods are almost identical, the difference is that
joinField()
only selects a single column from the joined table, while you can specify an array of columns in joinTable`.So you can always use
joinTable
instead ofjoinField
. The exampleis the same as
But
joinField()
should not be used if you need more than one column of the joined table.