Cakephp 4: Populate dropdowns from model method

I have a method in my controller called getLists() that grabs information for thirteen dropdown lists. Each list is populated with a $this->Model->Association->find() call with various conditions.

I really, really don’t want this giant method in my controller, so I moved it to my model. Question: How do I return the list variables to my controller?

If I call the method from my controller like this:

$this->Model->getLists($variable1, $variable2);

And the method is set up like this in my model:

public function getLists($var1 = null, $var2 = null)
{
    $projects = $this->Projects->find('list', [etc]);
    $systems = $this->Systems->find('list', [etc]);
    
    return <both of the above results>
 }

What do I return to the controller? I tried an array:

return [$projects, $systems];

But that didn’t work. How do I get the values back to my controller and set them for use in my views?

UPDATE: I figured it out. Controller needs the list() function to precede the method call:

list($projects, $systems) = $this->Model->getLists($var1, $var2);
$this->set(compact('projects', 'systems'));
1 Like