-
nneves
Junior Member
-
Posts: 14
Threads: 6
Joined: Jun 2020
Reputation:
0
08-18-2021, 04:50 PM
(This post was last modified: 08-19-2021, 12:42 AM by nneves.)
Hi
In my model I have this settings and when I insert a record, both created_at and updated_at are updated
Code: protected $table = 'strings';
protected $useSoftDeletes = true;
protected $allowedFields = ['code', 'string', 'views'];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
When I run an update, the updated_at is not updated
Code: $db = \Config\Database::connect();
$builder = $db->table('strings');
$builder->set('views', 'views+1', false);
$builder->where('code', $a);
$builder->where('string', $b);
$builder->update();
Am I doing something wrong?
Thanks
-
InsiteFX
Super Moderator
-
Posts: 6,575
Threads: 331
Joined: Oct 2014
Reputation:
240
Did you add this to your model?
PHP Code: /** * The type of column that created_at and updated_at * are expected to. * * Allowed: 'datetime', 'date', 'int' * * @var string */ protected $dateFormat = 'datetime';
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
-
nneves
Junior Member
-
Posts: 14
Threads: 6
Joined: Jun 2020
Reputation:
0
08-19-2021, 02:21 AM
(This post was last modified: 08-19-2021, 02:27 AM by nneves.)
Hi
No, but does not change the behavior.
In debug mode I can see that the insert query adds both created and updated dates, but when updating, the updated_at filed is not added.
I've tested executing the query manually and using the builder.
-
ikesela
Member
-
Posts: 155
Threads: 0
Joined: Nov 2020
Reputation:
7
08-19-2021, 04:12 AM
(This post was last modified: 08-19-2021, 04:14 AM by ikesela.)
this the problem, when you set to false, not working with updated_at (for auto update datetime).
$builder->set('views', 'views+1', false);
* this maybe bug
-
nfaiz
Member
-
Posts: 59
Threads: 10
Joined: Apr 2015
Reputation:
3
(08-18-2021, 04:50 PM)nneves Wrote: Hi
In my model I have this settings and when I insert a record, both created_at and updated_at are updated
Code: protected $table = 'strings';
protected $useSoftDeletes = true;
protected $allowedFields = ['code', 'string', 'views'];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
When I run an update, the updated_at is not updated
Code: $db = \Config\Database::connect();
$builder = $db->table('strings');
$builder->set('views', 'views+1', false);
$builder->where('code', $a);
$builder->where('string', $b);
$builder->update();
Am I doing something wrong?
Thanks
because you creating another builder?
-
nneves
Junior Member
-
Posts: 14
Threads: 6
Joined: Jun 2020
Reputation:
0
(08-19-2021, 04:12 AM)ikesela Wrote: this the problem, when you set to false, not working with updated_at (for auto update datetime).
$builder->set('views', 'views+1', false);
* this maybe bug
I've tested either with false, true or without any value and the updated_at field is not updated
(08-19-2021, 04:59 AM)nfaiz Wrote: because you creating another builder?
I can't use save/update because my primary key is composed with two fields.
I've tried too with $db->query('UPDATE string SET views = views + 1 ... and doesn't work either ...
-
wdeda
Member
-
Posts: 135
Threads: 5
Joined: Dec 2014
Reputation:
1
08-19-2021, 10:28 AM
(This post was last modified: 08-19-2021, 10:38 AM by wdeda.
Edit Reason: Justification for two Models.
)
The update of created_at and/or updated_at is completely random, both in option 1 and option 2, I gave up on understanding why sometimes it happens and sometimes it doesn't, and when it happens, according to my point of view, it's kind of meaningless, this is, updates created_at and update_at simultaneously with the same data. I think update_at should only be updated when an update actually occurs.
The solution I found was both in creating new tables and in existing tables using the following settings:
Code: created_at DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
update_at DATETIME on update CURRENT_TIMESTAMP NULL DEFAULT NULL
@ craig I don't agree. I use "two types" of Model:
1 - for queries, query builder; I don't use find or find all;
PHP Code: <?php
// 2021015
namespace App\Models;
use CodeIgniter\Model;
class RecordStylesModel extends Model {
public function styles($id) { $db = $this->db; $query = $db->table('record_styles') ->where('record_id', $id) ->get();
return $query->getResult();
}
public function numstyles($id) {
$db = $this->db; $countall = $db->table('record_styles'); $countall->where('record_id', $id);
return $countall->countAllResults(); } }
2 - for updating tables, inserting data.
PHP Code: <?php
namespace App\Models\People;
use CodeIgniter\Model;
class AllNamesModel extends Model { protected $DBGroup = 'default'; protected $table = 'names'; protected $primaryKey = 'id'; protected $useAutoIncrement = true; protected $insertID = 0; protected $returnType = 'array'; protected $useSoftDeletes = false; protected $protectFields = true; protected $allowedFields = [ 'cover', 'name', 'shortbio', 'dupname', 'vip', 'dborn', 'mborn', 'yborn', 'bornplace', 'ddeath', 'mdeath', 'ydeath', 'deathplace', 'tab0', 'tab1', 'tab2', 'tab3', 'tab4', 'active', 'formed', 'disband' ]; }
Of course I could do it in a single Model but as what I have is a private site, only local, I have a second site dedicated only to table updates, 28 in total, and other support services, etc.
-
ikesela
Member
-
Posts: 155
Threads: 0
Joined: Nov 2020
Reputation:
7
08-19-2021, 12:12 PM
(This post was last modified: 08-19-2021, 12:12 PM by ikesela.)
(08-19-2021, 05:25 AM)but nneves Wrote: (08-19-2021, 04:12 AM)ikesela Wrote: this the problem, when you set to false, not working with updated_at (for auto update datetime).
$builder->set('views', 'views+1', false);
* this maybe bug
I've tested either with false, true or without any value and the updated_at field is not updated
(08-19-2021, 04:59 AM)nfaiz Wrote: because you creating another builder?
I can't use save/update because my primary key is composed with two fields.
I've tried too with $db->query('UPDATE string SET views = views + 1 ... and doesn't work either ...
I have this problem too before, when using this line:
$builder->set('views', 'views+1', false);
my noob solution is to turn off timestamp: $useTimestamps = false;
then i did update updated_at manually .
i hope there are solution in future to make it works when function is used ( problem maybe at builder).
-
nneves
Junior Member
-
Posts: 14
Threads: 6
Joined: Jun 2020
Reputation:
0
Yes, I've disable useTimestamps and just update the fields manually until a stable solution.
Even so, there is an insert query that updates updated_at with useTimestamps = false.
|