This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4D v20 R7
OB Copy
|
OB Copy ( object {; resolvePtrs | {; option {; groupWith}}} ) -> Function result | ||||||||
Parameter | Type | Description | ||||||
object | Object, Object Field |
![]() |
Structured object | |||||
resolvePtrs | Boolean |
![]() |
True = resolve pointers, False or omitted = do not resolve pointers | |||||
option | Longint |
![]() |
ck shared: return a shared object, ck resolve pointers: resolve pointers before copying |
|||||
groupWith | Collection, Object |
![]() |
Shared collection or object to be grouped with the resulting object | |||||
Function result | Object |
![]() |
Deep copy of object | |||||
The OB Copy command returns an object containing a complete (deep) copy of the properties, sub-objects and values for the object.
If object contains pointer type values, by default the copy also contains the pointers. However, you can resolve pointers when copying by passing True in the resolvePtrs parameter. In this case, each pointer present as a value in object is evaluated when copying and its dereferenced value is used.
Note: If properties of object are shared objects or shared collections, they become regular (not shared) objects or collections in the returned copy. Use the second syntax if you want to return shared elements (see below).
If passed, the option parameter can contain one or both of the following constants:
option | Description |
ck resolve pointers | If the original object contains pointer type values, by default the copy also contains the pointers. However, you can resolve pointers when copying by passing the ck resolve pointers constant. In this case, each pointer present in the object is evaluated when copying and its dereferenced value is used. |
ck shared | By default, OB Copy returns a regular (not shared) object, even if the command is applied to a shared object. Pass the ck shared constant to create a shared object. In this case, you can use the groupWith parameter to associate the shared object with another collection or object (see below). |
The groupWith parameter allows you to designate a collection or an object with which the resulting object should be associated.
Notes:
You want to duplicate an object containing simple values:
C_OBJECT($Object)
C_TEXT($JsonString)
ARRAY OBJECT($arraySel;0)
ALL RECORDS([Product])
While(Not(End selection([Product])))
OB SET($Object;"id";[Product]ID_Product)
OB SET($Object;"Product Name";[Product]Product_Name)
OB SET($Object;"Price";[Product]Price)
OB SET($Object;"Tax rate";[Product]Tax_rate)
$ref_value:=OB Copy($Object) //direct copy
APPEND TO ARRAY($arraySel;$ref_value)
//the contents of $ref_value are identical to those of $Object
NEXT RECORD([Product])
End while
//the contents of $ref_value
$JsonString:=JSON Stringify array($arraySel)
You duplicate an object containing pointers (first syntax):
C_OBJECT($ref)
OB SET($ref;"name";->[Company]name) //object with pointers
OB SET($ref;"country";->[Company]country)
ARRAY OBJECT($sel;0)
ARRAY OBJECT($sel2;0)
ALL RECORDS([Company])
While(Not(End selection([Company])))
$ref_comp:=OB Copy($ref) // copy without evaluating pointers
// $ref_comp={"name":"->[Company]name","country":"->[Company]Country"}
$ref_comp2:=OB Copy($ref;True) //copy with evaluation of pointers
// $ref_comp={"name":"4D SAS","country":"France"}
APPEND TO ARRAY($sel;$ref_comp)
APPEND TO ARRAY($sel2;$ref_comp2)
NEXT RECORD([Company])
End while
$Object:=JSON Stringify array($sel)
$Object2:=JSON Stringify array($sel2)
// $Object = [{"name":"","country":""},{"name":"","country":""},...]
// $Object2 = [{"name":"4D SAS","country":"France"},{"name":"4D, Inc","country":"USA"},{"name":"Catalan","country":"France"}...]
We want to copy the regular (non shared) $person object into the $sharedObject shared object. To do this, we must create a shared copy of the object ($sharedObject).
C_OBJECT($person;$copy;$sharedObject)
C_TEXT($text)
$text:=Document to text(Get 4D folder(Current resources folder)+"person.txt")
$person:=JSON Parse($text) //$person is a standard object
$sharedObject:=New shared object()
$copy:=OB Copy($person;ck shared) //$copy is a shared object
//So it can be put in $sharedObject
Use($sharedObject)
$sharedObject.person:=$copy
End use
$obj contains a pointer ("name" property) on the current record "name" field.
C_OBJECT($obj;$objWithPtr;$sharedObjWithPtr)
$obj:=New object()
//$obj is an object with a pointer
OB SET($obj;"name";->[Persons]name)
ALL RECORDS([Persons])
//Now there is a current record on [Persons] table so [Persons]name is filled
//
// If we want to copy $obj as a standard object with evaluation of pointers
// We do this:
$objWithPtr:=OB Copy($obj;True)
//
// If we want to copy $obj as a shared object with evaluation of pointers
// We do this:
$sharedObjWithPtr:=OB Copy($obj;ck resolve pointers+ck shared)
We want to copy $sharedObj in $sharedColl but since they belong to different shared groups, a direct copy would result in an error. We must make a copy of $sharedObj and designate $sharedColl as shared group for the copy.
C_OBJECT($sharedObj;$objCopy)
C_COLLECTION($sharedColl)
//$sharedObj belongs to a shared group
$sharedObj:=New shared object("lastname";"Smith";"address";New shared object("city";"New York"))
//$sharedColl belongs to another shared group
$sharedColl:=New shared collection(New shared object("lastname";"Brown"))
$objCopy:=OB Copy($sharedObj;ck shared;$sharedColl)
//$objCopy is now in the same shared group as $sharedColl
//So $objCopy can be put in $sharedColl without error
Use($sharedColl)
$sharedColl.push($objCopy)
End use
Product: 4D
Theme: Objects (Language)
Number:
1225
Created: 4D v14
Modified: 4D v15
Modified: 4D v18 R3
Modified: 4D v18 R5
4D Language Reference ( 4D v20 R7)