<?php
  require_once('/www/oath-to-your-magento/web/app/Mage.php'); //Path to Magento
  umask(0);
  Mage::app(admin);

  $debug = true;

  
/*
* we need to set attributes for each product on a daily base
* our products are accessoires and parts for mobile phones 
* i.e. a universal screen protector can be used for a lot of phone models
* also, new phone models are being introduced now and then
* the mentioned screen protector could also be an accessoire for this new type
* therefore we decided to generate the attribute connections daily and overwrite the existing values
* 1) a manual mistake while editing a product via backend will be solved the next day automaticly
* 2) we don;t have to test if an attribute value was already set for this product
*  
* $brandvals and $modelvals are being generated from our supplier data on a daily base
* the below fixed values are just an example for debug purposes for 1 product only
* 
* newaspect_id is a custom text attribute
* model and brand are custom multi select attributes
* 
* after updating a product only 255 chars of the string are being sent to the database resulting in 
* 64 connected models instead of 1547
*  
* I tried so select all available models in magento backend and save it manually
* this also will not go beyond 255 chars when viewing the product _origData:protected afterwards 
*/     
$productId = 725;
$brandvals = '266,270,271,272,275,276,277,278,280,282,283,289,290,291,292,293,294,295,296,297,298,299,300,301,302,304,305,306,307,308,310,311,312,313,314,315,316'; 
$modelvals = '317,318,319,320,321,322,323,324,325,326,327,328,329,331,345,346,347,348,349,350,351,352,353,355,356,357,358,359,360,361,362,374,375,376,377,378,379,380,382,383,384,385,386,419,427,428,429,430,431,433,434,435,446,454,456,457,458,459,460,461,462,463,464,465,470,471,472,473,474,475,477,478,479,480,482,484,485,486,490,492,493,494,496,503,505,506,510,511,512,513,514,517,518,529,531,532,533,545,549,550,551,552,553,554,557,558,560,561,562,563,564,565,566,567,568,569,570,571,572,573,575,578,579,580,582,583,584,585,586,587,588,589,590,592,593,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,656,657,658,659,660,661,662,663,664,665,666,667,668,669,670,671,672,674,675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,693,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,712,713,714,715,716,717,718,719,720,721,722,723,732,733,736,737,738,739,740,741,742,743,744,745,746,747,748,749,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,775,776,777,778,779,780,781,784,785,787,788,791,792,2842,2822,2821,2810,2857,803,804,805,806,807,810,811,812,813,814,815,816,817,819,820,822,823,824,825,828,830,841,853,868,869,886,887,889,890,891,892,852,851,867,844,845,846,847,848,849,850,893,895,952,1008,1023,1024,1025,1026,1027,1028,1030,881,880,884,883,882,871,872,873,874,875,876,877,878,879,919,906,907,908,923,924,925,940,942,943,944,945,905,904,920,922,911,910,909,898,899,900,902,903,946,947,948,935,936,842,857,860,862,863,864,933,949,950,939,938,937,927,928,929,930,931,932,1033,965,981,996,997,998,999,1000,1001,1002,1003,1004,1005,2779,980,979,954,955,2813,2794,958,959,960,961,962,963,964,995,994,993,2797,2811,2835,2840,2841,2843,2844,2845,2846,2847,2852,2856,2796,2795,982,984,2778,986,987,988,989,990,991,992,1007,2867,1022,1035,1036,1051,1052,1053,1054,1055,1056,1057,1058,1059,1060,1034,1019,1021,1020,1009,1010,1011,1012,1015,1016,1017,1018,1061,1050,1049,953,968,969,970,971,973,974,975,976,977,978,1083,1062,1048,1037,1038,1039,1040,1041,1042,1044,1045,1046,1047,967,918,1073,1072,1071,1070,1069,1068,1067,1075,1121,1120,1100,1099,1080,1079,1078,1077,1066,1090,1089,1088,1087,1086,1085,1084,966,1091,1092,1081,1097,1095,1094,1093,1122,1123,917,1115,1114,1113,1110,1109,1116,1117,916,915,914,913,912,897,1101,1106,1135,1136,1129,1128,1127,1126,1125,1124,1134,1133,1132,1105,1103,1102,1118,1119,1130,1131,1104,1137,1139,1140,1141,1142,1143,1144,1145,1146,1147,1148,1149,1150,1151,1152,1153,1154,1155,1156,1157,1158,1159,1160,1162,1164,1174,1175,1176,1177,1178,1179,1180,1181,1182,1187,1189,1190,1191,1194,1195,1196,1199,1203,1204,1205,1206,1207,1211,1212,1213,1214,1215,1216,1221,1222,1229,1232,1233,1253,1259,1260,1261,1262,1263,1264,1268,1269,1270,1271,1272,1273,1274,1275,1276,1277,1278,1279,1280,1283,1284,1285,1287,1288,1289,1290,1291,1292,1293,1294,1295,1296,1297,1298,1299,1300,1301,1302,1303,1304,1305,1306,1307,1308,1310,1311,1312,1315,1316,1319,1320,1321,1322,1323,1324,1325,1328,1329,1331,1332,1333,1339,1358,1361,1365,1396,1397,1399,1403,1404,1410,1411,1412,1413,1414,1415,1416,1420,1425,1426,1427,1428,1429,1430,1431,1432,1433,1434,1435,1436,1437,1438,1439,1440,1441,1444,1445,1446,1447,1449,1450,1451,1452,1453,1454,1455,1456,1457,1458,1459,1460,1461,1462,1463,1464,1465,1466,1467,1468,1469,1470,1471,1472,1473,1474,1475,1476,1477,1478,1479,1480,1483,1484,1485,1486,1495,1496,1597,1538,1539,1530,1529,1528,1519,1520,1521,1522,1523,1537,1598,1599,1565,1518,1531,1532,1533,1534,1524,1525,1554,1553,1552,1543,1544,1545,1547,1548,1561,1527,1541,1542,1555,1556,1557,1559,1560,1549,1596,1584,1585,1586,1587,1578,1577,1567,1568,1569,1583,1582,2829,2812,2798,1566,1579,1580,1581,1570,1572,1609,1610,1611,1602,1601,1600,1591,1592,1593,1594,1608,1607,1574,1588,1589,1614,1590,1603,1604,1605,1606,1595,1571,1620,1619,1618,1617,1616,1633,1621,1622,1623,1631,1630,1629,1628,1626,1625,1624,1634,1645,1646,1638,1639,1640,1641,1642,1613,1564,1551,1550,1637,1636,1647,1648,1649,1650,1651,1652,1644,1643,1635,1653,1654,1655,1666,1669,1679,1680,1681,1685,1686,1687,1688,1689,1690,1696,1697,1698,1699,1701,1702,1703,1704,1705,1706,1707,1708,1709,1721,1711,1714,1716,1718,1720,2850,1728,1729,1730,1731,1732,1733,1734,1735,1736,1737,1738,1739,1740,1741,1742,1743,1744,1745,1746,1747,1748,1749,1750,1751,1752,1753,1754,1755,1756,1757,1758,1759,1773,1765,1766,1767,1768,1779,1780,1791,1792,1764,1763,1762,1774,1775,1776,1777,1778,1770,1769,1761,1793,1794,1786,1787,1788,1760,2823,2824,2825,2839,1785,1784,1783,1795,1796,1797,1798,1790,1789,1781,1782,2858,1772,1834,1816,1801,1802,1803,1804,1805,1806,1807,1808,1809,1810,1811,1812,1813,1814,1815,1817,1818,1828,1771,1799,1800,1819,1820,1821,1822,1823,1824,1825,1829,1830,1831,1832,1833,1827,1826,1835,1836,1837,1838,1839,1854,1860,1881,1870,1872,1871,1873,1874,1875,1876,1887,1869,1877,1878,1868,1879,1880,1882,1883,1884,1885,1886,1888,1889,1890,1896,1897,1892,1893,1894,1895,1891,2848,1898,1899,1900,1909,1910,1911,1912,1913,1914,1916,1917,1918,1919,1922,1925,1926,1928,1932,1933,1934,1935,1936,1937,1938,1939,1940,1942,1945,1947,1950,1951,1952,1953,1954,1956,1959,1960,1961,1962,1963,1993,1996,1999,2001,2002,2004,2005,2006,2008,2009,2010,2011,2013,2014,2015,2016,2017,2018,2079,2030,2029,2021,2022,2044,2023,2024,2026,2039,2038,2065,2064,2019,2031,2032,2033,2035,2036,2037,2052,2043,2045,2025,2046,2047,2048,2049,2063,2053,2062,2055,2056,2060,2061,2051,2799,2068,2069,2070,2071,2072,2073,2074,2777,2800,2828,2083,2085,2067,2066,2075,2081,2780,2082,2084,2089,2088,2076,2080,2077,2078,2086,2087,2090,2091,2092,2093,2094,2095,2096,2097,2098,2099,2101,2125,2127,2128,2129,2130,2131,2132,2133,2134,2141,2142,2143,2144,2145,2146,2147,2148,2168,2172,2173,2175,2176,2177,2180,2187,2195,2219,2243,2248,2255,2256,2257,2259,2260,2263,2264,2265,2267,2268,2269,2270,2273,2277,2279,2280,2281,2283,2284,2285,2289,2296,2297,2300,2302,2303,2304,2305,2306,2307,2308,2309,2310,2315,2316,2317,2319,2321,2323,2328,2329,2330,2332,2333,2351,2352,2353,2361,2367,2503,2409,2410,2411,2412,2413,2396,2378,2379,2380,2381,2382,2383,2384,2385,2387,2408,2407,2406,2504,2505,2506,2507,2508,2528,2453,2377,2397,2398,2399,2402,2403,2405,2388,2390,2391,2449,2451,2434,2433,2416,2417,2418,2419,2420,2421,2422,2423,2424,2425,2426,2427,2446,2392,2393,2394,2414,2544,2415,2435,2436,2438,2440,2441,2442,2443,2444,2445,2428,2853,2483,2484,2485,2486,2487,2488,2489,2472,2455,2456,2457,2460,2461,2462,2482,2481,2480,2854,2834,2826,2827,2818,2819,2820,2781,2776,2454,2474,2475,2476,2477,2478,2432,2479,2463,2465,2466,2523,2524,2525,2526,2527,2495,2496,2497,2498,2499,2500,2501,2522,2520,2467,2468,2469,2470,2471,2490,2531,2491,2511,2512,2513,2514,2515,2516,2517,2518,2519,2502,2429,2540,2539,2538,2537,2536,2535,2534,2533,2532,2464,2548,2547,2430,2431,2389,2529,2530,2541,2546,2542,2549,2551,2560,2561,2562,2563,2564,2565,2566,2570,2575,2592,2593,2597,2598,2601,2603,2604,2607,2612,2615,2616,2617,2620,2623,2624,2626,2629,2630,2631,2632,2633,2635,2636,2637,2642,2643,2644,2645,2646,2647,2648,2649,2651,2652,2655,2656,2657,2658,2659,2660,2661,2662,2663,2664,2665,2666,2667,2668,2669,2670,2671,2673,2674,2675,2676,2677,2678,2679,2680,2682,2683';

$update = update2ProductId($productId, $brandvals, $modelvals);



function update2ProductId($productId, $brandvals, $modelvals) {
  // returns 1 if saved, 2 if error, 99 product not exist, 0 no action  

 global $debug; 
 $action = false; 
 $errorlevel = 0;

 if ($debug) {
  var_dump($productId);
  var_dump($prod->sku);
  var_dump($prod->brand);    
  var_dump($prod->model);
 }  

  $prod = Mage::getModel('catalog/product')->loadByAttribute('newaspect_id', $productId);  
  if (!$prod) { $errorlevel = 99; return $errorlevel; }  

  $prod->brand = $brandvals;  
  $prod->model = $modelvals;
  
  if ($debug) { var_dump($prod); }  

      try {  
      $prod->save();
      $errorlevel = 1;            // success
    }
    catch (Exception $ex) {
      //Handle the error    
      $errorlevel = 2;            // general error
    }      
   if ($debug) { var_dump($errorlevel); }
   return $errorlevel;
}
  


?>