【问题标题】:Problem when trying to validate email that already exists in database尝试验证数据库中已存在的电子邮件时出现问题
【发布时间】:2019-07-30 17:54:35
【问题描述】:

我正在 php 中构建自定义 mvc 框架以便学习,当我尝试使用数据库中已存在的邮件提交表单时,我的验证应该阻止我这样做,而是出现此错误:

Fatal error: Uncaught Error: Call to a member function findUserByEmail() on null in C:\xampp\htdocs\gacho\App\Controllers\UsersController.php:

UsersController.php

<?php
namespace App\Controllers;

use App\Models\User;
use Core\Controller;

class UsersController extends Controller
{
    public function __construct($controller, $action)
    {
        parent::__construct($controller, $action);
        $this->userModel = $this->load_model('User');
    }

    public function registerAction()
    {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            $data = [
                'email' => trim($_POST['email']),
                ];
            }

            if (empty($data['email'])) {
                $data['email_err'] = "Please enter your email!!!";
            } else {
                if ($this->userModel->findUserByEmail($data['email'])) {
                $data['email_err'] = "Email is already taken!";
                }
          }
    }

用户.php

<?php
namespace App\Models;

use Core\Database;

class User
{
    private $db;

    public function __construct()
    {
        $this->db = new Database();
    }

    public function findUserByEmail($email)
    {
        $this->db->query('SELECT * FROM users WHERE email = :email');
        $this->db->bind(':email', $email);
        $row = $this->db->single();
        if ($this->db->rowCount() > 0) {
            return true;
        } else {
            return false;
        }
    }
}

Controller.php:

<?php
namespace Core;

class Controller
{
    protected $_controller;
    protected $_action;
    public $view;

    public function __construct($controller, $action)
    {
        $this->_controller = $controller;
        $this->_action = $action;
        $this->view = new View();
    }

    protected function load_model($model)
    {
        $modelPath = 'App\Models\\' . $model;
        if (class_exists($modelPath)) {
            $this->{$model.'Model'} = new $modelPath();
        } 
    }
}

我认为这个错误是关于 $this->userModel 的,但我被卡住了,感谢任何帮助。

【问题讨论】:

    标签: php validation oop model-view-controller


    【解决方案1】:

    问题是在 UsersController 的 __construct 中你有:

    $this->userModel = $this->load_model('User');
    

    因此,您将load_model 方法的返回值分配给userModel 属性。 load_model 方法不返回任何内容,因此 $this-&gt;userModel 始终设置为 NULL,不管 load_model 是否成功。

    如果你想通过返回值将它分配给一个属性,你应该只在load_model 中使用return new $modelPath();

    还在load_model 方法的末尾添加throw new Exception($modelPath. 'not found'); 以确保它确实加载了模型,而不仅仅是默默地找不到它。

    请注意$this-&gt;userModel$this-&gt;UserModel(区分大小写)和$modelPath = 'App\Models\\' . $model; 不同——为什么在App 之后有\,在Model 之后有两个\?

    【讨论】:

      【解决方案2】:

      我认为您需要在 $this->UserModel 中访问您的模型,因为 User 已传递到 load_model 方法中。

      【讨论】:

      • 我尝试了一些东西,但我想我并没有完全理解你的意思。请您再解释一下好吗?
      • 你能把UserController中的$this->userModel改成$this->UserModel吗
      • 是的。这就是我所做的,但它显示了同样的错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 1970-01-01
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      相关资源
      最近更新 更多