0
votes

I have following code in Silverstripe

<?php

$product= Product::get()->filter("ProductCode", $UniqueCode)->First();
if($product){
  $product->Stock = "250";
  $product->Name = "Abcd 123";
  $product->write();
  echo "New Stock = ".$product->Stock; //this prints the OLD value not the NEW one. Nor database is updated.
}
?>

Update: If I do $product->Name = "Abcd 123";, the Name' field is getting updated, but not theStock`

This didn't work. The Stock field of Product table is not updated. Can anybody tell me where I went wrong?

1
pleas provide more details - place a debug::dump($product); or even debug::show($UniqueCode); into your code to analyze. - munomono
You're completly sure you get a $product from the query? Try a var_dump($product) or echo $product->ID after the query, or use a proper tool like xdebug to debug your code. - wmk
@wmk, yes the $product returns the record - WatsMyName
@munomono, $product returns the record - WatsMyName
@munomono, actually I was sending String value to Integer field, Stock is integer field. I have fixed this issue, thanks - WatsMyName

1 Answers

0
votes

I have fixed the problem. For someone who step up this thread with same issue in future.

Actually Stock field is integer and i was supplying string value. You need to convert string into integer before supplying to object. For example:

$updatedvalue = "250";
$updatedvalue = (int)$updatedvalue;