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

feat: add method to insert empty data in Model #6109

Merged
merged 7 commits into from Jun 19, 2022

Conversation

kenjis
Copy link
Member

@kenjis kenjis commented Jun 11, 2022

Description

  • add Model::allowEmptyInserts() to insert empty data

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis kenjis marked this pull request as draft June 11, 2022 00:22
@kenjis kenjis added enhancement PRs that improve existing functionalities 4.3 labels Jun 11, 2022
@kenjis kenjis force-pushed the feat-model-permit-insert-empty branch 4 times, most recently from c1ad5d5 to 9cfcfe3 Compare June 11, 2022 02:06
@kenjis
Copy link
Member Author

kenjis commented Jun 11, 2022

It seems OCI8 driver can't get the insert id.

1) CodeIgniter\Models\InsertModelTest::testInsertPermitInsertNoData
Failed asserting that a row in the table [insert_no_data] matches the attributes 
{
    "id": 0
}

Found: [].

/home/runner/work/CodeIgniter4/CodeIgniter4/system/Test/DatabaseTestTrait.php:280
/home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Models/InsertModelTest.php:220

https://github.com/codeigniter4/CodeIgniter4/runs/6840259577?check_suite_focus=true

@iRedds
Copy link
Collaborator

iRedds commented Jun 11, 2022

I saw on a forum or a github issue that someone came across the inability to use an empty request.
In my opinion, this is due to the problem of database design, since in my opinion the table for the test in PR does not make logical sense without links to other tables and containing only auto-increment and timestamps.

The solution is to use a raw query or add a tinyint(1) column with arbitrary data.
It seems to me that adding just such functionality is not rational.

@kenjis
Copy link
Member Author

kenjis commented Jun 11, 2022

In my opinion, this is due to the problem of database design, since in my opinion the table for the test in PR does not make logical sense without links to other tables and containing only auto-increment and timestamps.

It makes sense if it has links to other tables.
For example, imagine that is users table, and all other user's attributes are stored in other tables.

@iRedds
Copy link
Collaborator

iRedds commented Jun 11, 2022

I wrote about links with other tables.
But what's the point of creating an empty record in the dependent table.

You either add data or you don't.
I can't imagine a case where it makes sense to add a record without data.
Maybe I don't understand something?

@kenjis
Copy link
Member Author

kenjis commented Jun 11, 2022

It is not to add a record without data. The record will have ID.
But just insert data is empty.
If you add timestamps to columns by the field default value, the record will have timestamps, but the insert data is empty.

The point is sometimes we need this functionality to avoid "no data to insert" exception of Model
It may be because of bad design for tables, but we don't say it is always because of bad design.

@iRedds
Copy link
Collaborator

iRedds commented Jun 11, 2022

The table consisting of (id, created_at, updated_at) seems strange to me.

Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this feature. Some implementation suggestions and requests.

/**
* Whether to prohibit inserting empty data.
*/
protected bool $prohibitInsertEmpty = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider naming after the existing properties.

Suggested change
protected bool $prohibitInsertEmpty = true;
protected bool $allowEmptyInserts = false;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

system/BaseModel.php Outdated Show resolved Hide resolved
system/BaseModel.php Outdated Show resolved Hide resolved
system/Model.php Show resolved Hide resolved
@MGatner
Copy link
Member

MGatner commented Jun 11, 2022

The table could have any amount of data from default values. If a developer wants to let the database handle those, as it stands currently you need to submit some "dummy" data. I think our protections in general are good but I am a fan of the option to override those protections when needed.

Example based on kenjis:

TABLE users
PRIMARY KEY id
TIMESTAMPS created_at, updated_at
FIELD role DEFAULT 'user'
FIELD active DEFAULT 0

TABLE emails
PRIMARY KEY email
TIMESTAMPS created_at
FOREIGN KEY user_id

This fails:

$id = model(UserModel::class)->insertReturnId([]);
model(EmailModel::class)->insert([
    'user_id' => $id,
    'email' => 'iredds@example.com',
]);

@kenjis
Copy link
Member Author

kenjis commented Jun 12, 2022

@ytetsuro Can you debug this? It seems only Oracle can't get the insert id.
#6109 (comment)

