Welcome Guest, Not a member yet? Register   Sign In
insert without data
#1

(This post was last modified: 06-05-2022, 04:30 AM by webdeveloper.)

Hi guys,
i'm facing one "problem" with inserting new data into table.

PHP Code:
$this->model->insert([]) 

for me expecting query would be:

Code:
INSERT INTO gallery VALUES();

which is fine and works well - inserts one new row into defined table

instead of this, codeigniter returns me an error 'no data for insert'

table structure:

Code:
gallery_id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT
created_at DATETIME DEFAULT CURRENT_TIMESTAMP()
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP()

I really don't need any data to insert.

Looking for solution how to override this behaviour without any hack and with query builder.

Wouldn't be great to check in query builder, if param $data of function insert($data = null, ...) isn't null but can be empty array?

vendor/codeigniter4/framework/system/Model.php

PHP Code:
/**
    * Inserts data into the database. If an object is provided,
    * it will attempt to convert it to an array.
    *
    * @param array|object|null $data
    * @param bool              $returnID Whether insert ID should be returned or not.
    *
    * @throws ReflectionException
    *
    * @return BaseResult|false|int|object|string
    */
    public function insert($data nullbool $returnID true)
    {
        if (! empty($this->tempData['data'])) {
            if (empty($data)) {
                $data $this->tempData['data'] ?? null;
            } else {
                $data $this->transformDataToArray($data'insert');
                $data array_merge($this->tempData['data'], $data);
            }
        }

        $this->escape  $this->tempData['escape'] ?? [];
        $this->tempData = [];

        return parent::insert($data$returnID);
    
Reply


Messages In This Thread
insert without data - by webdeveloper - 06-05-2022, 04:26 AM
RE: insert without data - by kilishan - 06-05-2022, 08:56 PM
RE: insert without data - by webdeveloper - 06-07-2022, 03:12 AM
RE: insert without data - by kilishan - 06-07-2022, 06:42 AM
RE: insert without data - by webdeveloper - 06-08-2022, 05:44 AM
RE: insert without data - by kenjis - 06-10-2022, 04:03 PM



Theme © iAndrew 2016 - Forum software by © MyBB