Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: model->save() insert/update data #3177

Closed
fujael opened this issue Jun 28, 2020 · 2 comments
Closed

Bug: model->save() insert/update data #3177

fujael opened this issue Jun 28, 2020 · 2 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@fujael
Copy link

fujael commented Jun 28, 2020

Direction
When we use model->save($data) there is no insert/update if user provides a primaryKey with its value in dataset.

Describe the bug
Let's consider this object of data to insert using save()
->save({ID: 1, name: 'A name'})
If the database table already has a row exists with primary key ID the operation will work smoothly.
But if the row doesn't exist then this function is not functional at all.
I guess this is a lack of logic to check a row before insert/update. No matter if provided data has value with primary key or not.

CodeIgniter 4 version
4.0.3 stable

Affected module(s)
All model classes

Expected behavior, and steps to reproduce if appropriate
We can update the function code like that-

` public function save($data): bool {
if (empty($data))
{
return true;
}
$builder = $this->builder();

    if (is_object($data) && isset($data->{$this->primaryKey}) && $builder->getWhere([$this->primaryKey => $data->{$this->primaryKey}])->getRow())
    {
        $response = $this->update($data->{$this->primaryKey}, $data);
    }
    elseif (is_array($data) && ! empty($data[$this->primaryKey]) && $builder->getWhere([$this->primaryKey => $data[$this->primaryKey] ])->getRow())
    {
        $response = $this->update($data[$this->primaryKey], $data);
    }
    else
    {
            $response = $this->insert($data, false);
            // call insert directly if you want the ID or the record object
            if ($response !== false)
            {
                    $response = true;
            }
    }
    return $response;
}`

Context

  • OS: [Windows 10]
  • Web server [CodeIgniter development server]
  • PHP version [7.4.5]
@fujael fujael added the bug Verified issues on the current code behavior or pull requests that will fix them label Jun 28, 2020
@fujael fujael changed the title Bug: Bug: model->save() insert/update data Jun 28, 2020
@michalsn
Copy link
Member

Sorry, but it's not a bug. Right now, the Model class is designed to support only auto_increment primary key.

Feel free to send a PR with enhancements.

@bhojkamal
Copy link

What is the differenct between $model->save($data) and $model->insert($data) for saving new data?
Please explain.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

3 participants