CodeIgniter Forums
ResourceController inserts 0000-00-00 00:00:00 on empty string? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: ResourceController inserts 0000-00-00 00:00:00 on empty string? (/showthread.php?tid=81665)



ResourceController inserts 0000-00-00 00:00:00 on empty string? - blaasvaer - 04-05-2022

I pass query data to the default update method on a ResourceController, but it inserts 0000-00-00 00:00:00 into the timestamp instead of the default NULL the database is set to use.
Now, WHY does it do that, and HOW (apart from overriding the update method, rewriting it completely – which makes the whole point of the ResourceController obsolete), can I make it accept the empty string as a NULL value?

Or, to put it in another way: Why does CI 4 ResourceController default update method in a model set timestamp to: 0000-00-00 00:00:00 when the default is set to NULL and it receives an empty string?


RE: ResourceController inserts 0000-00-00 00:00:00 on empty string? - ignitedcms - 04-05-2022

Hmmm, try setting null to true in dbforge perhaps? Oh wait you're using CI4 my bad


RE: ResourceController inserts 0000-00-00 00:00:00 on empty string? - blaasvaer - 04-05-2022

Oh, of course, dumb me!

WTF is dbforge?

Did you actually read my post?


RE: ResourceController inserts 0000-00-00 00:00:00 on empty string? - iRedds - 04-05-2022

There is no ResourceController class in CI4.
Controllers do not work with the base.

Where is the problem area code?


RE: ResourceController inserts 0000-00-00 00:00:00 on empty string? - blaasvaer - 04-05-2022

(04-05-2022, 08:42 AM)Really?iRedds Wrote: There is no ResourceController class in CI4.
Controllers do not work with the base.

Where is the problem area code?
Check this out: https://codeigniter4.github.io/userguide/incoming/restful.html#resourcecontroller


RE: ResourceController inserts 0000-00-00 00:00:00 on empty string? - iRedds - 04-05-2022

I was looking for ResponseController instead of ResourceController. Ha ha I'm an idiot.
Yes, here I was wrong.

Honestly, I do not understand the meaning of this controller. Someone's bad joke?
The ResourceController requires methods to be implemented.
Where is your code?


RE: ResourceController inserts 0000-00-00 00:00:00 on empty string? - kenjis - 04-05-2022

It is MySQL thing, not CodeIgniter, not ResourceController.

An empty string ('') is not null.
If you want to insert null, convert the empty string to null in PHP.

Code:
create table test_datetime
(
    id    int unsigned auto_increment
        primary key,
    date_ datetime null
);

PHP Code:
<?php
namespace App\Controllers;
use 
CodeIgniter\Model;

class 
Home extends BaseController
{
    public function index()
    {
        $model = new class extends Model {
            protected $table 'test_datetime';
            protected $allowedFields = ['date_'];
        };

        $model->insert(['date_' => '']);
        $model->insert(['date_' => null]);

        var_dump($model->findAll());
    }


Code:
.../app/Controllers/Home.php:17:
array (size=2)
  0 =>
    array (size=2)
      'id' => string '1' (length=1)
      'date_' => string '0000-00-00 00:00:00' (length=19)
  1 =>
    array (size=2)
      'id' => string '2' (length=1)
      'date_' => null



RE: ResourceController inserts 0000-00-00 00:00:00 on empty string? - ignitedcms - 04-06-2022

Thank you Kenjis, nice explanation.