dousi0144 2019-04-06 23:29
浏览 72

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

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
}
  • 写回答

1条回答 默认 最新

  • duanqian3464 2019-04-07 06:50
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题