mardi 23 février 2016

Catch mysql *warning* (not exception) while using Laravel DB Builder?

I have a BIGINT field, and for testing I maxxed out the field and then tried to add even more. The following code returns successful without throwing an \Exception, but it is throwing a warning in an sql query and, obviously, not updating the field. How can I catch a warning and return the warning message that the field is at its maximum? (As you can see I have tried both a DB::transaction and a try/catch without success).

        try {
            DB::transaction(function () use ($data, $quantity) {
                $query = 'INSERT INTO item_user (user_id, item_id, quantity) VALUES '.substr($data,0,-2).' ON DUPLICATE KEY UPDATE quantity = GREATEST(`quantity` + '.$quantity.',0)';
                DB::statement($query);

                // subquery to delete any rows left with zero
                DB::table('item_user')->where('quantity',0)->delete();
            });

            return redirect()->route('index')
                ->with( ['flash' => ['message' =>"<i class='fa fa-check-square-o fa-1x'></i> Success! Item totals updated.", 'level' =>  "success"] ] );

        } catch (\Exception $e) {

            return redirect()->back()->withErrors( $e->getMessage() )->withInput();
        }

This returns successful on the page but capturing and running the query directly on SQL gets me the warning and number :

Warning Code : 1264
Out of range value for column 'quantity' at row 1

Thanks.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire