Structured Data Fields - Objectbricks

Skip to end of metadata
Go to start of metadata

Objectbricks (since 1.4.0)

With Objectbricks, objects can be extended without changing the class definition. This is especially useful when storing product data.
Often there is a base set of attributes, which all products have. But then there are lots of attributes, which only a subset of your products has.

Take the example of a car accessories dealer. Brakes have different attributes than tires, rims or navigation systems.
The old fashion way to deal with this use case is, to define a product class which contains all possible attributes from all product types. This would work fine, but most of the attributes will be empty and the object editor would be quite unmanageable.

The new way is to use Objectbricks. The product class itself has only attributes all products have. This might be attributes like name, article number, manufacturer, price, etc.
In addition to that, for each product group there is an Objectbrick. The Objectbrick for brakes has attributes like diameter, material. The Objectbrick for tires has dimension, type, maximum speed and so on.

By creating a tire product object, the tire Objectbrick is added and so this tire product has all the tire attributes.

To one object a number of Objectbricks can be added, but just one instance per Objectbrick type. This is the main difference to Fieldcollections.
Because only one instance per Objectbrick can be added to an object, Objectbricks fully support inheritance. Each attribute of an Objectbrick can be overwritten in child objects.

Despite this flexibility, the database model in the background stays clean and well structured. This is because the attributes of an Objectbrick have to be defined like these of Fieldcollections.

Definition of an Objectbrick

As mentioned before, Objectbricks themselves are defined the same way as objects and Fieldcollections are and support the same data types as Fieldcollections.

To allow adding an Objectbrick to an object, two things have to be done:

  • There is a new data type Objectbricks for classes. This data type defines, where Objectbricks can be added. A field of this data type has to be added to the object class.

  • In the Objectbrick definition, the object class and the desired field has to be added to the allowed classes.

Retrieving Objectbricks via code

By saving the object class, for each Objectbrick field of this class there is an own data class created with getters for each allowed Objectbrick.
For our example, this data class would look for example like this.

The getter $product->getBricks() returns an instance of this class filled with the Objectbricks of the $product. By calling a Objectbrick type getter, the Objectbrick class with its attribute getter is returned.

//Snippet 1 - receiving data of a Objectbrick
$product = Object_Product::getById(4);
$tiretype = $product->getBricks()->getTire()->getTireType();

Setting data works the same way as retrieving data. For all getters there are corresponding setters. By saving an object, all bricks are saved too.

//Snippet 2 - setting data of a Objectbrick
$product = Object_Product::getById(4);
$product->getBricks()->getTire()->setTireType("Winter");
$product->save();

//Snippet 3 - adding a new Objectbrick to an object
$product = new Object_Product();
$product->setKey("testproduct");
$product->setParent(Object_Product::getById(4));

$product->setName("testproduct");

$tireBrick = new Object_Objectbrick_Data_Tire($product);
$tireBrick->setTireType("allyear");
$product->getBricks()->setTire($tireBrick);
$product->save();

Querying for Objectbrick data

Data of Objectbricks can be queried in the same way as data of fieldcollections. The Objectbricks have to be added to the Object_List object and then the Objectbrick data can be queried in the condition like in the sample below.

//Snippet 4 - querying for Objectbrick data
$productList = Object_Product::getList(array(
	/* add here all Objectbricks you need in the condition */
	"objectbricks" => array("Tire","Brake"),
	/* in the condition access Objectbrick attributes with OBJECTBRICKNAME.ATTRIBUTENAME */
	"condition" => "Tire.dimension > 200"
));

If you want to obtain a list of objects which have a specific Objectbrick you can query for the "fieldname" value in the condition statement.

//Snippet 5 - return all Products that have the Objectbrick "Tire"
$productList = Object_Product::getList(array(
	/* add here all Objectbricks you need in the condition */
	"objectbricks" => array("Tire"),
	/* in the condition access Objectbrick attributes with OBJECTBRICKNAME.ATTRIBUTENAME */
	"condition" => "Tire.fieldname != ''"
));

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.