From 48274a3bb170696c1514db0a3e0fc9484c2d495a Mon Sep 17 00:00:00 2001 From: ben Date: Wed, 21 May 2008 14:44:02 +0000 Subject: Metadata can now be added to before an entity is saved. It will be added to the db on save. git-svn-id: https://code.elgg.org/elgg/trunk@667 36083f99-b078-4883-b0ff-0f9b5a30f544 --- engine/lib/entities.php | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'engine/lib') diff --git a/engine/lib/entities.php b/engine/lib/entities.php index 2cadf199c..850da0008 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -31,7 +31,8 @@ * Subclasses should add to this in their constructors. * Any field not appearing in this will be viewed as a */ - protected $attributes; + protected $attributes; + protected $temp_metadata; /** * Initialise the attributes array. @@ -45,6 +46,7 @@ { // Create attributes array if not already created if (!is_array($this->attributes)) $this->attributes = array(); + if (!is_array($this->temp_metadata)) $this->temp_metadata = array(); $this->attributes['guid'] = ""; $this->attributes['type'] = ""; @@ -74,10 +76,15 @@ return $this->attributes[$name]; } - // No, so see if its in the meta data for this entity - $meta = $this->getMetaData($name); - if ($meta) - return $meta; + // No, so see if its in the meta data for this entity + if (isset($this->guid)) { + $meta = $this->getMetaData($name); + if ($meta) + return $meta; + } else { + if (isset($this->temp_metadata[$name])) + return $this->temp_metadata[$name]; + } // Can't find it, so return null return null; @@ -108,8 +115,11 @@ $this->attributes[$name] = $value; } - else - return $this->setMetaData($name, $value); + else if (isset($this->guid)) { + return $this->setMetaData($name, $value); + } else { + $this->temp_metadata[$name] = $value; + } return true; } @@ -371,6 +381,14 @@ { $this->attributes['guid'] = create_entity($this->attributes['type'], $this->attributes['subtype'], $this->attributes['owner_guid'], $this->attributes['access_id']); // Create a new entity (nb: using attribute array directly 'cos set function does something special!) if (!$this->attributes['guid']) throw new IOException("Unable to save new object's base entity information!"); + + // Save any unsaved metadata + if (sizeof($this->temp_metadata) > 0) { + foreach($this->temp_metadata as $name => $value) { + $this->$name = $value; + unset($this->temp_metadata[$name]); + } + } return $this->attributes['guid']; } -- cgit v1.2.3