public function load($object, $params = [])
{
$db = Db::get();
$data = null;
if ($object instanceof Object\Concrete) {
if (!method_exists($this, "getLazyLoading") or !$this->getLazyLoading() or array_key_exists("force", $params) && $params["force"]) {
$relations = $db->fetchAll("SELECT * FROM object_relations_" . $object->getClassId() . " WHERE src_id = ? AND fieldname = ? AND ownertype = 'object'", [$object->getId(), $this->getName()]);
} else {
return null;
}
} elseif ($object instanceof Object\Fieldcollection\Data\AbstractData) {
$relations = $db->fetchAll("SELECT * FROM object_relations_" . $object->getObject()->getClassId() . " WHERE src_id = ? AND fieldname = ? AND ownertype = 'fieldcollection' AND ownername = ? AND position = ?", [$object->getObject()->getId(), $this->getName(), $object->getFieldname(), $object->getIndex()]);
} elseif ($object instanceof Object\Localizedfield) {
if (isset($params["context"]) && $params["context"]["containerType"] == "fieldcollection") {
$context = $params["context"];
$fieldname = $context["fieldname"];
$index = $context["index"];
$filter = "/fieldcollection~" . $fieldname . "/" . $index . "/%";
$relations = $db->fetchAll("SELECT * FROM object_relations_" . $object->getObject()->getClassId() . " WHERE src_id = ? AND fieldname = ? AND ownertype = 'localizedfield' AND position = ? AND ownername LIKE ?", [$object->getObject()->getId(), $this->getName(), $params["language"], $filter]);
} else {
$relations = $db->fetchAll("SELECT * FROM object_relations_" . $object->getObject()->getClassId() . " WHERE src_id = ? AND fieldname = ? AND ownertype = 'localizedfield' AND ownername = 'localizedfield' AND position = ?", [$object->getObject()->getId(), $this->getName(), $params["language"]]);
}
} elseif ($object instanceof Object\Objectbrick\Data\AbstractData) {
$relations = $db->fetchAll("SELECT * FROM object_relations_" . $object->getObject()->getClassId() . " WHERE src_id = ? AND fieldname = ? AND ownertype = 'objectbrick' AND ownername = ? AND position = ?", [$object->getObject()->getId(), $this->getName(), $object->getFieldname(), $object->getType()]);
// THIS IS KIND A HACK: it's necessary because of this bug PIMCORE-1454 and therefore cannot be removed
if (count($relations) < 1) {
$relations = $db->fetchAll("SELECT * FROM object_relations_" . $object->getObject()->getClassId() . " WHERE src_id = ? AND fieldname = ? AND ownertype = 'objectbrick' AND ownername = ? AND (position IS NULL OR position = '')", [$object->getObject()->getId(), $this->getName(), $object->getFieldname()]);
}
// HACK END
}
// using PHP sorting to order the relations, because "ORDER BY index ASC" in the queries above will cause a
// filesort in MySQL which is extremely slow especially when there are millions of relations in the database
usort($relations, function ($a, $b) {
if ($a["index"] == $b["index"]) {
return 0;
}
return $a["index"] < $b["index"] ? -1 : 1;
});
$data = $this->getDataFromResource($relations, $object, $params);
return $data;
}