我用PHP创建了一个MVC格式,我得到了Trying to get property of non-object错误,因为例程对象为null,而您看到它是假的。简而言之,这里发生的事情是,我得到了一个具有routine_id的routine_id对象,并且我正在寻找与事件对象具有相同routine_id的例程对象,但是我得到了这个错误:
注意:尝试获取第13行bool(false) aaaaaaaaaaaaaaaaaaarray(1) { => object(Event_model)#6 (8) { "name"=> NULL "user_id"=> NULL "routine_id"=> string(2) "20“”年份“=> string(4)”月“=> string(1) "3”日期“=>”中的非对象属性字符串(2) "27“"event_id"=> string(1) "3”"_connection":protected=> object#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
}发布于 2019-04-07 06:50:30
当你打电话..。
$event = $event->getEventsbydate($y, $m, $d);它应该运行SQL,然后返回..。
return $stmt->fetchAll();然后使用$event在
$routine = $routine->getRoutine($event->routine_id);但是fetchAll()返回一个事件数组,所以如果您只期望一个事件,那么您可以执行其中一个.
return $stmt->fetch();使用fetch()只返回一行,或
$routine = $routine->getRoutine($event[0]->routine_id);所以使用$event[0]来选择第一行。
您可能会有多个事件,在这种情况下,您可能需要一个循环并创建多个Routine_model。
https://stackoverflow.com/questions/55554353
复制相似问题