Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » update just one field of a DataObject

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

Page: 1
Go to End
Author Topic: 1142 Views
  • Stefdv
    Avatar
    Community Member
    110 Posts

    update just one field of a DataObject Link to this post

    Hello,

    I have a form where a Breeder can add a Dog to the Database. In the form there is a Dropdownfield where they can choose an other dog that would be the kid of the new dog. Like Newdog is FatherOf....

    Now i've come a long way ( in my opinion) but i have no idea how to update just one field in a record.

             $dogStep = $this->getSavedStepByClass('DogDetailStep');
                if($dogStep->loadData())
                {
             // Create the new Dog      
                   $dog = new Dog();
                   $dogStep->saveInto($dog);
                   $dog->BreederID    = $dog->BreederID;
                   $dog->write();
             // New Dog is in Database
             
             // I need the ID of the just created Dog
                   $thisdog = $dog->PedigreeName;
                   $newDog = DataObject::get_one('Dog', "PedigreeName = '$thisdog'");
             
             
                   $assignkidID = $dog->FatherOfID;
             //FatherOfID is a field from the form where i choose which Dog is the kid.
                   
                   $kid = DataObject::get_one('Dog', "ID = '$assignkidID'");
             //So now i have the kid that i choose in the form.

             //Now i have to fill the field $kid->MyFatherID with $newDog->ID
                   
             ??????????


    And that is where i'm lost. I've tryed

    $kid->write();

    but then it overwrites the whole dog with the new one, so i need to update just that one field MyFatherID.

  • Stefdv
    Avatar
    Community Member
    110 Posts

    Re: update just one field of a DataObject Link to this post

    Stupid me

    I have to set the field first, and then it doesn't mather that it writes everything back, it's the same dog........duh

    $kid->MyFatherID = $newDog->ID;
    $kid->write();

    1142 Views
Page: 1
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.