Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

SaveAttribute in Model

SS ✭✭
edited January 2010 in Vanilla 2.0 - 2.8
Developing apps by using Garden (Vanilla) I noticed that very often I need field "Attributes" in table structure and of cousre need method like SaveAttribute (so I copy-pasted SaveAttribute() code from UserModel with a little changes).

Maybe is reasonable to add SaveAttribute() method to Gdn_Model class?

It will look like:
public function SaveAttribute($RowID, $Name, $Value = '') { if (!isset($this->Schema)) $this->DefineSchema(); // TODO: need make sure that $RowID is not multiple primary key $FieldName = $this->PrimaryKey; $Row = $this->SQL ->Select('Attributes') ->From($this->Name) ->Where($FieldName, $RowID) ->Get() ->FirstRow(); if(!$Row) throw new Exception(Gdn::Translate('ErrorRecordNotFound')); $Values = Format::Unserialize($Row->Attributes); if(!is_array($Values)) $Values = array(); if(!is_array($Name)) $Name = array($Name => $Value); $Values = Format::Serialize(array_merge($Values, $Name)); return $this->SQL ->From($this->Name) ->Set('Attributes', $Values) ->Where($FieldName, $ID) ->Put(); }


  • SS ✭✭
    Damn... non-linebreaked < code > tag
  • MarkMark Vanilla Staff
    If you want to make that change and do a pull request on GitHub, I'll add it to the core. It's a great idea!
Sign In or Register to comment.