/** * add object with Model (if not already added) * @param Object $pObject * @param boolean $pThrowException throw exception if object can't be added (no complete id or object already added) * @throws \Exception */ public function addObject(Object $pObject, $pThrowException = true) { if ($pObject->hasCompleteId()) { if ($pObject->getModel()->hasIdProperty()) { $lModelName = $pObject->getModel()->getModelName(); $lId = $pObject->getId(); if (!array_key_exists($lModelName, $this->mMap)) { $this->mMap[$lModelName] = array(); } // if object NOT already added, we can add it and create attached LocalObjectCollection if (!array_key_exists($lId, $this->mMap[$lModelName])) { $this->mMap[$lModelName][$lId] = $pObject; } else { if ($pThrowException) { throw new \Exception('object already added'); } } } } else { if ($pThrowException) { trigger_error(json_encode($pObject->toObject())); trigger_error($pObject->getModel()->getModelName()); trigger_error(json_encode($pObject->getModel()->getIdproperties())); throw new \Exception('object can\'t be added, object has no id or id is incomplete'); } } }
$lResult = ObjectService::getObjects(json_decode($Json)); if (!is_object($lResult) || !isset($lResult->success) || !$lResult->success || !isset($lResult->result) || !is_array($lResult->result)) { throw new Exception('bad ObjectService::getObjects return ' . json_encode($lResult)); } if (json_encode($lResult->result) != '[{"id1":"1","id2":"23","date":"2016-05-01T14:53:54+02:00","timestamp":"2016-10-16T21:50:19+02:00","string":"aaaa","integer":"0","mainParentTestDb":"1"},{"id1":"1","id2":"101","date":"2016-04-13T09:14:33+02:00","timestamp":"2016-10-16T21:50:19+02:00","object":{"plop":"plop","plop2":"plop2"},"objectWithId":{"plop":"plop","plop2":"plop2"},"string":"cccc","integer":"2","mainParentTestDb":"1"}]') { throw new Exception('bad objects : ' . json_encode($lResult->result)); } /** ****************************** test following export import objects ****************************** **/ $lBasedObjects = [json_decode('{"id1":"1","id2":"23","date":"2016-05-01T14:53:54+02:00","timestamp":"2016-10-16T21:50:19+02:00","string":"aaaa","integer":0,"mainParentTestDb":"1"}'), json_decode('{"id1":"1","id2":"101","date":"2016-04-13T09:14:33+02:00","timestamp":"2016-10-16T21:50:19+02:00","string":"cccc","integer":2,"object":{"plop":"plop","plop2":"plop2"},"objectWithId":{"plop":"plop","plop2":"plop2"},"mainParentTestDb":"1"}')]; $lObject = null; foreach ($lResult->result as $lIndex => $lPhpObject) { $lObject = new Object('testDb'); $lObject->fromObject($lPhpObject); $lObject2 = new Object('testDb'); $lObject2->fromXml($lObject->toXml(false)); if (json_encode($lObject2->toObject(false, 'Europe/Berlin')) !== json_encode($lBasedObjects[$lIndex])) { throw new Exception('bad object : ' . json_encode($lObject2->toObject(false, 'Europe/Berlin'))); } } /** ****************************** test DateTime/DateTimeZone with database serialization ****************************** **/ $lDbTestModel = InstanceModel::getInstance()->getInstanceModel('testDb'); $lObject = $lDbTestModel->loadObject('[1,1501774389]'); $lObjectJson = $lObject->toObject(); $lObject->getValue('timestamp')->sub(new DateInterval('P0Y0M0DT5H0M0S')); $lObject->save(SqlTable::UPDATE); $lObject = $lDbTestModel->loadObject('[1,1501774389]', true); $lObject->getValue('timestamp')->add(new DateInterval('P0Y0M0DT5H0M0S')); $lObject->save(SqlTable::UPDATE); $lObject = $lDbTestModel->loadObject('[1,1501774389]', true); if (json_encode($lObject->toObject()) !== json_encode($lObjectJson)) { throw new Exception('bad object');