首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么我不能在这里尝试获取非对象错误的属性?

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

Stack Overflow用户
提问于 2019-04-06 23:29:17
回答 1查看 45关注 0票数 0

我用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是空的。

测试代码:

代码语言:javascript
运行
复制
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
}
EN

回答 1

Stack Overflow用户

发布于 2019-04-07 06:50:30

当你打电话..。

代码语言:javascript
运行
复制
$event = $event->getEventsbydate($y, $m, $d);

它应该运行SQL,然后返回..。

代码语言:javascript
运行
复制
return $stmt->fetchAll();

然后使用$event

代码语言:javascript
运行
复制
$routine = $routine->getRoutine($event->routine_id);

但是fetchAll()返回一个事件数组,所以如果您只期望一个事件,那么您可以执行其中一个.

代码语言:javascript
运行
复制
return $stmt->fetch();

使用fetch()只返回一行,或

代码语言:javascript
运行
复制
$routine = $routine->getRoutine($event[0]->routine_id);

所以使用$event[0]来选择第一行。

您可能会有多个事件,在这种情况下,您可能需要一个循环并创建多个Routine_model

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55554353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档