Elgg\Cache\MetadataCache::populateFromEntities PHP Method

populateFromEntities() public method

Populate the cache from a set of entities
public populateFromEntities ( integer | array $guids ) : void
$guids integer | array Array of or single GUIDs
return void
    public function populateFromEntities($guids)
    {
        if (empty($guids)) {
            return;
        }
        $version = (int) elgg_get_config('version');
        if (!empty($version) && $version < 2016110900) {
            // can't use this during upgrade from 2.x to 3.0
            return;
        }
        $access_key = $this->getAccessKey();
        if (!is_array($guids)) {
            $guids = array($guids);
        }
        $guids = array_unique($guids);
        // could be useful at some point in future
        //$guids = $this->filterMetadataHeavyEntities($guids);
        $db_prefix = _elgg_services()->db->prefix;
        $options = array('guids' => $guids, 'limit' => 0, 'callback' => false, 'distinct' => false, 'order_by' => 'n_table.entity_guid, n_table.time_created ASC, n_table.id ASC', 'wheres' => array(_elgg_get_access_where_sql(array('table_alias' => 'n_table', 'guid_column' => 'entity_guid'))));
        $data = _elgg_services()->metadataTable->getAll($options);
        // make sure we show all entities as loaded
        foreach ($guids as $guid) {
            $this->values[$access_key][$guid] = null;
        }
        // build up metadata for each entity, save when GUID changes (or data ends)
        $last_guid = null;
        $metadata = array();
        $last_row_idx = count($data) - 1;
        foreach ($data as $i => $row) {
            $name = $row->name;
            $value = $row->value_type === 'text' ? $row->value : (int) $row->value;
            $guid = $row->entity_guid;
            if ($guid !== $last_guid) {
                if ($last_guid) {
                    $this->values[$access_key][$last_guid] = $metadata;
                }
                $metadata = array();
            }
            if (isset($metadata[$name])) {
                $metadata[$name] = (array) $metadata[$name];
                $metadata[$name][] = $value;
            } else {
                $metadata[$name] = $value;
            }
            if ($i == $last_row_idx) {
                $this->values[$access_key][$guid] = $metadata;
            }
            $last_guid = $guid;
        }
    }

Usage Example

 public function testPopulateFromEntities()
 {
     // test populating cache from set of entities
     $this->obj1->setMetadata($this->name, $this->value);
     $this->obj1->setMetadata($this->name, 4, 'integer', true);
     $this->obj1->setMetadata("{$this->name}-2", "{$this->value}-2");
     $this->obj2->setMetadata($this->name, $this->value);
     $this->cache->clearAll();
     $this->cache->populateFromEntities(array($this->guid1, $this->guid2));
     $this->assertIdentical($this->cache->getSingle($this->guid1, $this->name), [$this->value, 4]);
     $this->assertIdentical($this->cache->getSingle($this->guid1, "{$this->name}-2"), "{$this->value}-2");
     $this->assertIdentical($this->cache->getSingle($this->guid2, $this->name), $this->value);
 }
All Usage Examples Of Elgg\Cache\MetadataCache::populateFromEntities