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

How to Declare Instance of DataObject?


Go to End


2 Posts   1447 Views

Avatar
SilverstripeNewbie

Community Member, 14 Posts

11 October 2015 at 8:54am

Edited: 11/10/2015 9:25am

Hi, do I declare and instance of DataObject correctly? I tried this to delete a first found record but did not work,

$product = Product; or $product = Product;::create();
$product->get();
$product->delete();echo $product->ID;
$product->write();

I think the error is on delete.

Avatar
martimiz

Forum Moderator, 1391 Posts

24 October 2015 at 9:54am

Edited: 24/10/2015 9:55am

instantiating a new Product object would be someting like this:

$product = new Product();
Or
$product = Product::create();

To get the first of a set of Products you could:

$product = Product::get()->first();

you could possibly even:

Product::get()->first()->delete();

A good starting point would be the docs, specifically here:
https://docs.silverstripe.org/en/3.1/developer_guides/model/data_model_and_orm/