Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Insert null into int field but got 0.
  • my sql table

    CREATE TABLE IF NOT EXISTS `ws_cash` (
      `cash_id` int(11) NOT NULL AUTO_INCREMENT,
      `account_id` int(11) DEFAULT NULL COMMENT 'refer to accounts.account_id of employee who do this.',
      `order_id` int(11) DEFAULT NULL COMMENT 'refer to esm_orders.order_id',
      `cash_in` decimal(20,9) NOT NULL DEFAULT '0.000000000' COMMENT 'cash in. as absolute number',
      `cash_out` decimal(20,9) NOT NULL DEFAULT '0.000000000' COMMENT 'cash out. as absolute number',
      `cash_date` bigint(20) DEFAULT NULL COMMENT 'cash record date.',
      PRIMARY KEY (`cash_id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT='contain cash in or out records.' AUTO_INCREMENT=1 ;


    fuelphp code
    $data['account_id'] = $this->getMyAccountId();
                    $data['order_id'] = trim(\Input::post('order_id'));
                    if ($data['order_id'] == null || $data['order_id'] == '0') {unset($data['order_id']);}
                    $data['cash_in'] = trim(\Input::post('cash_in'));
                    $data['cash_out'] = trim(\Input::post('cash_out'));
                    $data['cash_date'] = time();
                   
                    list($cash_id) = \DB::insert('cash')
                        ->set($data)
                        ->execute();


    the field order_id should be null when it is not set. but it is become 0.
    Please help.
  • HarroHarro
    Accepted Answer
    DB:insert() doesn't invent values.

    Start by dumping the generated insert query after the insert using

    echo \Debug::dump(\DB::last_query());

    Is the order_id column present? And what value is it set to? Is it in $data?
  • when i set the number to order_id for example order_id = 5.
    It can insert 5 into field order_id correctly.
    but when i set order_id = null or unset it, it should be null but it is not. It is becomes 0.


  • Did you do what I asked?

    if you don't pass a column and a value to insert(), it will not be part of the INSERT statement. So it's either your code, or your DB engine. And you're not going to find out which if you don't debug.
  • I found it. The javascript send data value 'null' (I wrote it to send null not 'null' that is string).

    Thank you @Harro Verton.
  • Ok, cool you found the problem.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion