public function sync() { $channel = Channel::Shopify; //get all products from shopify $products = Shopify_api::execute('admin/products.json', 'GET'); //first go through products Fancymodel::transStart(); foreach ($products['products'] as $product) { $id = $product['id']; $thingProduct = null; try { $thingProduct = Thing::loadByAltId($id); } catch (InstanceNotFoundException $ex) { //create a thing for the top level product $thingProduct = new Thing(); $thingProduct->setAltid($id); $thingProduct->setName("product"); $thingProduct->setChannel($channel); $thingProduct->save(); } $thingData_raw = $thingProduct->getData(); //indexed numerically //make array indexed by key $thingDatas = array(); //indexed by key foreach ($thingData_raw as $thingData) { $thingDatas[$thingData->getKey()] = $thingData; } //$productMembers = get_object_vars($product); foreach ($product as $key => $value) { //store the things and data for a variant if ($key === "variants") { $this->syncChildren("variant", "variants", $product); } else { if ($key === "options") { $this->syncChildren("option", "options", $product); } else { if ($key === "images" || $key === "image") { } else { //create a new data if not exist in thingData if (!isset($thingDatas[$key])) { $data = new Data(); $data->setKey($key); $data->setValue($value); $data->setThing($thingProduct); $data->save(); } else { $dbData = $thingDatas[$key]; if ($dbData->getValue() != $value) { $dbData->setValue($value); $dbData->save(); } } } } } } } Fancymodel::transCommit(); echo "sync complete"; }
public function setThing($_val) { parent::setOneRelated("Thing", $_val); }