├── README.md └── magento-create-setup.php /README.md: -------------------------------------------------------------------------------- 1 | MagentoAttributeMigrationGenerate 2 | ================================= 3 | 4 | A Magento shell script to automatically create an attribute migration script for a specific attribute. This allows developers to tweak their attributes in the UI, and then generate a script for deployment. 5 | 6 | ###Magento Attribute Migration Generator 7 | 8 | This script generates a setup resource script for any existing Magento attribute. 9 | 10 | ./magento-create-setup.php attribute_code 11 | 12 | NOTE: Must be run from the root of the Magento system with the existing attribute. 13 | 14 | Original Blog Post: http://alanstorm.com/magento_attribute_migration_generator 15 | 16 | N98-Magerun 17 | -------------------------------------------------- 18 | There's currently an effort (fully blessed) to migrate this to the `n98-magerun` platform. -------------------------------------------------------------------------------- /magento-create-setup.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | getConnection('core_read'); 74 | $select = $read->select() 75 | ->from('eav_attribute_option') 76 | ->join('eav_attribute_option_value','eav_attribute_option.option_id=eav_attribute_option_value.option_id') 77 | ->where('attribute_id=?',$attribute->getId()) 78 | ->where('store_id=0') 79 | ->order('eav_attribute_option_value.option_id'); 80 | 81 | $query = $select->query(); 82 | 83 | $values = array(); 84 | foreach($query->fetchAll() as $rows) 85 | { 86 | $values[] = $rows['value']; 87 | } 88 | 89 | //$values = array('#f00000','abc123'); 90 | return array('values'=>$values); 91 | } 92 | 93 | function get_key_legend() 94 | { 95 | return array( 96 | 97 | //catalog 98 | 'frontend_input_renderer' => 'input_renderer', 99 | 'is_global' => 'global', 100 | 'is_visible' => 'visible', 101 | 'is_searchable' => 'searchable', 102 | 'is_filterable' => 'filterable', 103 | 'is_comparable' => 'comparable', 104 | 'is_visible_on_front' => 'visible_on_front', 105 | 'is_wysiwyg_enabled' => 'wysiwyg_enabled', 106 | 'is_visible_in_advanced_search' => 'visible_in_advanced_search', 107 | 'is_filterable_in_search' => 'filterable_in_search', 108 | 'is_used_for_promo_rules' => 'used_for_promo_rules', 109 | 110 | 111 | 'backend_model' => 'backend', 112 | 'backend_type' => 'type', 113 | 'backend_table' => 'table', 114 | 'frontend_model' => 'frontend', 115 | 'frontend_input' => 'input', 116 | 'frontend_label' => 'label', 117 | 'source_model' => 'source', 118 | 'is_required' => 'required', 119 | 'is_user_defined' => 'user_defined', 120 | 'default_value' => 'default', 121 | 'is_unique' => 'unique', 122 | 'is_global' => 'global', 123 | 124 | ); 125 | } 126 | 127 | function get_migration_script_for_attribute($code) 128 | { 129 | //load the existing attribute model 130 | $m = Mage::getModel('catalog/resource_eav_attribute') 131 | ->loadByCode('catalog_product',$code); 132 | 133 | if(!$m->getId()) 134 | { 135 | error(sprintf("Could not find attribute [%s].",$code)); 136 | } 137 | //get a map of "real attribute properties to properties used in setup resource array 138 | $real_to_setup_key_legend = get_key_legend(); 139 | 140 | //swap keys from above 141 | $data = $m->getData(); 142 | $keys_legend = array_keys($real_to_setup_key_legend); 143 | $new_data = array(); 144 | foreach($data as $key=>$value) 145 | { 146 | if(in_array($key,$keys_legend)) 147 | { 148 | $key = $real_to_setup_key_legend[$key]; 149 | } 150 | $new_data[$key] = $value; 151 | } 152 | 153 | //unset items from model that we don't need and would be discarded by 154 | //resource script anyways 155 | $attribute_code = $new_data['attribute_code']; 156 | unset($new_data['attribute_id']); 157 | unset($new_data['attribute_code']); 158 | unset($new_data['entity_type_id']); 159 | 160 | //chuck a few warnings out there for things that were a little murky 161 | if($new_data['attribute_model']) 162 | { 163 | echo "//WARNING, value detected in attribute_model. We've never seen a value there before and this script doesn't handle it. Caution, etc. " . "\n"; 164 | } 165 | 166 | if($new_data['is_used_for_price_rules']) 167 | { 168 | echo "//WARNING, non false value detected in is_used_for_price_rules. The setup resource migration scripts may not support this (per 1.7.0.1)" . "\n"; 169 | } 170 | 171 | 172 | //load values for attributes (if any exist) 173 | $new_data['option'] = get_option_array_for_attribute($m); 174 | 175 | //get text for script 176 | $array = var_export($new_data, true); 177 | 178 | //generate script using simple string concatenation, making 179 | //a single tear fall down the cheek of a CS professor 180 | $script = "addAttribute('catalog_product','$attribute_code',\$attr); 189 | 190 | "; 191 | return $script; 192 | } 193 | 194 | function error($error) 195 | { 196 | echo $error . "\n"; 197 | exit; 198 | } 199 | 200 | function usage() 201 | { 202 | echo "USAGE: magento-create-setup.php attribute_code" . "\n"; 203 | } 204 | 205 | function main($argv) 206 | { 207 | $script = array_shift($argv); 208 | $code = array_shift($argv); 209 | if(!$code) 210 | { 211 | usage(); 212 | exit; 213 | } 214 | $script = get_migration_script_for_attribute($code); 215 | echo $script; 216 | } 217 | 218 | bootstrap(); 219 | main($argv); --------------------------------------------------------------------------------