protected function syncChildren($thingName, $member, $parent) { if (!isset($parent[$member])) { throw new DumbassDeveloperException($member . " is not a child member of given parent"); } $channel = Channel::Shopify; $children = $parent[$member]; $parentThing = Thing::loadByAltId($parent["id"]); foreach ($children as $child) { $id = $child['id']; $thingChild = null; try { $thingChild = Thing::loadByAltId($id); } catch (InstanceNotFoundException $ex) { //create a thing for the top level product $thingChild = new Thing(); $thingChild->setAltid($id); $thingChild->setName($thingName); $thingChild->setChannel($channel); $thingChild->setThing($parentThing); //set the parent of the child thing $thingChild->save(); } //all data associated to the child $thingData_raw = $thingChild->getData(); //indexed numerically //make array indexed by key $thingDatas = array(); //indexed by key foreach ($thingData_raw as $thingData) { $thingDatas[$thingData->getKey()] = $thingData; } foreach ($child as $key => $value) { //create a new data if not exist in thingData if (!isset($thingDatas[$key])) { $data = new Data(); $data->setKey($key); $data->setValue($value); $data->setThing($thingChild); $data->save(); } else { $dbData = $thingDatas[$key]; if ($dbData->getValue() != $value) { $dbData->setValue($value); $dbData->save(); } } } } }