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: insertBatch not generating createdField #3838

Closed
wiedhodho opened this issue Oct 30, 2020 · 14 comments
Closed

Bug: insertBatch not generating createdField #3838

wiedhodho opened this issue Oct 30, 2020 · 14 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@wiedhodho
Copy link

when I use insertBatch, the field createdField not generating current date/timestamp

database: mariadb
feature: insertbatch

@wiedhodho wiedhodho added the bug Verified issues on the current code behavior or pull requests that will fix them label Oct 30, 2020
@paulbalandan
Copy link
Member

paulbalandan commented Oct 30, 2020

Did you set the $useTimestamps to true?

@wiedhodho
Copy link
Author

Yes, I have set $useTimestamps to true, but it still null on database.

@paulbalandan
Copy link
Member

Please provide your Model code where you are doing the insertBatch.

@paulbalandan
Copy link
Member

Please confirm if you set the following correctly:

  1. $useTimestamps is set to true
  2. the $createdField is included in the array of allowedFields
  3. the $createdField is set in the database

@wiedhodho wiedhodho reopened this Oct 30, 2020
@wiedhodho
Copy link
Author

this is my code:

`protected $table = 'data_miskin';
protected $primaryKey = 'data_miskin_id';

protected $returnType = 'object';
protected $useSoftDeletes = true;

protected $allowedFields = [
	'data_miskin_penerima',
	'data_miskin_kategori',
	'data_miskin_created',
	'data_miskin_user',
	'data_miskin_program',
];

protected $useTimestamps = true;
protected $createdField = 'data_miskin_created';
protected $updatedField = 'data_miskin_lastupdate';
protected $deletedField = 'data_miskin_deleted';

protected $validationRules = [
	'data_miskin_penerima' => 'required',
	'data_miskin_kategori' => 'required',
	'data_miskin_user' => 'required',
	'data_miskin_program' => 'required',
];`

@paulbalandan
Copy link
Member

then, the code how are you inserting to the db?

@wiedhodho
Copy link
Author

wiedhodho commented Oct 30, 2020

this is the code to insert to the db

foreach ($json->pilihan as $p) {
				$data[] = [
					'data_miskin_penerima' => $p,
					'data_miskin_kategori' => $json->kategori,
					'data_miskin_user' => 'guest',
					'data_miskin_program' => $json->id,
				];
			}
			$r = $this->model->insertBatch($data);

@paulbalandan
Copy link
Member

Reading my emails, I've seen you've originally forgotten to include the created field in the allowed fields. Is your code still doing null inserts after inclusion?

@wiedhodho
Copy link
Author

at the first code, I have forgot to include createdfield in the allowed field. Then I add the createdfield in the allowed fields, but it still same, the code not generated the timestamp/date in the createdfield with $db->insertBatch();

@MGatner
Copy link
Member

MGatner commented Nov 6, 2020

Created and updated fields do not need to be in allowedFields

@jahmedk
Copy link

jahmedk commented Nov 11, 2020

@wiedhodho which data type are you using for
'data_miskin_created';
'data_miskin_lastupdate';
'data_miskin_deleted';
in table??
I think if you use a data type that is not able to save the data in this date-time or time-stamp format, then this problem can also arise.

@wiedhodho
Copy link
Author

I use timestamp data type.
When I use $db->save, the createdfield automatic filled by current time. But not when use inserbatch

@MGatner
Copy link
Member

MGatner commented Nov 12, 2020

We did not have any framework tests to confirm this so I added a few:
#3879

@michalsn
Copy link
Member

michalsn commented Jan 3, 2021

It's working just fine, my guess is that you're using v4.0.4, and changes that add support for $useTimestamps in the insertBatch() method was introduced only in the develop branch.

@michalsn michalsn closed this as completed Jan 3, 2021
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

5 participants