Welcome Guest, Not a member yet? Register   Sign In
ResourceController inserts 0000-00-00 00:00:00 on empty string?
#1

(This post was last modified: 04-05-2022, 02:51 AM by blaasvaer.)

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?
Reply
#2

(This post was last modified: 04-05-2022, 02:47 AM by ignitedcms.)

Hmmm, try setting null to true in dbforge perhaps? Oh wait you're using CI4 my bad
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply
#3

(This post was last modified: 04-05-2022, 02:47 AM by blaasvaer.)

Oh, of course, dumb me!

WTF is dbforge?

Did you actually read my post?
Reply
#4

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

Where is the problem area code?
Reply
#5

(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...controller
Reply
#6

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?
Reply
#7

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
Reply
#8

Thank you Kenjis, nice explanation.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
Reply




Theme © iAndrew 2016 - Forum software by © MyBB