Пример #1
0
 /**
  * Returns some eager-loaded elements on a given handle.
  *
  * @param string $handle The handle of the eager-loaded elements
  *
  * @return BaseElementModel[]|null The eager-loaded elements, or null
  */
 public function getEagerLoadedElements($handle)
 {
     if (isset($this->_eagerLoadedElements[$handle])) {
         ElementHelper::setNextPrevOnElements($this->_eagerLoadedElements[$handle]);
         return $this->_eagerLoadedElements[$handle];
     }
     return null;
 }
Пример #2
0
 /**
  * Populates element models from a given element query's result set.
  *
  * @param array                $results      The result set of an element query
  * @param ElementCriteriaModel $criteria     The element criteria model
  * @param string               $contentTable The content table that was joined in by buildElementsQuery()
  * @param array                $fieldColumns Info about the content field columns being selected
  *
  * @return BaseElementModel[] The populated element models.
  */
 public function populateElements($results, ElementCriteriaModel $criteria, $contentTable, $fieldColumns)
 {
     $elements = array();
     $locale = $criteria->locale;
     $elementType = $criteria->getElementType();
     $indexBy = $criteria->indexBy;
     foreach ($results as $result) {
         // Do we have a placeholder for this element?
         if (isset($this->_placeholderElements[$result['id']][$locale])) {
             $element = $this->_placeholderElements[$result['id']][$locale];
         } else {
             // Make a copy to pass to the onPopulateElement event
             $originalResult = array_merge($result);
             if ($contentTable) {
                 // Separate the content values from the main element attributes
                 $content = array('id' => isset($result['contentId']) ? $result['contentId'] : null, 'elementId' => $result['id'], 'locale' => $locale, 'title' => isset($result['title']) ? $result['title'] : null);
                 unset($result['title']);
                 if ($fieldColumns) {
                     foreach ($fieldColumns as $column) {
                         // Account for results where multiple fields have the same handle, but from
                         // different columns e.g. two Matrix block types that each have a field with the
                         // same handle
                         $colName = $column['column'];
                         $fieldHandle = $column['handle'];
                         if (!isset($content[$fieldHandle]) || empty($content[$fieldHandle]) && !empty($result[$colName])) {
                             $content[$fieldHandle] = $result[$colName];
                         }
                         unset($result[$colName]);
                     }
                 }
             }
             $result['locale'] = $locale;
             // Should we set a search score on the element?
             if (isset($this->_searchResults[$result['id']])) {
                 $result['searchScore'] = $this->_searchResults[$result['id']];
             }
             $element = $elementType->populateElementModel($result);
             // Was an element returned?
             if (!$element || !$element instanceof BaseElementModel) {
                 continue;
             }
             if ($contentTable) {
                 $element->setContent($content);
             }
             // Fire an 'onPopulateElement' event
             $this->onPopulateElement(new Event($this, array('element' => $element, 'result' => $originalResult)));
         }
         if ($indexBy) {
             $elements[$element->{$indexBy}] = $element;
         } else {
             $elements[] = $element;
         }
     }
     ElementHelper::setNextPrevOnElements($elements);
     // Should we eager-load some elements onto these?
     if ($criteria->with) {
         $this->eagerLoadElements($elementType, $elements, $criteria->with);
     }
     // Fire an 'onPopulateElements' event
     $this->onPopulateElements(new Event($this, array('elements' => $elements, 'criteria' => $criteria)));
     // Fire the criteria's 'onPopulateElements' event
     $criteria->onPopulateElements(new Event($criteria, array('elements' => $elements)));
     return $elements;
 }