为什么我不能在这里尝试获取非对象错误的属性?

I made an MVC format in PHP, and I'm getting Trying to get property of non-object error because routine object is null which you see it is false. In short, what is happening here is that I got a $event object which has routine_id and I am looking for routine object that has the same routine_id as event object, however I'm getting this error:

none Notice: Trying to get property of non-object in G:\5th semester\Ecommerce\xampp\htdocs\app\controllers\Event.php on line 13 bool(false) aaaaaaaaaaaaaaaaaaarray(1) { [0]=> object(Event_model)#6 (8) { ["name"]=> NULL ["user_id"]=> NULL ["routine_id"]=> string(2) "20" ["year"]=> string(4) "2019" ["month"]=> string(1) "3" ["day"]=> string(2) "27" ["event_id"]=> string(1) "3" ["_connection":protected]=> object(PDO)#7 (0) { } } }

I try to var_dump $routine and $event so $event shows routine_id, but $routine is null.

Testing code:

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
}

When you call...

$event = $event->getEventsbydate($y, $m, $d);

this should run the SQL and then return...

return $stmt->fetchAll();

and then you use $event in

$routine = $routine->getRoutine($event->routine_id);

BUT fetchAll() returns an array of events, so if your expecting just one event, then you can do one of either...

return $stmt->fetch();

to use fetch() to just return the one row, or

$routine = $routine->getRoutine($event[0]->routine_id);

So use $event[0] to pick the first row.

It may be that you will have multiple events, in this case you may need a loop and create multiple Routine_models.