Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

[User Error] Uncaught Exception: Object->__call(): the method 'dosomething' does not exist on 'MyPage_Controller'


Go to End


2 Posts   3348 Views

Avatar
dalailomo

Community Member, 1 Post

27 March 2015 at 9:03pm

Edited: 27/03/2015 9:08pm

Hi everyone!

I have a quite strange issue which I cannot solve unfortunately, I will try to explain it as clear as possible.

We are migrating a older version of silverstripe, 2.4 to 3.1 exactly.

For testing this issue, I also have another fresh install of silverstripe 3.1. Let's call them Site A, and Site B.

Site A (already updated from silverstripe 2.4):
File /mysite/code/MyPage.php

class MyPage extends Page { 
  // ...
  public function myFunction() { die('hey yo!'); }
  // ...
}
 class MyPage_Controller extends Page_Controller {
  // ...
  public function doSomething() {
    $this->myFunction(); // the variable $this, should access the MyPage model, right? With some kind of magic related with Object::__call() magic method... and so on
  }
  // ...
}

File /mysite/tests/MyPageTest.php

class MyPageControllerTest extends FunctionalTest {
  protected $_page;
  
  public function setUp() {
    parent::setUp();
    $this->_page = new MyPage_Controller;
  }

  public function testDoSomething() {
    $this->_page->doSomething(); // it's not really a test, I know, I m just executing it to illustrate the issue
  }
}

Navigating to the corresponding url, it works, and I can see a blank page with my message 'hey yo!', Happy ending!
Navigating to /dev/test/MyPageControllerTest, it does not work, I m getting the error
[User Error] Uncaught Exception: Object->__call(): the method 'dosomething' does not exist on 'MyPage_Controller'

Site B (just a fresh from scratch silverstripe install):
Exaclty the same code as above:

Navigating to the corresponding url, it does not work, I m getting the error
[User Error] Uncaught Exception: Object->__call(): the method 'dosomething' does not exist on 'MyPage_Controller'
Navigating to /dev/test/MyPageControllerTest, it does not work, I m getting the error
[User Error] Uncaught Exception: Object->__call(): the method 'dosomething' does not exist on 'MyPage_Controller'

What am I missing? :(

I hope I explained myself clearly enough.

Thanks!

Avatar
kinglozzer

Community Member, 187 Posts

28 March 2015 at 1:43am

Edited: 28/03/2015 1:46am

Hi dalailomo,

Usually, when you call $this->myFunction(), it will check if that method exists on the controller (which it doesn't) then fall back to the data record that is associated with that controller via some __call() magic. In this case, as you've instantiated the controller manually, there is no associated data record given, so the data record is just an instance of Page (which doesn’t have that method).

You can set it manually in your setUp() method, something like this should work:

public function setUp() {
    parent::setUp();
    $this->_page = new MyPage_Controller;
    $this->_page->dataRecord = new MyPage;
  }

or even:

public function setUp() {
    parent::setUp();
    $this->_page = new MyPage_Controller(new MyPage);
  }

Hope this helps,
Loz