Skip to content

Commit

Permalink
Normale Felder und ID-Felder sind getrennt und können einzeln angefra…
Browse files Browse the repository at this point in the history
…gt werden.
  • Loading branch information
Sebastian Blum committed Dec 19, 2016
1 parent 0dbe0b3 commit fa908d4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
35 changes: 26 additions & 9 deletions Visionline/Crm/WebApi/CacheEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,27 @@ class CacheEntry
public $lastModified;

/**
* The field values of the the entity
* The field values of the entity
* @var array
*/
public $fields;

/**
* The field values of the entity as ids
* @var array
*/
public $idFields;

/**
* Create a cache entry
* @param string $type The entity type
* @param int $id The entity id
* @param int $lastModified The date of the last modification of the entity
* @param array $fields The field values of the the entity
* @param array $idFields The field values of the the entity containing ids instead of names
* @throws \InvalidArgumentException If invalid arguments were supplied
*/
public function __construct($type, $id, $lastModified = null, $fields = array())
public function __construct($type, $id, $lastModified = null, $fields = array(), $idFields = array())
{
if (!isset($type))
{
Expand All @@ -60,6 +67,18 @@ public function __construct($type, $id, $lastModified = null, $fields = array())
$this->id = $id;
$this->lastModified = $lastModified;
$this->fields = $fields;
$this->idFields = $idFields;
}

/**
* Hook after deserialization
*/
public function __wakeup()
{
// make sure $this->idFields is an array
if (!isset($this->idFields) || !$this->idFields) {
$this->idFields = array();
}
}

/**
Expand Down Expand Up @@ -98,22 +117,20 @@ public function merge(CacheEntry $other)
$result = new CacheEntry($this->type, $this->id);
$result->lastModified = max($this->lastModified, $other->lastModified);
$result->fields = array_merge($this->fields, $other->fields);

$result->idFields = array_merge($this->idFields, $other->idFields);

return $result;
}

/**
* Computes the key under which a cache entry can be stored or retrieved.
* @param string $type The entity type
* @param string $id the entity id
* @param string $language the language
* @param bool $return_ids Whether the cache entry contains IDs instead of values for relation fields
* @return string The key under which a cache entry can be stored or retrieved.
*/
public static function computeKey($type, $id, $language, $return_ids)
public static function computeKey($type, $id, $language)
{
return $type . '#' . $id . '-' . strtolower($language) . ($return_ids ? '-ids' : '');
return $type . '#' . $id . '-' . strtolower($language);
}
}

?>
6 changes: 3 additions & 3 deletions Visionline/Crm/WebApi/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,17 @@ public function uniqueResult()
/**
* Runs this query by calling result() and returns the specified fields of the matching entities by calling WebApi::get for the query results.
* @param array $fields An array of strings specifying which fields should be returned. The strings in the array have to be UTF-8 encoded.
* @param bool $return_ids Whether to return ids instead of values for relations
* @param array $idFields An array string string specifying fields that should be returned with their id value instead of their name. The strings in the array have to be UTF-8 encoded.
* @return array A two-dimensional array that contains the results. The keys of the first level are
* the ids of the entities. The keys of the second level are the field identifiers and values are
* the corresponding field values. The field values are UTF-8 encoded strings.
* @throws \SoapFault if a remote error occurs.
* @see WebApi::get
*/
public function fields(array $fields, $return_ids = false)
public function fields(array $fields, array $idFields = array())
{
$queryResults = $this->result();
$fieldResults = $this->webapi->get($this->type, $queryResults, $fields, $return_ids);
$fieldResults = $this->webapi->get($this->type, $queryResults, $fields, $idFields);

uksort($fieldResults, function($id1, $id2) use ($queryResults) {
foreach ($queryResults as $queryResult)
Expand Down
28 changes: 14 additions & 14 deletions Visionline/Crm/WebApi/WebApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -1175,24 +1175,24 @@ public function update($type, $id, array $values) {
* multiple ids of entities (array of int), a query result describing an entity (QueryResult) or multiple query results describing entities
* (array of QueryResult)
* @param array $fields The requested fields (array of string). The strings in this array have to be UTF-8 encoded.
* @param boolean $return_ids Whether to return ids instead of values for relations
* @param array $idFields The requested fields as ids (array of string). The strings in this array have to be UTF-8 encoded.
* @return array A two-dimensional array that contains the results. The keys of the first level are
* the ids of the entities. The keys of the second level are the field identifiers and values are
* the corresponding field values.
* @see EntityType
* @see QueryResult
*/
public function get($type, array $which, array $fields, $return_ids = false)
public function get($type, array $which, array $fields, array $idFields = array())
{
$results = array();

if (is_int($which))
{
return $this->get($type, array($which), $fields, $return_ids);
return $this->get($type, array($which), $fields, $idFields);
}
else if ($which instanceof QueryResult)
{
return $this->get($type, array($which), $fields, $return_ids);
return $this->get($type, array($which), $fields, $idFields);
}
else if (is_array($which))
{
Expand All @@ -1219,19 +1219,19 @@ public function get($type, array $which, array $fields, $return_ids = false)
}

// If cache is available, read cache entries where possible
$getIds = $this->cacheRead($type, $results, $fields, $return_ids);
$getIds = $this->cacheRead($type, $results, $fields, $idFields);

if (count($getIds) > 0)
{
$this->debug('get: Going to call _Get for ids = ', $getIds);

// Call webservice
$getResults = $this->_Get($type, $getIds, $fields, $return_ids);
$getResults = $this->_Get($type, $getIds, $fields, $idFields);

foreach ($getResults as $id => $fields)
{
// If cache is available, write cache entries
$this->cacheWrite($type, $id, $results[$id]->lastModified, $fields, $return_ids);
$this->cacheWrite($type, $id, $results[$id]->lastModified, $fields, $idFields);

$results[$id] = $fields;
}
Expand All @@ -1246,25 +1246,27 @@ public function get($type, array $which, array $fields, $return_ids = false)
* @param int $id The entity id
* @param int $lastModified The entitys last modification date
* @param array $fields The entitys fields
* @param boolean $return_ids Whether the cache entry contains IDs instead of values for relation fields
* @param array $idFields The entity`s fields with ids instead of names
*/
private function cacheWrite($type, $id, $lastModified, $fields, $return_ids = false)
private function cacheWrite($type, $id, $lastModified, $fields, $idFields = array())
{
// If cache is available, store retrieved data
if (isset($this->cache))
{
// compute key
$key = CacheEntry::computeKey($type, $id, $this->language);

// Create cache entry
$cacheEntry = new CacheEntry($type, $id, $lastModified, $fields, $return_ids);
$cacheEntry = new CacheEntry($type, $id, $lastModified, $fields, $idFields);

// If an up-to-date cache entry already exists, merge it with the new one
$oldCacheEntry = $this->cache->get($cacheEntry->getKey($this->language));
$oldCacheEntry = $this->cache->get($key);
if ($oldCacheEntry != null && $oldCacheEntry->lastModified >= $cacheEntry->lastModified)
{
$cacheEntry = $cacheEntry->merge($oldCacheEntry);
}

// Put entry into cache
$key = $cacheEntry->getKey($this->language);
$this->cache->put($key, $cacheEntry);

$this->debug('get: Stored cache entry for key = ', $key);
Expand Down Expand Up @@ -1454,5 +1456,3 @@ public function getInterests($contacts, array $filterByStatus = null)
}
}
}

?>

0 comments on commit fa908d4

Please sign in to comment.