problem with my pdo syntax
problem with my pdo syntax
i have a price lookup table which I am using with ->setValue(function () use ($db) {} to insert a default value into my products table.
This works when the lookup table has a value, but will otherwise throw an error.
I am trying to trap the error, so that a price of 0.00 is set if no rows are returned, but it is not working.
$editor->field(
Field::inst('tblitem.ItemPrice')
->setValue(function () use ($db) {
//retrieve values from editor
$itemtypeid = $_POST['data']['tblitem']['ItemTypeID'];
$schoolid = $_POST['data']['tblitem']['ItemSchoolID'];
$qualityid = $_POST['data']['tblitem']['ItemQualityID'];
$result = $db->sql('SELECT refpricelookup.DefaultPrice FROM refpricelookup WHERE refpricelookup.ItemTypeID = '.$itemtypeid.' AND refpricelookup.SchoolID = '.$schoolid.' AND refpricelookup.QualityID = '.$qualityid.'');
//if ($result->count() === 0) {
// $value = 0;
//} else {
$row = $result->fetch();
$value = $row['DefaultPrice'];
//}
return $value;
} )
);
i have commented out the error in the above snippet and it works, but if I use the if block, i get an error
This discussion has been closed.
Answers
What is the error?
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ItemPrice' cannot be null
so the default price is not being retrieved from $result
Changed the syntax, and I think I have it...