0

I want to update the Content string within the object. With the first foreach I get this:

object(Page)#1716 (20) {
        ["_cache_statusFlags":protected]=> NULL
        ["creatableChildrenCache":protected]=> NULL
        ["destroyed"]=> bool(false)
        ["record":protected]=> array(22) {
          ["ClassName"]=> string(4) "Page"
          ["ID"]=> int(1)
          ["ParentID"]=> int(0)
          ["Title"]=> string(5) "Home"
          ["MenuTitle"]=> NULL
          ["URLSegment"]=> string(4) "home"
          ["Content"]=> string(2210) "
My content that needs to be updated
"...

I tried with json_decode, (array) but I either get "Uncaught Error: Cannot use object of type Page as array" or no error and no update.

// json_decode($results,true);
foreach ($results as $result) {
   json_decode($result,true); // seem like it does nothing
   // tried (array)$result['record']['Content'], (array)$result['Content'],...
   // $result['Content'] = str_replace($keywords,'<span class="highlight-search">' .$keywords . '</span>',$result->Content);
   $result['record']['Content'] = str_replace($keywords,'<span class="highlight-search">' .$keywords . '</span>',$result->Content);
}

I also tried with foreach($result as $key => $field)...

At this point I'm just guessing. What is the proper procedure?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
user805528
  • 189
  • 1
  • 5
  • 17
  • Its an object, not a json string, but it an object with protected properties – RiggsFolly Oct 26 '22 at 09:34
  • `json_decode($result,true); // seem like it does nothing`...well 1) $result seemingly isn't a JSON string, and 2) even if it was, you're not capturing the output of it, so of course you won't see anything useful from it. Try [reading the manual](https://www.php.net/manual/en/function.json-decode.php) – ADyson Oct 26 '22 at 09:36
  • `With the first foreach I get this`...what actual line of code outputs that? I don't see anything in your code which would output anything. – ADyson Oct 26 '22 at 09:39
  • This is not actually a duplicate - the question which was linked to ignores the fact that Silverstripe has some magic going on for its db fields. You can just go `$this->Content = '

    This is my content

    ';` to set the Content, or `$myContent = $this->Content;` to get the content. Obviously replace `$this` with the variable holding the `DataObject` if you're accessing it externally.
    – Guy Sartorelli Oct 26 '22 at 21:38

0 Answers0