public function update() {
		$t = new Template;
		$t->name = $this->getTemplateName();

		$fg = new Fieldgroup;
		$fg->name =  $this->getTemplateName();
		foreach($this->fields as $field) { // add global fields
			if(!($field->flags & Field::flagGlobal)) continue;
			$fg->add($field);
		}
		$fg->save();
		$t->fieldgroup = $fg;

		$t->fieldgroup->save();
		$t->save();

		$this->templateSetup($t);

		$t->fieldgroup->save();
		$t->save();

		return $t;
	}