SIlverstripeNewbie
SIlverstripeNewbie

Reputation: 291

Calling delete() causes server error

Why is my delete() causing server error in Silverstripe? Below is the code:

$product = Product::create();
$product = Product::get()->filter(array('Price' => 26.32));
$product->delete();     

The above is in requireDefaultRecords() and run when /dev/build?flush

Upvotes: 1

Views: 88

Answers (1)

3dgoo
3dgoo

Reputation: 15794

Product::get()->filter(array('Price' => 26.32)) will return a DataList, not a Product object. This is because Product::get()->filter() could find more than one product. This will still return a DataList even if the filter function only finds one item.

What you need to do is go through each item in the DataList and delete each one.

$newProduct = Product::create();
$products = Product::get()->filter(array('Price' => 26.32));

foreach ($products as $product) {
    $product->delete(); 
}

Upvotes: 7

Related Questions