@@ -270,17 +270,13 @@ public function __set( $property, $value ) {
270
270
return ;
271
271
}
272
272
273
- /*
274
- * NOTE: For fast work set STRING value with ENTITY ID
275
- */
276
-
277
273
/* If this is a Lookup field, it MUST be set to an Entity of an appropriate type */
278
274
if ( $ this ->attributes [ $ property ]->isLookup && $ value != null ) {
279
275
/* Check the new value is an Entity */
280
- if ( !$ value instanceOf self ) {
276
+ if ( !$ value instanceOf EntityReference ) {
281
277
$ trace = debug_backtrace ();
282
278
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 '
284
280
. ' class in ' . $ trace [0 ]['file ' ] . ' on line ' . $ trace [0 ]['line ' ], E_USER_ERROR );
285
281
}
286
282
/* Check the new value is the right type of Entity */
@@ -293,8 +289,9 @@ public function __set( $property, $value ) {
293
289
/* Clear any AttributeOf related to this field */
294
290
$ this ->clearAttributesOf ( $ property );
295
291
}
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
298
295
*/
299
296
if ( $ this ->attributes [ $ property ]->optionSet != null && $ this ->attributes [$ property ]->type !== 'Boolean ' ) {
300
297
/* Container for the final value */
@@ -354,7 +351,16 @@ public function __set( $property, $value ) {
354
351
$ value = (bool )$ value ;
355
352
}
356
353
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 ) {
358
364
/* Update the property value with whatever value was passed */
359
365
$ this ->propertyValues [ $ property ]['Value ' ] = $ value ;
360
366
/* Mark the property as changed */
@@ -1306,4 +1312,16 @@ public static function generateCacheKey( $logicalName, $id, $columnSet = null )
1306
1312
1307
1313
return sha1 ( "{$ logicalName }_ {$ id }{$ columnSetString }" );
1308
1314
}
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
+ }
1309
1327
}
0 commit comments