public function update($attrs) { $myProps = array(); if ($attrs["name"]) { if ($attrs["name"] !== $this->name) { $attrs["nickname"] = $attrs["name"]; } } // Don't update restricted stuff foreach (self::$const_columns as $restricted) { unset($attrs[$restricted]); } // Move properties to where they belong $properties = $attrs["properties"] ?: $attrs; foreach ($properties as $prop => $val) { if (self::$columns[$prop]) { $myProps[$prop] = $val; } else { if ($this->properties[$prop]) { $result = $this->properties[$prop]->update(["value" => $val]); if (!$result) { throw new \Exception("Property " . $prop . " failed to update"); } } else { $this->properties[$prop] = $property = \GameObject\Model\GameObjectPropertyModel::build(["object_id" => $this->object_id, "property" => $prop, "value" => $val]); $result = $property->save(); if (!$result) { print_r($property); throw new \Exception("Property " . $prop . " failed to create"); } } } } unset($attrs["properties"]); foreach ($attrs as $prop => $val) { if (self::$columns[$prop]) { $myProps[$prop] = $val; } else { throw new \Exception("Property " . $prop . " is not part of GameObjectModel. Please add to 'properties'"); } } if (count($myProps) > 0) { return parent::update($myProps); } return $this; }
public function setProperty($prop, $val) { // Look through existing properties foreach ($this->properties as $property) { if ($property->property === $prop) { $property->update(["value" => $val]); return; } } // No existing property $property = GameObjectPropertyModel::build(["object_id" => $this->object_id, "property" => $prop, "value" => $val]); $property->save(); }