/** * Create a new usable Dynamics CRM Entity object * * @param Client $_client * @param String $_logicalName Allows constructing arbitrary Entities by setting the EntityLogicalName directly * @param String $IDorKeyAttributes Allows constructing arbitrary Entities by setting the EntityLogicalName directly * @param array $columnSet List of Entity fields to retrieve * * @internal param AlexaCRM\CRMToolkit\AlexaSDK $_auth Connection to the Dynamics CRM server - should be active already. */ public function __construct(Client $_client, $_logicalName = null, $IDorKeyAttributes = null, $columnSet = null) { try { /* Store AlexaCRM\CRMToolkit\AlexaSDK object */ $this->client = $_client; /* If a new LogicalName was passed, set it in this Entity */ if ($_logicalName != null && $_logicalName != $this->entityLogicalName) { /* If this value was already set, don't allow changing it. */ /* - otherwise, you could have a AlexaSDK_Incident that was actually an Account! */ if ($this->entityLogicalName != null) { throw new Exception('Cannot override the Entity Logical Name on a strongly typed Entity'); } /* Set the Logical Name */ $this->entityLogicalName = $_logicalName; } /* Check we have a Logical Name for the Entity */ if ($this->entityLogicalName == null) { throw new Exception('Cannot instantiate an abstract Entity - specify the Logical Name'); } /* Setup property values from entity metadata attributes */ foreach ($this->attributes as $attribute) { $this->propertyValues[$attribute->logicalName] = array('Value' => null, 'Changed' => false); } /* Check the ID or AlexaCRM\CRMToolkit\KeyAttributes to retrieve the entity values */ if ($IDorKeyAttributes != null) { /* Set EntityValues if specified Entity ID */ if (is_string($IDorKeyAttributes) && self::isGuid($IDorKeyAttributes)) { /* Set the ID of Entity record */ $this->setID($IDorKeyAttributes); /* Get the raw XML data */ $rawSoapResponse = $this->client->retrieveRaw($this, $columnSet); /* NOTE: ParseRetrieveResponse method of AlexaCRM\CRMToolkit\AlexaSDK_Entity class, not the AlexaCRM\CRMToolkit\AlexaSDK class */ $this->parseRetrieveResponse($this->client, $this->LogicalName, $rawSoapResponse); } else { if ($IDorKeyAttributes instanceof KeyAttributes) { if (version_compare($this->client->organizationVersion, "7.1.0", "<")) { throw new Exception('Entity ID must be a valid GUID for the organization version lower then 7.1.0'); } /* Set the keyAttributes array */ $this->keyAttributes = $IDorKeyAttributes; /* Add the KeyAttribute values to the entity object values */ foreach ($IDorKeyAttributes->getKeys() as $key => $attribute) { $this->propertyValues[$key] = array("Value" => $attribute, "Changed" => true); } /* Get the raw XML data */ try { $rawSoapResponse = $this->client->retrieveRaw($this, $columnSet); /* NOTE: ParseRetrieveResponse method of AlexaCRM\CRMToolkit\AlexaSDK_Entity class, not the AlexaCRM\CRMToolkit\AlexaSDK class */ $this->parseExecuteRetrieveResponse($this->client, $this->LogicalName, $rawSoapResponse); } catch (SoapFault $sf) { $errorCode = $sf->faultcode; // undocumented feature if ($errorCode == '-2147088239') { $this->exists = false; } /* ToDo: Return exception with user-friendly details, maybe KeyAttribute parameters invalid */ } } } } } catch (Exception $e) { $this->client->logger->error('Caught exception while creating an Entity object', ['exception' => $e]); } }