/** * Searches objects by text parameters * @param $classId Id of the class * @param $text Text to lookup in the objects * @param $fullTextSearch if true, the text is searched in all text fields and every coincidence is returned. * If false, it is only looked up in the title. */ function findByText($classId, $text, $fullTextSearch, $folderId = -1) { $objectQuery = new Query("Object"); $criteriaGroup = new CriteriaGroup(CriteriaGroup::getAndSeparator()); // Lookup by class... $criteria = new Criteria($objectQuery, "classID", $classId); $criteriaGroup->addCriterion($criteria); if ($folderId != -1 && $folderId != null) { // Lookup by folder... $objectFolderQuery =& $objectQuery->queryRelationedClass("ObjectFolder"); $folderCriteria = new Criteria($objectFolderQuery, "folderID", $folderId); $criteriaGroup->addCriterion($folderCriteria); } // ... and criterias sent if ($fullTextSearch) { // by fullTextIndex $criteria = new Criteria($objectQuery, "fullTextIndex", $text, Criteria::likeType()); $criteriaGroup->addCriterion($criteria); } else { // Issue a query to ObjectAttribute entity $objectAttributeQuery =& $objectQuery->queryRelationedClass("ObjectAttribute"); // Get the class $baseClassMapper = new BaseClassMapper(); $class = $baseClassMapper->get($classId); /* @var $class BaseClass */ // Get the fields that compose the title $titleClassAttributesArray = $class->getTitleClassAttributes(); // We need to query all title fields using the given value with OR (anyone can match) $criteriaGroupTitle = new CriteriaGroup(CriteriaGroup::getOrSeparator()); foreach ($titleClassAttributesArray as $titleClassAttribute) { // Must belong to the given class and LIKE the given title $criteriaGroupItem = new CriteriaGroup(); /* @var $titleClassAttribute ClassAttribute */ $criteriaGroupItem->addCriterion($criteria); $criteria = new Criteria($objectAttributeQuery, "classAttributeId", $titleClassAttribute->getId()); $criteriaGroupItem->addCriterion($criteria); $criteria = new Criteria($objectAttributeQuery, "value", $text, Criteria::likeType()); $criteriaGroupItem->addCriterion($criteria); $criteriaGroupTitle->addCriterion($criteriaGroupItem); } // Set the title criteria $criteriaGroup->addCriterion($criteriaGroupTitle); } $objectQuery->setCriterion($criteriaGroup); // Execute the query and map the results $objectMapper = new ObjectMapper(); $rv = $objectMapper->mapAll($objectQuery->execute()); return $rv; }
echo "Resulting query: " . $query->getQueryString() . "<br/>"; assert("SELECT DISTINCT ##Object.* FROM ##Object LEFT OUTER JOIN ##Class ON ##Object.ID = ##Class.ObjectID WHERE ##Class.className = 'news'" == $query->getQueryString()); echo "Query with CriteriaGroup instead of only one Criteria<br/>"; $query = new Query("object"); $criteriaGroup = new CriteriaGroup(CriteriaGroup::getAndSeparator()); $criteria = new Criteria($query, "hits", "10", Criteria::lessEqualThanType()); $criteriaGroup->addCriterion($criteria); $criteria = new Criteria($query, "hits", "1", Criteria::moreEqualThanType()); $criteriaGroup->addCriterion($criteria); $query->setCriterion($criteriaGroup); echo "Resulting query: " . $query->getQueryString() . "<br/>"; echo "Query with multiples Criteria Groups and an order<br/>"; $query = new Query("object"); $criteriaGroupRoot = new CriteriaGroup(CriteriaGroup::getAndSeparator()); $criteriaGroupOne = new CriteriaGroup(CriteriaGroup::getAndSeparator()); $criteria = new Criteria($query, "created", "2005-04-15", Criteria::lessThanType()); $criteriaGroupOne->addCriterion($criteria); $criteriaGroupTwo = new CriteriaGroup(CriteriaGroup::getOrSeparator()); $criteria = new Criteria($query, "updatedBy", "1"); $criteriaGroupTwo->addCriterion($criteria); $criteria = new Criteria($query, "updatedBy", "2"); $criteriaGroupTwo->addCriterion($criteria); $criteriaGroupRoot->addCriterion($criteriaGroupOne); $criteriaGroupRoot->addCriterion($criteriaGroupTwo); $query->setCriterion($criteriaGroupRoot); $order = new Order($query, "hits", Order::OrderTypeAsc()); $query->addOrder($order); echo "Resulting query: " . $query->getQueryString() . "<br/>"; $rs = $query->execute(); echo "Executed query: "; print_r($rs);