SS4: Unable to access a (DataObject) class in a controller

**Silverstripe Version:4

Hello,
I am trying to access the (DataObject) class Offerer.php in my controller TestController.php .
The controller works fine.

<?php

use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\ORM\DataObject;

class TestController extends PageController {

 private static $allowed_actions = [
		'light',
                // ...
    ];

public function light() {

      $offerer =  new Offerer();
// ...
}

}

The location for both TestController.php and Offerer.php are located in

Details of your query go here

The locations of the files being the same may not necessarily mean they are in the same namespace

Try importing the Offerer class; also it is “best practice” to use Offerrer::create() syntax due to injection

I actually posted the question three times and specifically stated the various ways I tried to instantiate the DataObject class Offerer:

public function light() {

  $offerer = new Offerer();
  /* $offerer = Offerer::create(); */ 

}

The exact error message: “Error message: [Alert] Class ‘Offerer’ not found GET /silver/test/light Line 22 in C:\xampp\htdocs\silver\mysite\code\TestController.php”

I used as reference the following official documentation:
https://docs.silverstripe.org/en/4/developer_guides/model/data_model_and_orm/

Can you post the code for the Offerer() class as well? It might help track down the issue.

Thank you.

https://paste.ofcode.org/y9vEWkfh3EiEba5ZYcJDrd

Does it work if you try: $offerer = \Offerer::create(); ?

(You will find below a screenshot of the error. Thank you.)

Sorry if these are basic questions, but did you run a dev/build after you added your DataObject? Is there a corresponding table in the database?

No worries, I really appreciate your help. (I apologize but I am a newbie)
I think you nailed it because even though I have run http://localhost/silver/dev/build and http://localhost/silver/dev/build?flush numerous times, I do not see the table structure being created. Am I missing something ?

Can you just confirm the location of the file containing the Offerer() class. Is it in /mysite/code?

One thing I noticed is that the class is using a short opening tag <? instead of the full <?php tag. Might be worth changing that in case your system doesn’t have short tags enabled.

Offerer() is indeed in /mysite/code and changing the opening tag to <?php gives the same error.

Did you run another dev/build after you changed the tag?

I hadn’t. The table has been created and the template renders the data.
I thank you wholeheartedly.
Any future tips ?

1 Like

Glad it was something simple!

There are a few hints and tips in the Tips & Tricks - Silverstripe Forum section on here which might be worth a look.

The ‘Lessons’ area https://www.silverstripe.org/learn/lessons/v4/ is also a good place for hints too, even for an experience developer.

Hello,
I seem to be getting a problem somewhat related.
When I try to instantiate other DataObject classes (all located in /mysite/code) in the same function light() { }, I always keep getting the same fatal error (see screenshot below) which always refers to the same DataObject class Apartment.php (dev/build regularly used). Thanks.

Have you checked the syntax of the file in question? Can you post the code for it?

I am bit lost when it comes to figuring out the error message because regardless which one of my four DataObject PHP Objects I instantiate in the light() {} method , the error message above in red always refers to Apartment.php (which is one of the DataObject PHP Objects). Here is the file:
https://paste.ofcode.org/mw6x6Mjmrgv4xQTJ6a75rb

You’re missing a semi-colon after the second use statement.

The error message pretty much tells you that :slight_smile:

"unexpected T_CLASS, expecting ‘;’

  1. I added it.

(in TestController.php)

public function light() {

	$apartment = Apartment::create();
	var_dump($apartment);

}

  1. dev/build?flush

  2. calling http://localhost/silver/dev/build/

Error message: " … " 3 C:\xampp\htdocs\silver\vendor\silverstripe\framework\src\Core\Manifest\ClassManifest.php(448): SilverStripe\Core\Manifest\ClassManifest->handleFile(‘HomeController…’, ‘C:\xampp\htdocs…’, false)

  1. when I call http://localhost/silver/test/light, I get

[Alert] Class ‘Apartment’ not found

What notation worked for the other dataobject? Was it with the leading slash ie. \Offerer::create();

If so, then you’ll probably need that here as well. You’re creating all your code in the global namespace by the look of things, so you may need the leading backslashes to be able to reference the class.