problem with my pdo syntax

problem with my pdo syntax

crush123crush123 Posts: 417Questions: 126Answers: 18

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

Answers

  • tangerinetangerine Posts: 3,350Questions: 37Answers: 394

    i get an error

    What is the error?

  • crush123crush123 Posts: 417Questions: 126Answers: 18

    SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ItemPrice' cannot be null

    so the default price is not being retrieved from $result

  • crush123crush123 Posts: 417Questions: 126Answers: 18
    edited August 2015

    Changed the syntax, and I think I have it...

    $result = $db->sql('SELECT refpricelookup.DefaultPrice FROM refpricelookup WHERE refpricelookup.ItemTypeID = '.$itemtypeid.' AND refpricelookup.SchoolID = '.$schoolid.' AND refpricelookup.QualityID = '.$qualityid.'');
                            $row = $result->fetch();
                            if ($row) {
                                $value = $row['DefaultPrice'];
                            } else {
                                $value = 0;
                            }
                        return $value;      
    
This discussion has been closed.