Skip to content

Commit 077ae7e

Browse files
committed
Compare two lookup values only by logical name and ID
1 parent dc3748b commit 077ae7e

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

src/Entity.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,13 @@ public function __set( $property, $value ) {
270270
return;
271271
}
272272

273-
/*
274-
* NOTE: For fast work set STRING value with ENTITY ID
275-
*/
276-
277273
/* If this is a Lookup field, it MUST be set to an Entity of an appropriate type */
278274
if ( $this->attributes[ $property ]->isLookup && $value != null ) {
279275
/* Check the new value is an Entity */
280-
if ( !$value instanceOf self ) {
276+
if ( !$value instanceOf EntityReference ) {
281277
$trace = debug_backtrace();
282278
throw new Exception( 'Property ' . $property . ' of the ' . $this->entityLogicalName
283-
. ' entity must be a object of ' . get_class()
279+
. ' entity must be a object of Entity or EntityReference'
284280
. ' class in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_ERROR );
285281
}
286282
/* Check the new value is the right type of Entity */
@@ -293,8 +289,9 @@ public function __set( $property, $value ) {
293289
/* Clear any AttributeOf related to this field */
294290
$this->clearAttributesOf( $property );
295291
}
296-
/* If this is an AlexaCRM\CRMToolkit\Entity\OptionSet field, it MUST be set to a valid OptionSetValue
297-
* according to the definition of the AlexaCRM\CRMToolkit\Entity\OptionSet
292+
293+
/* If this is an OptionSet field, it MUST be set to a valid OptionSetValue
294+
* according to the definition of the OptionSet
298295
*/
299296
if ( $this->attributes[ $property ]->optionSet != null && $this->attributes[$property]->type !== 'Boolean' ) {
300297
/* Container for the final value */
@@ -354,7 +351,16 @@ public function __set( $property, $value ) {
354351
$value = (bool)$value;
355352
}
356353

357-
if ( $this->propertyValues[ $property ]['Value'] != $value ) {
354+
if ( $this->attributes[$property]->isLookup ) {
355+
if ( $this->propertyValues[$property]['Value'] instanceof EntityReference ) {
356+
if ( $this->propertyValues[$property]['Value']->equals( $value ) ) {
357+
return;
358+
}
359+
}
360+
361+
$this->propertyValues[ $property ]['Value'] = $value;
362+
$this->propertyValues[ $property ]['Changed'] = true;
363+
} elseif ( $this->propertyValues[ $property ]['Value'] != $value ) {
358364
/* Update the property value with whatever value was passed */
359365
$this->propertyValues[ $property ]['Value'] = $value;
360366
/* Mark the property as changed */
@@ -1306,4 +1312,16 @@ public static function generateCacheKey( $logicalName, $id, $columnSet = null )
13061312

13071313
return sha1( "{$logicalName}_{$id}{$columnSetString}" );
13081314
}
1315+
1316+
/**
1317+
* Converts the Entity to to a new EntityReference.
1318+
*
1319+
* @return EntityReference
1320+
*/
1321+
public function toEntityReference() {
1322+
$ref = new EntityReference( $this->entityLogicalName, $this->getID() );
1323+
$ref->displayName = $this->displayName;
1324+
1325+
return $ref;
1326+
}
13091327
}

src/Entity/EntityReference.php

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace AlexaCRM\CRMToolkit\Entity;
1919

2020
use AlexaCRM\CRMToolkit\AbstractClient;
21+
use AlexaCRM\CRMToolkit\Entity;
2122
use AlexaCRM\CRMToolkit\KeyAttributes;
2223
use Exception;
2324

@@ -33,14 +34,14 @@ class EntityReference extends AbstractClient {
3334
/**
3435
* The ID of the Entity
3536
*
36-
* @var String corresponds GUID structure in Dynamics CRM
37+
* @var string corresponds GUID structure in Dynamics CRM
3738
*/
3839
protected $entityID = null;
3940

4041
/**
41-
* entityName, display name of Entity object record
42+
* Display name of the Entity object record
4243
*
43-
* @var String
44+
* @var string
4445
*/
4546
protected $displayName = null;
4647

@@ -108,21 +109,20 @@ public function __toString() {
108109
/**
109110
* Private utility function to get the ID field; enforces NULL --> EmptyGUID
110111
*
111-
* @ignore
112-
* @return String GUID if it's existing record, empty GUID otherwise
112+
* @return string GUID if it's existing record, empty GUID otherwise
113113
*/
114114
protected function getID() {
115115
if ( $this->entityID == null ) {
116116
return self::EmptyGUID;
117-
} else {
118-
return $this->entityID;
119117
}
118+
119+
return $this->entityID;
120120
}
121121

122122
/**
123123
* Private utility function to set the ID field; enforces "Set Once" logic
124124
*
125-
* @param String $value
125+
* @param string $value
126126
*
127127
* @throws Exception if the ID is already set
128128
* @return void
@@ -132,6 +132,7 @@ protected function setID( $value ) {
132132
if ( $this->entityID != null ) {
133133
throw new Exception( 'Cannot change the ID of an Entity' );
134134
}
135+
135136
$this->entityID = $value;
136137
}
137138

@@ -152,4 +153,27 @@ public function getFormattedValue( $property, $timezoneOffset = null ) {
152153
return null;
153154
}
154155

156+
/**
157+
* Determines whether two entity references are equal.
158+
*
159+
* @param mixed $reference
160+
*
161+
* @return bool
162+
*/
163+
public function equals( $reference ) {
164+
if ( !( $reference instanceof EntityReference ) ) {
165+
return false;
166+
}
167+
168+
if ( strcasecmp( $this->entityLogicalName, $reference->entityLogicalName ) !== 0 ) {
169+
return false;
170+
}
171+
172+
if ( $this->getID() !== $reference->getID() ) {
173+
return false;
174+
}
175+
176+
return true;
177+
}
178+
155179
}

0 commit comments

Comments
 (0)