Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » DataObject won't update

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 2198 Views
  • netnerd85
    Avatar
    Community Member
    42 Posts

    DataObject won't update Link to this post

    I'm trying to update the property of a DataObject and it's just not having it. The DataObject is a sub class of another class extending DataObject if that matters. The objects property updates fine but doesn't save to the database. Am I missing something?

    $results = DataObject::get("User", "SHA1(`Username`) = '{$activationKey}'", "", "", "1");
    if($results->TotalItems() == 1)
    {
       $results = $results->toArray();

       $results[0]->Activated = true;
       $results[0]->write();
    }

    [EDIT] The DataObject will update its own properties but not the parent objects, guess that makes sense but how can I update the parent objects properties?

  • Hypnus
    Avatar
    Community Member
    11 Posts

    Re: DataObject won't update Link to this post

    Hello,

    I have a similar problem in Silverstripe 2.4.3, i am using a custom independent Controller for a widget and from the post data i retrieve the values to update a newly created DataObject but that does not work. If i use the widgets Controller it can only update the DataObject by the $form->saveInto(CustomDataObject); and the write.

    This is my code segment:

          $data = $this->requestParams;
          
          if (isset($data) && is_array($data) && (count($data) > 0) ) {                  
             $subscriber = new CustomDataObject();

             $subscriber->FullName = "Test"; //hardcoded for test purposes only. It must be the $data['FullName']
             $subscriber->Email = "test@example.com"; //hardcoded for test purposes only. It must be the $data['Email']

             $subscriber->write();
          }

    It does write the object to the database but without updating the fields the FullName and Email. I also tried it with update($data) but that does not work either.

    Thanks

  • netnerd85
    Avatar
    Community Member
    42 Posts

    Re: DataObject won't update Link to this post

    Well I solved mine, I could have sworn I tried this but here's the new code...

    $results = DataObject::get("User", "SHA1(`Username`) = '{$activationKey}'", "", "", "1");
    if($results->TotalItems() == 1)
    {
    $results = $results->toArray();

    $results[0]->Activated = 'true'; //add single quotes around boolean true to make it a string
    $results[0]->write();
    }

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: DataObject won't update Link to this post

    Question as a aside

    $results = DataObject::get("User", "SHA1(`Username`) = '{$activationKey}'", "", "", "1");

    Any reason why you're not using get_one() which will get 1 object and you won't get it wrapped in a set (usually don't need a set for 1 object)

    $results = DataObject::get_one("User", "SHA1(`Username`) = '{$activationKey}'");
    if($results) {
    $results->Activated = 'true';
    $results->write();
    }

  • netnerd85
    Avatar
    Community Member
    42 Posts

    Re: DataObject won't update Link to this post

    "Any reason why you're not using get_one()", I'd really like to say yes but no, I didn't see get_one in the documentation, thank you very much for pointing it out! much appreciated

  • Hypnus
    Avatar
    Community Member
    11 Posts

    Re: DataObject won't update Link to this post

    Hi guys,

    Thanks Willr for the interest in the thread.

    Nice to see that your problem got solved, unfortunately mine did not. Anyway, I am curious what was the source of your problem, did you set the Activated property as string in you User data object (db property as 'Varchar' or 'Text') ?

    Regarding my problem, does anyone has any idea what could it be?
    Thanks

    P.S.: Good luck netnerd85 with your project.

  • Hypnus
    Avatar
    Community Member
    11 Posts

    Re: DataObject won't update Link to this post

    Hi again,

    Ok, I did got my problem solved.

    The issue was with the fact that I've added getters and setters to my CustomDataObject (actually the problem was the setters).

    This was my mistake, but I did not found anywhere any warning about this so why is it not OK?

    Thanks

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: DataObject won't update Link to this post

    did you set the Activated property as string in you User data object (db property as 'Varchar' or 'Text') ?

    If Activated is either true or false, Boolean would be a better type to use than Varchar.

    2198 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.