CodeIgniter Shield自定义用户字段无法存入数据库求助
问题描述
我在使用CodeIgniter Shield授权登录系统时,按照官方指南添加了名为cellulare的自定义用户字段,但注册新用户时该字段无法存入用户表。我肯定漏了某个步骤,但找不到问题在哪,求帮忙排查。
相关代码
迁移文件(AddMobileNumberToUsers.php)
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Forge; use CodeIgniter\Database\Migration; class AddMobileNumberToUsers extends Migration { /** * @var string[] */ private array $tables; public function __construct(?Forge $forge = null) { parent::__construct($forge); /** @var \Config\Auth $authConfig */ $authConfig = config('Auth'); $this->tables = $authConfig->tables; } public function up() { $fields = [ 'cellulare' => ['type' => 'VARCHAR', 'constraint' => '20', 'null' => true], ]; $this->forge->addColumn($this->tables['users'], $fields); } public function down() { $fields = [ 'cellulare', ]; $this->forge->dropColumn($this->tables['users'], $fields); } }
UserModel.php
<?php 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, 'cellulare', ]; } }
Validation.php
<?php namespace Config; use CodeIgniter\Config\BaseConfig; use CodeIgniter\Validation\StrictRules\CreditCardRules; use CodeIgniter\Validation\StrictRules\FileRules; use CodeIgniter\Validation\StrictRules\FormatRules; use CodeIgniter\Validation\StrictRules\Rules; class Validation extends BaseConfig { // -------------------------------------------------------------------- // Setup // -------------------------------------------------------------------- /** * Stores the classes that contain the * rules that are available. * * @var string[] */ public array $ruleSets = [ Rules::class, FormatRules::class, FileRules::class, CreditCardRules::class, ]; /** * Specifies the views that are used to display the * errors. * * @var array<string, string> */ public array $templates = [ 'list' => 'CodeIgniter\Validation\Views\list', 'single' => 'CodeIgniter\Validation\Views\single', ]; // -------------------------------------------------------------------- // Rules // -------------------------------------------------------------------- public $registration = [ 'username' => [ 'label' => 'Auth.username', 'rules' => [ 'required', 'max_length[30]', 'min_length[3]', 'regex_match[/\A[a-zA-Z0-9\.]+\z/]', 'is_unique[users.username]', ], ], 'cellulare' => [ 'label' => 'cellulare', 'rules' => [ 'max_length[20]', 'min_length[10]', 'regex_match[/\A[0-9]+\z/]', 'is_unique[users.cellulare]', ], ], 'email' => [ 'label' => 'Auth.email', 'rules' => [ 'required', 'max_length[254]', 'valid_email', 'is_unique[auth_identities.secret]', ], ], 'password' => [ 'label' => 'Auth.password', 'rules' => [ 'required', 'max_byte[72]', 'strong_password[]', ], 'errors' => [ 'max_byte' => 'Auth.errorPasswordTooLongBytes', ] ], 'password_confirm' => [ 'label' => 'Auth.passwordConfirm', 'rules' => 'required|matches[password]', ], ]; }
排查方向
- 确认Shield使用自定义UserModel:打开
Config/Auth.php,找到$userModel配置项,修改为public string $userModel = \App\Models\UserModel::class;,确保Shield用的是你添加了cellulare字段的模型。 - 检查注册数据传递:确保注册表单包含
cellulare输入框,且控制器处理注册时,将该字段包含在提交的数据中,比如:$data = $this->request->getPost(['username', 'email', 'password', 'password_confirm', 'cellulare']); $user = auth()->register($data); - 调整验证规则:如果
cellulare是可选字段,给它的验证规则加上permit_empty,否则用户未填写时会触发验证失败,导致数据无法写入。修改后的规则如下:'cellulare' => [ 'label' => 'cellulare', 'rules' => [ 'permit_empty', 'max_length[20]', 'min_length[10]', 'regex_match[/\A[0-9]+\z/]', 'is_unique[users.cellulare]', ], ], - 验证迁移执行结果:运行
php spark migrate后,直接查看数据库的users表,确认cellulare字段是否存在,避免迁移未成功执行的情况。 - 调试allowedFields:可以在
UserModel的initialize方法中临时添加var_dump($this->allowedFields);,确认cellulare确实被加入到允许字段列表中。
内容的提问来源于stack exchange,提问作者Federico elbambolo
相关产品推荐
相关产品推荐

