Welcome Guest, Not a member yet? Register   Sign In
allowedFields in class
#8

(05-23-2020, 02:40 AM)jreklund Wrote:
(05-21-2020, 04:12 PM)wdeda Wrote: Apparently, I presume, the system does not validate the fields in the Model with those in the table and/or it is only verified that the fields to be updated and listed in the Controller are listed in "allowed fields". But everything leads me to believe that the definition of what will actually be inserted/updated is what appears in "allowed fields", in Model.

That's correct, it would be a performance issue to look up the fields every time. And the reason $allowedFields are there in the first place are that you can just throw in the complete $_POST into $model->save() and it will just insert/update the data you are looking for. All other POST data will be ignored. Protecting you from mass assignment from other people.

Turning this:

PHP Code:
$model->save([
    'movie_id'  => $this->request->getVar('movie_id'),
/* 'ind'        => $this->request->getVar('ind'), */
    'countryx'  => $this->request->getVar('countryx'),
    'name5'      => $this->request->getVar('name5'),
    'link5'      => $this->request->getVar('link5'),
    'country5'  => $this->request->getVar('country5'),
    'name6'      => $this->request->getVar('name6'),
    'link6'      => $this->request->getVar('link6'),
    'country6'  => $this->request->getVar('country6'),
    'name7'      => $this->request->getVar('name7'),
    'link3'      => $this->request->getVar('link7'),
    'country7'  => $this->request->getVar('country7'),
    'name8'      => $this->request->getVar('name8'),
    'link8'      => $this->request->getVar('link8'),
    'country8'  => $this->request->getVar('country8'),
]); 

Into this:

PHP Code:
$model->save($this->request->getPost()); 

In case you don't need that feature at all, just apply this. And it will be disabled 
PHP Code:
$model = new GlobeNomModel();
$model->protect(false); 

Or in the model itself (protectFields):

PHP Code:
<?php namespace App\Models;

use 
CodeIgniter\Model;

class 
OscarNomModel extends Model
{
    protected $table  'oscnom';
    protected $primaryKey 'id';    
    
protected $returnType 'object';

    protected $TimeStamps true;
    protected $useSoftDeletes true;
    protected $protectFields false;


And it will insert everything in your array into the database, and in case it dosen't match in development an error will be thrown. In production you will get "We seem to have hit a snag. Please try again later...".

You can also use it for placeholders, so you can have dynamic rules, and those rules dosen't get inserted into the database.
https://codeigniter.com/user_guide/model...aceholders

(05-21-2020, 04:12 PM)wdeda Wrote: The other issue is not really a problem. When I was doing the migration, CI3 to Ci4, I had and I have this need to update several fields in the table of ALL movies, because during the process of creating the table I had a view of the data to be displayed, but later I changed of idea about several things and I'm paying the price for it.

Personally I just make those updates in an MySQL editor, either web-based like phpMyAdmin or HeidiSQL (personal favorite, as you can connect to servers not having an GUI).

Thank you very much, I could not expect anything other than that, a clear and objective answer.

I have in my ToDoList the creation of a development/test environment, with a replicated database and the objective of pioneering, yes, pioneering the CI manual from end to end, with all possible tests. Of course, if I were younger and with the availability of now I would have been at 555 Seymour St., Vancouver, BC for a long time.

Thank you very much, may you always be blessed.
Reply


Messages In This Thread
allowedFields in class - by pws - 05-20-2020, 09:48 AM
RE: allowedFields in class - by wdeda - 05-20-2020, 12:20 PM
RE: allowedFields in class - by jreklund - 05-21-2020, 02:47 AM
RE: allowedFields in class - by wdeda - 05-21-2020, 05:44 AM
RE: allowedFields in class - by jreklund - 05-21-2020, 07:42 AM
RE: allowedFields in class - by wdeda - 05-21-2020, 04:12 PM
RE: allowedFields in class - by jreklund - 05-23-2020, 02:40 AM
RE: allowedFields in class - by wdeda - 05-23-2020, 08:42 PM
RE: allowedFields in class - by pws - 06-05-2020, 08:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB