2

I am developing a website using the SilverStripe CMS. I am having a little problem with querying DataObjects. I am trying to get all the DataObjects in one go as follows to optimize performance:

DataObject::get();

When call this I get the following error:

[Emergency] Uncaught InvalidArgumentException: Call ::get() instead of DataObject::get()

If I could query it, I would like to do further operations like this:

DataObject::get()->filterAny([ 'ClassName' => 'ClassName' ]);

I have also tried to use the following query:

$sqlQuery = new SQLQuery();
$sqlQuery->setFrom('Player');

But the issue is that I do not know what is the name of the global / parent table name DataObject.

Is it possible to do what I am trying to do in SilverStripe and, if so, how?

scrowler
  • 24,273
  • 9
  • 60
  • 92
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372
  • try calling with Object name say for Gallery model $gallery = App\Model\Gallery::get() – thomas paulson Aug 21 '19 at 11:26
  • Hi @thomaspaulson, if I have to use the object names, I have to create multiple queries. Is there a way to query all the data objects from the one main table? – Wai Yan Hein Aug 21 '19 at 12:46

1 Answers1

2

We cannot call DataObject::get() to retrieve all DataObjects in one go. DataObject itself does not have a database table and therefore cannot be queried.

We cannot fetch all custom data objects in one query.

We can get all pages in one query by calling Sitetree::get().

If we want to be able to fetch all our own custom DataObjects in one query we would need to start by creating a BaseObject and having all our custom DataObjects extend BaseObject.

BaseObject

use SilverStripe\ORM\DataObject;

class BaseObject extends DataObject
{
}

Other custom classes

class ExampleObject extends BaseObject
{
}

class AnotherExampleObject extends BaseObject
{
}

We can then call BaseObject::get() to fetch all our custom objects and call SiteTree::get() to get all our pages.

Note, this will still not get any DataObjects defined in any installed modules, including the SilverStripe core. This includes Member, File, SiteConfig etc. These will always need to be retrieved with their own get() requests.

3dgoo
  • 15,716
  • 6
  • 46
  • 58