我正在用php构建自定义mvc框架,以进行学习,当我尝试使用数据库中已存在的邮件提交表单时,我的验证应阻止我这样做,相反,我收到此错误: / p>
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!";
}
}
}
User.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的,但我遇到了麻烦,我们将为您提供任何帮助。
答案 0 :(得分:1)
问题在于,在UsersController的__construct中,您拥有:
subset
因此,您将from pyspark.ml.recommendation import ALS
lines = spark.read.text("Dataset.csv").rdd
parts = lines.map(lambda row: row.value.split(","))
ratingsRDD = parts.map(lambda p: Row(userId=int(p[1]),repoId=int(p[2]),repoCount=float(p[3])))
ratings = spark.createDataFrame(ratingsRDD)
model = ALS.trainImplicit(ratings, rank=5,lambda_=0.01, alpha = 1.0, iterations =5)
方法的返回值分配给$this->userModel = $this->load_model('User');
属性。
userModel
方法不会返回任何内容,因此load_model
始终设置为load_model
,与$this->userModel
是否成功无关。
如果要通过返回值将其分配给属性,则应仅在NULL
中的load_model
。
还要在return new $modelPath();
方法的末尾添加load_model
,以确保它确实加载了模型,而不仅仅是默默地失败了。
请注意,throw new Exception($modelPath. 'not found');
与load_model
(区分大小写)和$this->userModel
是不同的-为什么在App后有\,在模型后有两个\?
答案 1 :(得分:-1)
我认为您需要在$ this-> UserModel中访问您的模型,因为User
已传递到load_model方法中。