Element builder: Difference between revisions
page created |
+ Record element |
||
(One intermediate revision by the same user not shown) | |||
Line 10: | Line 10: | ||
* When it is a namespaced class name it is used as such | * When it is a namespaced class name it is used as such | ||
* When it is a string it is used as a property name and looked up via the Properties facade. | * When it is a string it is used as a property name and looked up via the Properties facade. | ||
== Using the property name as a method == | |||
It's also possible to use the property name as a method. This method accepts excactly one string Parameter and that is the property name. | |||
Example | |||
$builder->integer("int_element"); | |||
Creates an element of type integer, names it "int_element" and returns it. | |||
== Arrays == | |||
Of course it is possible to include an array as a record member. Just use the method array(string $name). Note that is required to pass the expected data type(s) for the array (see [[Array properties]]). | |||
Example | |||
$builder->array('array_element')->setAllowedElementType(TypeInteger::class); // Creates an array of integer | |||
== Records == | |||
Other records can also be an element of a record. There is, however, a difference to other types: A method with the name record() does not exists. Instead you have to decide, how the record should be included. | |||
=== embedRecord(string $record) or includeRecord(string $record) === | |||
When using one of these methods, the elements of that record are included in the current recors as if they where their own elements. | |||
Example: | |||
Let's assume there is a record called "ChildRecord" with the elements "int_element" and "string_element". | |||
$builder->integer('own_int_element'); | |||
$builder->embedRecord(ChildRecord::class); | |||
or | |||
$builder->integer('own_int_element'); | |||
$builder->includeRecord(ChildRecord::class); | |||
will create a record with three elements: "own_int_element", "int_element" and "string_element". So you can use | |||
$record->own_int_element = 123; | |||
$record->int_element = 234; | |||
$record->string_element = 'ABC'; | |||
The difference between the two methods is how meta informations about the elements are passed to the storage. While embedRecord() preserves the original class as a meta information, includeRecord() lets the element appear to come from the including record. For more details see [[Storages]]. | |||
=== referRecord(string $name, string $record) === | |||
While the first methods include the elements of the included records and lets them seem to be the records own elements, this method created a new property that stands for the the included record. | |||
Example: | |||
$builder->referRecord('record_element',ChildRecord::class); | |||
creates a record with a property called "record_element". | |||
$record->record_element->int_element = 123; // valid statement | |||
$record->int_element = 123; // throws an exception | |||
== Calculated fields == | |||
== InfoMarket refernces == | |||
[[Category:Properties]] | [[Category:Properties]] | ||
[[Category:Helpers]] | [[Category:Helpers]] |
Latest revision as of 08:44, 30 September 2024
The ElementBuilder class is a helper for adding elements to a record property.
__construct(RecordProperty $owner)
Takes the owning record property as a parameter for later use.
Adding a property
addProperty(string $property_name, string $name): AbstractProperty
Decides how $property_name is resolved:
- When it is a namespaced class name it is used as such
- When it is a string it is used as a property name and looked up via the Properties facade.
Using the property name as a method
It's also possible to use the property name as a method. This method accepts excactly one string Parameter and that is the property name.
Example
$builder->integer("int_element");
Creates an element of type integer, names it "int_element" and returns it.
Arrays
Of course it is possible to include an array as a record member. Just use the method array(string $name). Note that is required to pass the expected data type(s) for the array (see Array properties).
Example
$builder->array('array_element')->setAllowedElementType(TypeInteger::class); // Creates an array of integer
Records
Other records can also be an element of a record. There is, however, a difference to other types: A method with the name record() does not exists. Instead you have to decide, how the record should be included.
embedRecord(string $record) or includeRecord(string $record)
When using one of these methods, the elements of that record are included in the current recors as if they where their own elements.
Example: Let's assume there is a record called "ChildRecord" with the elements "int_element" and "string_element".
$builder->integer('own_int_element'); $builder->embedRecord(ChildRecord::class);
or
$builder->integer('own_int_element'); $builder->includeRecord(ChildRecord::class);
will create a record with three elements: "own_int_element", "int_element" and "string_element". So you can use
$record->own_int_element = 123; $record->int_element = 234; $record->string_element = 'ABC';
The difference between the two methods is how meta informations about the elements are passed to the storage. While embedRecord() preserves the original class as a meta information, includeRecord() lets the element appear to come from the including record. For more details see Storages.
referRecord(string $name, string $record)
While the first methods include the elements of the included records and lets them seem to be the records own elements, this method created a new property that stands for the the included record.
Example:
$builder->referRecord('record_element',ChildRecord::class);
creates a record with a property called "record_element".
$record->record_element->int_element = 123; // valid statement $record->int_element = 123; // throws an exception