Welcome Guest, Not a member yet? Register   Sign In
Model basic CRUD methods and other queries on other tables using $this->db->table()
#1

I'm having what looks to me like a bug.
I have a model configured like this:

PHP Code:
protected $table         'sottoeventi';
protected 
$primaryKey    'id';

protected 
$returnType    'object';

protected 
$allowedFields = [
    'id_pdv',
    'id_evento',
    'focus',
    'status',
    'ultima_modifica',
    'sostituisce',
    'invio_sms',
    'ab_est',
    'motivazione',
];

protected 
$validationRules    = [
    'id_pdv' => 'required|is_natural_no_zero',
];

protected 
$validationMessages = [
    'id_pdv' => [
        'required'          => 'App.campo_vuoto',
        'is_natural_no_zero' => 'App.campo_vuoto',
    ],
]; 



Here are a number of methods that use CRUDs to execute various queries on the table set in $table.

but in certain situations I need to run queries on other tables before or after, so, in the specific case I have this kind of situation:


PHP Code:
// More code

else :

    // I delete a certain recored from a different table than the one set in $table and then I use the function table('my_different_table')
    $this->db->table('sottoeventi_personale')->where('id_sottoevento', (int)$id_sottoevento)->delete();

    // Log
    $basic_stuff_model->save_log('Delete giornate sottoevento'$this->db->getLastQuery());

    // Update the record on the main table set to $table
    $this->where('id', (int)$id_sottoevento)
        ->update(
            [
                'data_inizio' => '0000-00-00',
                'data_fine'  => '0000-00-00'
            ]
        );

endif; 


By doing so, however, I receive an error from the second query, that of update on the model table ($table)

There is no data to update.

If I modify the query using the table() function like this:

PHP Code:
$this->db->table('sottoeventi')->where('id', (int)$id_sottoevento)
    ->update(
        [
            'data_inizio' => '0000-00-00',
            'data_fine'  => '0000-00-00'
        ]
    ); 

everything works

I tried printing the $this->table variable, before and after the first query, but the value is correct.

At this point, I would expect the query to work normally when I update like this:

PHP Code:
$this->where('id', (int)$id_sottoevento)
        ->update(
            [
                'data_inizio' => '0000-00-00',
                'data_fine'  => '0000-00-00'
            ]
        ); 

Didn't I understand? or is there something wrong?
Reply
#2

Quote:To help protect against Mass Assignment Attacks, the Model class requires that you list all of the field names that can be changed during inserts and updates in the $allowedFields class property.
https://codeigniter4.github.io/CodeIgnit...ing-fields
Reply
#3

(This post was last modified: 02-22-2023, 02:21 AM by serialkiller.)

(02-21-2023, 10:22 PM)kenjis Wrote:
Quote:To help protect against Mass Assignment Attacks, the Model class requires that you list all of the field names that can be changed during inserts and updates in the $allowedFields class property.
https://codeigniter4.github.io/CodeIgnit...ing-fields

Another question, in the documentation in the "Upgrading From a Previous Version" section, there is no mention of version 4.3.2 which however is available from composer, is it just an oversight?

(02-21-2023, 10:22 PM)kenjis Wrote:
Quote:To help protect against Mass Assignment Attacks, the Model class requires that you list all of the field names that can be changed during inserts and updates in the $allowedFields class property.
https://codeigniter4.github.io/CodeIgnit...ing-fields

I fixed it, I thought I understood the problem, i.e. that the "start_date" and "end_date" fields weren't present in the $allowedFields array, but even adding them, I still have problems, what didn't I understand?
Reply
#4

(02-22-2023, 02:15 AM)serialkiller Wrote: Another question, in the documentation in the "Upgrading From a Previous Version" section, there is no mention of version 4.3.2 which however is available from composer, is it just an oversight?

See https://codeigniter4.github.io/CodeIgnit...e_432.html
Reply
#5

(02-22-2023, 02:15 AM)serialkiller Wrote: I fixed it, I thought I understood the problem, i.e. that the "start_date" and "end_date" fields weren't present in the $allowedFields array, but even adding them, I still have problems, what didn't I understand?

What are "start_date" and "end_date"?
Your fields are "data_inizio" and "data_fine".
Reply
#6

(02-22-2023, 02:47 AM)kenjis Wrote:
(02-22-2023, 02:15 AM)serialkiller Wrote: Another question, in the documentation in the "Upgrading From a Previous Version" section, there is no mention of version 4.3.2 which however is available from composer, is it just an oversight?

See https://codeigniter4.github.io/CodeIgnit...e_432.html
So I don't understand why it isn't marked here

https://codeigniter.com/user_guide/insta...ading.html

For the problem of the post, what am I still doing wrong?
Reply
#7

See the Note in https://forum.codeigniter.com/showthread...#pid406955
Reply
#8

(02-22-2023, 02:55 AM)kenjis Wrote: See the Note in https://forum.codeigniter.com/showthread...#pid406955

ok, thanks, I didn't read.

Back to the main problem, earlier I mistakenly forgot to add the 2 fields in the "allowedFields" list, now they are present, but it keeps telling me "There is no data to update", if I use the table('table_name') function, it works
Reply
#9

When you use table('table_name'), you are using a new Query Builder instance directly.
You are not using the Model.
Reply
#10

(This post was last modified: 02-22-2023, 03:28 AM by serialkiller.)

(02-22-2023, 03:06 AM)kenjis Wrote: When you use  table('table_name'), you are using a new Query Builder instance directly.
You are not using the Model.

Yes, I know this, this is the problem right here, because if I don't use table() of the query builder, the query fails?

The Model has its own table set, and the query in question should use that table "sottoeventi".

Code:
protected $table = 'sottoeventi';

In the specific case I first run other queries with the query builder, use different tables than the model one, then use table('other_table').



PHP Code:
$this->db->table('sottoeventi_personale')->where('id_sottoevento', (int)$id_sottoevento)->delete();

// Log
$basic_stuff_model->save_log('Delete giornate sottoevento'$this->db->getLastQuery());

$this->where('id', (int)$id_sottoevento)
        ->update(
            [
                'data_inizio' => '0000-00-00',
                'data_fine'   => '0000-00-00'
            ]
        ); 

The update query returns the error "There is no data to update", while you expect it to update the "sottoeventi" table, which is the Model
Reply




Theme © iAndrew 2016 - Forum software by © MyBB