Object Lists

Skip to end of metadata
Go to start of metadata

Once data is available in a structured manner, it can not only be accessed more conveniently but also be filtered, sorted, grouped and displayed intuitivly by the use of an object list. Moreover, data can be exported very easily not only programmatically but also through the pimcore object csv export.

Object lists are a simple way to retrieve objects from pimcore while being able to filter and sort data along that process. Object lists also come with a built-in paginator that simplyfies the display of results in a paged manner.

When working with object lists, user defined routes come in handy while implementing a object detail views. User defined routes allow directing requests to  certain detail pages, even though the request does not portray the path of a document, but matches a certain route.
An object list class  is created automatically for each class defined in pimcore. Objects for the class "myobject" are retrieved through a list as in the following example:

$entries = new Object_Myclassname_List();
$entries ->setOffset($offset);
$entries ->setLimit($perPage);
$entries ->setOrderKey("date");
$entries ->setOrder("desc");
$entries ->setCondition("name LIKE " . $entries->quote("%bernie%")); // make sure that you quote variables in conditions!

foreach ($entries as $entry) {
    $entry->getName();
}

// there is also a shorthand eg.:
$items = Object_Myclassname::getList(array(
    "offset" => $offset,
    "limit" => $perPage,
    "orderKey" => "date",
    "order" => "desc"
));

// order by multiple columns
$items = Object_Myclassname::getList(array(
    "offset" => $offset,
    "limit" => $perPage,
    "orderKey" => array("date", "name"),
    "order" => "desc"
));

// with different directions
$items = Object_Myclassname::getList(array(
    "offset" => $offset,
    "limit" => $perPage,
    "orderKey" => array("name", "date"),
    "order" => array("asc","desc")
));

// with random order
$items = new Object_PhoneProduct_List();
$items->setOrderKey("RAND()", false);

foreach ($items as $item) {
    echo $item . "<br />"; // output the path of the object
}

// with subselect in order
$items = new Object_PhoneProduct_List();
$items->setOrderKey("(SELECT id FROM sometable GROUP BY someField)", false);

Using prepared statement placeholders and variables (since 1.4.2)

The syntax is similar to that from the Zend Framework described here: http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.select.fetchall

$entries = new Object_Myclassname_List();
$entries ->setCondition("name LIKE ?", "%bernie%");
$entries->load();

foreach ...

// using more variables / placeholders

$entries = new Object_Myclassname_List();
$entries ->setCondition("name LIKE ? AND date > ?", array("%bernie%", time()));
$entries->load();

foreach ...

Get Objects matching a value of a property (since 1.2.1)

Often it's very useful to get a list of objects or a single object where a property is matching exactly one value.
This is especially useful to get an object matching a foreign key, or get a list of objects with only one condition.

$result= Object_ClassName::getByMyfieldname($value, [int $limit, int $offset]);


If you set no limit, a list object containing all matching objects is returned. If the limit is set to 1 the first matching object is returned directly.

Examples:

// get a list of cities in Austria
$list = Object_City::getByCountry("AT");
foreach ($list as $city) {
	// do something with the cities
	$city->getZip();
	...
}




// get a city by zip
$city = Object_City::getByZip(5020, 1);
$city->getZip(); // do something with the city



// get the first 10 cities in Austria
$list = Object_City::getByCountry("AT", 10);
foreach ($list as $city) {
	// do something with the cities
	$city->getZip();
}


Get a Object List including unpublished Objects (since 1.0.3, nightly 750)

Normally object lists only give published objects. This can be changed by setting a lists "unpublished" property to true.

Example:

$list = Object_News::getList(
    array(  "unpublished" => true  )
);

or

$list = new Object_News_List();
$list->setUnpublished(true);
$list->load();

Filter Objects by attributes from Field Collections (since 1.4.0)

To filter objects by attributes from frield collections, you can use following syntax (Both code snippets result in the same object list).

$list = new Object_Collectiontest_List();
$list->addFieldCollection("MyCollection", "collection");
$list->addFieldCollection("MyCollection");
$list->setCondition("`MyCollection~collection`.myinput = 'hugo' AND `MyCollection`.myinput = 'testinput'");

or

$list = Object_Collectiontest::getList(array(
   "fieldCollections" => array(
      array("type" => "MyCollection", "fieldname" => "collection"),
      array("type" => "MyCollection")
   ),
   "condition" => "`MyCollection~collection`.myinput = 'hugo' AND `MyCollection`.myinput = 'testinput'"
));

You can add field collections to an list object by specifying the type of the field collection and optionally the fieldname. The fieldname is the fieldname of the field collection in the class definition of the current object.
Once field collections are added to an object list, you can access attributes of field collections in the condition of the object list. The syntax is as shown in the examples above FIELDCOLLECTIONTYPE~FIELDNAME.ATTRIBUTE_OF_FIELDCOLLECTION, or if you have not specified a fieldname FIELDCOLLECTION.ATTRIBUTE_OF_FIELDCOLLECTION.

The object list of this example only delivers objects of the type Collectiontest, which have
+) an Fieldcollection of the type MyCollection and the value testinput in the attribute myinput and
+) an Fieldcollection in the field collection of the type MyCollection and the value hugo in the attribute myinput


Related Forum Topics

  1. Get objects by relation
  2. How to use Zend Paginator with Object Lists

Labels

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