From aa0a6eb3a4ea1f147b84601f9ff5f87ae2ccfcb0 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 28 Nov 2016 00:00:00 +0100 Subject: [PATCH 1/2] Improved table methods to find relations by property. --- models/Table/ItemRelationsRelation.php | 32 ++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/models/Table/ItemRelationsRelation.php b/models/Table/ItemRelationsRelation.php index 75cbe4b..61c8ad0 100644 --- a/models/Table/ItemRelationsRelation.php +++ b/models/Table/ItemRelationsRelation.php @@ -44,11 +44,12 @@ public function getSelect() * * @param integer $subjectItemId * @param boolean $onlyExistingObjectItems + * @param integer|array $propertyId * @return array */ - public function findBySubjectItemId($subjectItemId, $onlyExistingObjectItems = true) + public function findBySubjectItemId($subjectItemId, $onlyExistingObjectItems = true, $propertyId = null) { - $db = $this->getDb(); + $db = $this->_db; $select = $this->getSelect() ->where('item_relations_relations.subject_item_id = ?', (int) $subjectItemId); if ($onlyExistingObjectItems) { @@ -58,6 +59,17 @@ public function findBySubjectItemId($subjectItemId, $onlyExistingObjectItems = t array() ); } + if ($propertyId) { + if (is_array($propertyId)) { + $select + ->where('item_relations_relations.property_id IN (?)', array_map('intval', $propertyId)); + } + // Single property. + else{ + $select + ->where('item_relations_relations.property_id = ?', (int) $propertyId); + } + } return $this->fetchObjects($select); } @@ -66,11 +78,12 @@ public function findBySubjectItemId($subjectItemId, $onlyExistingObjectItems = t * * @param integer $objectItemId * @param boolean $onlyExistingSubjectItems + * @param integer|array $propertyId * @return array */ - public function findByObjectItemId($objectItemId, $onlyExistingSubjectItems = true) + public function findByObjectItemId($objectItemId, $onlyExistingSubjectItems = true, $propertyId = null) { - $db = $this->getDb(); + $db = $this->_db; $select = $this->getSelect() ->where('item_relations_relations.object_item_id = ?', (int) $objectItemId); if ($onlyExistingSubjectItems) { @@ -80,6 +93,17 @@ public function findByObjectItemId($objectItemId, $onlyExistingSubjectItems = tr array() ); } + if ($propertyId) { + if (is_array($propertyId)) { + $select + ->where('item_relations_relations.property_id IN (?)', array_map('intval', $propertyId)); + } + // Single property. + else{ + $select + ->where('item_relations_relations.property_id = ?', (int) $propertyId); + } + } return $this->fetchObjects($select); } } From cd7ec4940bf57c69663bb8f51fd7c7d22450bcf7 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 28 Nov 2016 00:00:00 +0100 Subject: [PATCH 2/2] Added a method to find relations simpler. --- models/Table/ItemRelationsRelation.php | 31 ++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/models/Table/ItemRelationsRelation.php b/models/Table/ItemRelationsRelation.php index 61c8ad0..b14cf21 100644 --- a/models/Table/ItemRelationsRelation.php +++ b/models/Table/ItemRelationsRelation.php @@ -106,4 +106,35 @@ public function findByObjectItemId($objectItemId, $onlyExistingSubjectItems = tr } return $this->fetchObjects($select); } + + /** + * Find all specified relations. + * + * @internal This is a short for findBy(). + * + * @param integer|array $objectItemId + * @param integer|array $propertyId + * @param integer|array $subjectItemId + * @return array + */ + public function findRelations($subjectItemId = null, $propertyId = null, $objectItemId = null) + { + $params = array(); + if (!is_null($subjectItemId)) { + $params['subject_item_id'] = is_array($subjectItemId) + ? array_map('intval', $subjectItemId) + : (integer) $subjectItemId; + } + if (!is_null($propertyId)) { + $params['property_id'] = is_array($propertyId) + ? array_map('intval', $propertyId) + : (integer) $propertyId; + } + if (!is_null($objectItemId)) { + $params['object_item_id'] = is_array($objectItemId) + ? array_map('intval', $objectItemId) + : (integer) $objectItemId; + } + return $this->findBy($params); + } }