@ytetsuro
Copy link
Contributor

ytetsuro commented Jun 12, 2022

@kenjis

Ok!
I will check it!

Copy link
Contributor

@ytetsuro ytetsuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kenjis

This test is lose the database error message.
You can’t understand why fail.😭

I fixed it.👍

#6113

system/Model.php Outdated Show resolved Hide resolved
@kenjis kenjis force-pushed the feat-model-permit-insert-empty branch from a58dc17 to 6729351 Compare June 12, 2022 08:46
@kenjis
Copy link
Member Author

kenjis commented Jun 12, 2022

@ytetsuro Thank you so much!
I was wondering if there was some bug in getting the insert id.

@MGatner
Copy link
Member

MGatner commented Jun 12, 2022

I would like to hear if @iRedds has any other thoughts before we move on from the discussion.

@kenjis
Copy link
Member Author

kenjis commented Jun 14, 2022

I think if we support insertion of empty data, Query Builder should support it.
The query is not unified.
See https://github.com/codeigniter4/CodeIgniter4/pull/6109/files#diff-1a690370bdbdc5997068f9ff210bacef314eeb65d1df73af206783ce055d8d1dR280

@kenjis kenjis marked this pull request as ready for review June 14, 2022 09:20
Copy link
Member

@MGatner MGatner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@MGatner
Copy link
Member

MGatner commented Jun 14, 2022

@kenjis Your diff link doesn't work for me on mobile so I'm not sure what the issue is in Query Builder, but I am fine with adding a parallel feature there.

@kenjis
Copy link
Member Author

kenjis commented Jun 14, 2022

@MGatner Sorry, I didn't explain what the issue is in Query Builder.
The link just shows there are three types of queries to insert no data.

Query Builder also prohibit inserting no data.
$builder->insert([]) does not work, an exception is thrown.

@kenjis kenjis added the database Issues or pull requests that affect the database layer label Jun 15, 2022
@kenjis kenjis force-pushed the feat-model-permit-insert-empty branch from 6729351 to 6ab4d62 Compare June 15, 2022 07:20
@kenjis
Copy link
Member Author

kenjis commented Jun 15, 2022

Rebased and added the documentation.

@iRedds Any comments?

@MGatner
Copy link
Member

MGatner commented Jun 15, 2022

My only thought on the docs is maybe to mention an example of why one might want do this.

kenjis and others added 7 commits June 20, 2022 05:55
ErrorException : mb_strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated
.../CodeIgniter4/system/Validation/FileRules.php:138
This is an important config change.
Now allowEmptyInserts() is a normal setter.
Co-authored-by: ytetsuro <phper.0o0@gmail.com>
@kenjis kenjis force-pushed the feat-model-permit-insert-empty branch from 6ab4d62 to a48cba9 Compare June 19, 2022 20:57
@kenjis
Copy link
Member Author

kenjis commented Jun 19, 2022

Resolved conflict.

@kenjis kenjis merged commit 9974626 into codeigniter4:4.3 Jun 19, 2022
@kenjis kenjis deleted the feat-model-permit-insert-empty branch June 19, 2022 21:22
@kenjis
Copy link
Member Author

kenjis commented Jun 19, 2022

Sorry, I made a mistake when I rebased.

I mistakenly rebased with develop first and then rebased with 4.3.
So this PR contains commits from develop that have not yet been merged into 4.3.

@MGatner
Copy link
Member

MGatner commented Jun 20, 2022

Shouldn't be a problem, develop => 4.3 should handle that after next patch release.

@kenjis
Copy link
Member Author

kenjis commented Jun 21, 2022

Okay, thanks.
Since revert does not make much sense, leave it as it is.

develop => 4.3 should handle that after next patch release.

No, it is daily job.
I generally do it once a day.

If we do not merge, bugs will remain unfixed in 4.3, and conflicts are likely to occur as the difference between develop and 4.3 grows. So it is better to merge continuously.

@MGatner
Copy link
Member

MGatner commented Jun 22, 2022

If that becomes tiresome we could set up a GitHub Action to merge to 4.3 anytime develop is updated. I'd be willing to look into that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.3 database Issues or pull requests that affect the database layer enhancement PRs that improve existing functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants