CodeIgniter Forums
Extended model not saving additional fields - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Extended model not saving additional fields (/showthread.php?tid=88953)



Extended model not saving additional fields - Pizote - 12-08-2023

I am using CI4 with Shield.  I have extended the shield UserModel to add some additional fields.  The field names match in the form, the model, and the db.  I cannot get the canned Shield registration page to save the additional fields.  There is no entry in the log files, and there are no validation issues.  All of the other fields in the original register form are saving to the db.

Any ideas as to what I am missing here?

UserModel
PHP Code:
declare(strict_types=1);

namespace 
App\Models;

use 
CodeIgniter\Shield\Models\UserModel as ShieldUserModel;

class 
UserModel extends ShieldUserModel
{
    protected function initialize(): void
    
{
        parent::initialize();

        $this->allowedFields = [
            ...$this->allowedFields,
            'first_name',
            'middle_initial',
            'last_name',
        ];
    }


Register form:

PHP Code:
<!-- First Name -->
<
div class="form-floating mb-2">
    <input type="text" class="form-control" id="floatingFirstnameInput" name="first_name"
        inputmode="text" autocomplete="first_name" placeholder="<?= lang('Auth.first_name') ?>"
        value="<?= old('first_name') ?>" required>
    <label for="floatingFirstnameInput">
        <?= lang('Auth.first_name'?>
    </label>
</div>

<!-- Middle Initial -->
<div class="form-floating mb-2">
     <input type="text" class="form-control" id="floatingMiddleinitialInput" name="middle_initial"
           inputmode="text" autocomplete="middle_initial" placeholder="<?= lang('Auth.middle_initial'?>"
           value="<?= old('middle_initial'?>">
     <label for="floatingMiddleinitialInput">
         <?= lang('Auth.middle_initial'?>
     </label>
</div>

<!-- Last Name -->
<div class="form-floating mb-4">
    <input type="text" class="form-control" id="floatingLastnameInput" name="last_name"
        inputmode="text" autocomplete="last_name" placeholder="<?= lang('Auth.last_name'?>"
        value="<?= old('last_name'?>" required>
    <label for="floatingLastnameInput">
        <?= lang('Auth.last_name'?>
    </label>
</div> 



RE: Extended model not saving additional fields - datamweb - 12-08-2023

See https://github.com/codeigniter4/shield/discussions/333


RE: Extended model not saving additional fields - Pizote - 12-08-2023

(12-08-2023, 12:38 PM)datamweb Wrote: See https://github.com/codeigniter4/shield/discussions/333

Step six did the trick.  

Changing the public string to:

PHP Code:
public string $userProvider = \App\Models\UserModel::class; 

Thank you!


RE: Extended model not saving additional fields - datamweb - 12-09-2023

Thanks for the feedback. The documents have been updated.
https://github.com/codeigniter4/shield/pull/977