Why is this ArrayList Not Updating?

Silverstripe Version:4.4.x-dev

Question: Why is this ArrayList not updating?

In my Page class I have an ArrayList variable results. In my template, the function updateResults is called multiple times in a loop. I also have a function getResults that just returns the results. Each time updateResults is called, a new ArrayData object is pushed to the ArrayList.

Here’s the outline of my Page.php

class Page extends SiteTree {
    ...
    private $results;
   
    public function __construct($record = null, $isSingleton = false, $queryParams = array()) {
        $this->results = new ArrayList();
	parent::__construct($record, $isSingleton, $queryParams);
    }
    
    public function updateResults($title, $content) {
        $this->results->push(new ArrayData(['title' => $title, 'content' => $content]));
    }

   public function getResults() {
       return $this->results;
   }
}

If I were to add foreach ($this->searchResults as $s) {echo $s->Title;} to getResults it would print nothing.

What can I do?

By changing $this->results = new ArrayList(); to $this->results = ArrayList::singleton(), I got it work. Though, I am not sure this is the right way to do this… Would appreciate feedback.

I would

a. not use __construct for page, too confusing and easy to break things
b. use MyClass::create(); e.g. ArrayList::create();
c. not use the template to “do” things as the templates are cached (and values from methods are).

Could you take a step back and provide a bit more detail about what you are trying to do? templates should be concerned with displaying innformation. Using a template to update results sounds like an anti-pattern.