【发布时间】:2019-04-06 23:29:17
【问题描述】:
我在 PHP 中创建了一个 MVC 格式,我收到了Trying to get property of non-object 错误,因为例程对象为空,而你看到它是假的。简而言之,这里发生的是我得到了一个具有routine_id 的$event 对象,我正在寻找与事件对象具有相同routine_id 的例程对象,但是我收到了这个错误:
none 注意:试图在 G:\5th 中获取非对象的属性 第 13 行学期\电子商务\xampp\htdocs\app\controllers\Event.php bool(false) aaaaaaaaaaaaaaaaarray(1) { [0]=> 对象(Event_model)#6 (8) { ["name"]=> NULL ["user_id"]=> NULL ["routine_id"]=> 字符串(2) "20" ["year"]=> string(4) "2019" ["month"]=> string(1) "3" ["day"]=> 字符串(2)“27”[“event_id”]=>字符串(1)“3” ["_connection":protected]=> 对象(PDO)#7 (0) { } } }
我尝试 var_dump $routine 和 $event 所以 $event 显示routine_id,但 $routine 为空。
测试代码:
var_dump($routine);
echo "aaaaaaaaaaaaaaaaaa";
var_dump($event);
public function index($y, $m, $d) {
$event = $this->model('Event_model');
$event = $event->getEventsbydate($y, $m, $d);
$routine = $this->model('Routine_model');
$routine = $routine->getRoutine($event->routine_id);
if($event == false){ //if there is nothing in the workout_list
header("location:/Event/create/$y/$m/$d"); //mylist controller , create method
} else {
var_dump($routine);
echo "aaaaaaaaaaaaaaaaaa";
var_dump($event);
//header('location:/Routine/index_bycalendar/$event->routine_id');
}
}
<?php
//for event
public function insert() {
$stmt = $this
->_connection
->prepare("INSERT INTO event(routine_id, year, month, day) VALUES(:routine_id, :year, :month, :day)");
$stmt->execute(['routine_id' => $this->routine_id, 'year' => $this->year, 'month' => $this->month, 'day' => $this->day]);
return $stmt->rowCount();
}
public function getEventsbydate($year, $month, $day) {
$stmt = $this
->_connection
->prepare("SELECT * FROM event WHERE year = :year AND month = :month AND day = :day");
$stmt->execute(['year' => $year, 'month' => $month, 'day' => $day]);
$stmt->setFetchMode(PDO::FETCH_CLASS, "Event_model"); //datatype user
return $stmt->fetchAll();
}
public function getRoutine($routine_id) {
$stmt = $this
->_connection
->prepare("SELECT * FROM routine WHERE routine_id = :routine_id");
$stmt->execute(['routine_id' => $routine_id]);
$stmt->setFetchMode(PDO::FETCH_CLASS, "Routine_model"); //datatype user
return $stmt->fetch(); //it should return a user
}
【问题讨论】:
-
尝试使用硬编码
routine_id在你的sql客户端(例如在phpMyAdmin中)中的方法getRoutine()执行sql语句值 (20)。喜欢SELECT * FROM routine WHERE routine_id = 20。结果是什么? -
感谢您的评论我得到了我想要得到的行,即routine_id = 20