Codeigniter 4 在型号为'的控制器上插入函数;I don’我不能在codeigniter4中工作。。我怎样才能解决这个问题?
从两天前开始我就一直面临这个问题。但是,也无法修复它。我已经在下面提交了所有必要的编码信息。请帮帮我。 这是我的控制器Codeigniter 4 在型号为'的控制器上插入函数;I don’我不能在codeigniter4中工作。。我怎样才能解决这个问题?,codeigniter-4,Codeigniter 4,从两天前开始我就一直面临这个问题。但是,也无法修复它。我已经在下面提交了所有必要的编码信息。请帮帮我。 这是我的控制器 <?php namespace App\Controllers; use App\Models\UserModel; use App\Controllers\BaseController; class UserController extends BaseController { protected $userMode
<?php namespace App\Controllers;
use App\Models\UserModel;
use App\Controllers\BaseController;
class UserController extends BaseController
{
protected $userModel;
public function __construct()
{
$this->userModel = new UserModel();
}
public function registration() {
if($this->request->getMethod() == 'post'):
$rules = $this->userModel->getValidationRules(['only' => ['user', 'email', 'password',
'confirm_password', 'gender']]);
if(!$this->validate($rules)):
$this->session->setFlashdata('errors', $this->validator->getErrors());
return redirect()->back()->withInput();
else:
$data = $this->request->getPost();
unset($data['confirm_password'], $data['registration'], $data['ci4app_auto_fill']);
if($this->userModel->insert($data)):
$this->session->setFlashdata('success', 'Your registration has been done successfully. Thank you.');
return redirect()->to('login');
else:
echo "Sorry! Your registration has not been successful. Please, try again. Thank you.";
endif;
endif;
endif;
}
}
你试过自己调试吗?如果是,你到底做了什么?试着用一个小例子来解决这个大问题,用更少的变量等等。。。“一次一个手术。谢谢,@Gildas,谢谢你的热情回应。”。是的。在尝试使用$this->userModel->insert($data)插入数据库之前,一切正常。但是,只要我尝试插入数据,并看到var_dump的结果,就会发现bool false,这意味着没有插入任何数据,这样的情况就会发生。但未生成任何错误消息。这就是问题所在。再次感谢您。您两次验证您的日期,不确定这是否是问题所在,但这在这里发生。如果在模型中声明验证规则,它们将在所有插入和更新操作中自动运行,则无需将这些验证规则加载到控制器中并在那里运行。非常感谢@marcogmonteiro。然后,我必须在模型中定义一个类-公共函数insertData($data){if($this->db->table('users')->insert($data)):返回true;endif;}-并在控制器中像$this->userModel->insertData($data)那样调用它。然后,它开始工作并插入数据。但是,问题是,当时我的hashPassword()函数不起作用,它绑定到$beforeInsert事件,如代码部分的avob帖子所示,因此密码没有被哈希。相比之下,如果我想在controller中使用insert furntion,那么如何从all中选择一些验证规则。使用默认的insert方法并在失败时打印$this->userModel->errors()。可能有一些验证规则没有通过,因为您可能发送了错误的数据。
<?php namespace App\Models;
use CodeIgniter\Model;
class UserModel extends Model
{
protected $table = 'users';
protected $primaryKey = 'id';
protected $returnType = 'array';
protected $allowedFields = ['user', 'name', 'email', 'phone', 'gender', 'password', 'address',
'city', 'estate', 'country', 'image'];
protected $beforeInsert = ['hashPassword'];
protected $useSoftDeletes = true;
protected $useTimestamps = false;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $validationRules = [
'user' => [
'label' => 'User ID',
'rules' => 'trim|required|is_unique[users.user,id,{id}]|alpha_numeric|min_length[3]'
],
'gender' => [
'label' => 'Gender',
'rules' => 'trim|required'
],
'email' => [
'label' => 'E-mail ID',
'rules' => 'trim|required|is_unique[users.email,id,{id}]|valid_email'
],
'password' => [
'label' => 'Password',
'rules' => 'trim|required|min_length[5]'
],
'confirm_password' => [
'label' => "Password Confirmation",
'rules' => 'trim|required|matches[password]'
],
'phone' => [
'label' => 'Phone Number',
'rules' => 'trim|numeric|max_length[13]|is_unique[users.phone,id,{id}]'
],
'address' => [
'label' => 'Address',
'rules' => 'trim|alpha_numeric_space'
],
'city' => [
'label' => 'City',
'rules' => 'trim|alpha_numeric_space'
],
'estate' => [
'label' => 'Estate',
'rules' => 'trim|alpha_space'
],
'country' => [
'label' => 'Country',
'rules' => 'trim|alpha_space'
]
];
protected $validationMessages = [];
protected $skipValidation = false;
public function hashPassword($data) {
if(! isset($data['data']['password'])) return $data;
$data['data']['password'] = password_hash($data['data']['password'], PASSWORD_DEFAULT);
return $data;
}
}