【问题标题】:Message: Trying to get property of non-object Filename: controllers/site.php消息:试图获取非对象文件名的属性:controllers/site.php
【发布时间】:2015-11-29 18:40:02
【问题描述】:

我的模型名称是 common_model 并且有一个方法:

function select_fields_where_like_join($tbl = '', $data, $joins = '', $where = '', $single = FALSE, $field = '', $value = '',$group_by='',$order_by = '',$limit = '')
{

    if (is_array($data) and isset($data[1]))
    {

        $this->db->select($data[0],$data[1]);

    }
    else
    {
        $this->db->select($data);
    }

    $this->db->from($tbl);
    if ($joins != '') 
    {
        foreach ($joins as $k => $v) 
        {
            $this->db->join($v['table'], $v['condition'], $v['type']);
        }
    }

    if ($value !== '') 
    {
        $this->db->like('LOWER(' . $field . ')', strtolower($value));
    }

    if ($where != '') 
    {
        $this->db->where($where);
    }
    if($group_by != '')
    {
        $this->db->group_by($group_by);
    }
    if($order_by != '')
    {
        if(is_array($order_by))
        {
            $this->db->order_by($order_by[0],$order_by[1]);
        }
        else
        {
            $this->db->order_by($order_by);
        }
    }
    if($limit != '')
    {
        if(is_array($limit))
        {
            $this->db->limit($limit[0],$limit[1]);
        }
        else
        {
            $this->db->limit($limit);
        }
    }
    $query = $this->db->get();

    if ($query->num_rows() > 0)
    {

        if ($single == TRUE) 
        {
            return $query->row();
        }
        else 
        {
            return $query->result();
        }
    }
    else 
    {


        return FALSE;
    }

}

这是我的控制器方法:

public function load_vo_training($themeID=NULL)
{
    if (!isset($themeID) || !is_numeric($themeID) || empty($themeID) || $themeID == NULL) 
    {
        $msg = 'Redirected To Themes, As No Record Found For your Request:FAIL';
        $this->session->set_flashdata('msg', $msg);
        redirect('site/load_theme');
    }
    $bool = is_admin($this->data['UserID']);
    if (!is_admin($this->data['UserID'])) 
    {

        $PTable = 'Theme T';
        $selectData = array('COUNT(*) AS TotalRecordsFound', true);
        $joins = array(
            array(
                'table' => 'project_theme PT',
                'condition' => 'PT.theme_id = T.theme_id AND PT.enabled = 1',
                'type' => 'INNER'
            ),
            array(
                'table' => 'project P',
                'condition' => 'P.project_id = PT.projects_id',
                'type' => 'INNER'
            ),
            array(
                'table' => 'sys_groups_projects_themes_permissions SGPTP',
                'condition' => 'SGPTP.projectID = P.project_id AND SGPTP.trashed = 0',
                'type' => 'INNER'
            ),
            array(
                'table' => 'sys_groups G',
                'condition' => 'G.groupID = SGPTP.groupID',
                'type' => 'INNER'
            ),
            array(
                'table' => 'user_account U',
                'condition' => 'U.groupID = G.groupID',
                'type' => 'INNER'
            )
        );
        $where = array(
            'T.theme_id' => $themeID
        );
        $countResult = $this->common_model->select_fields_where_like_join($PTable, $selectData, $joins, $where);
        if ($countResult->TotalRecordsFound > 0) 
        {
            echo $countResult->TotalRecordsFound;
        } else 
        {
            echo "no record";
        }
    }
}

我得到了这样的输出,带有错误消息 array(1) { [0]=> object(stdClass)#26 (1) { ["TotalRecordsFound"]=> string(3) "139" } }

遇到了 PHP 错误

严重性:通知

消息:试图获取非对象的属性

文件名:controllers/site.php

行号:5408

请帮助我并提前感谢。

【问题讨论】:

  • 好的thanx。现在我正确地发布了我的问题。任何人请帮助
  • 如果 CI 版本 3 重要,控制器、模型和库应该大写。 Docs.
  • 我使用的是 CI 2.2.0 版。
  • 在您的模型上尝试返回 $query->result_array();return $query->row_array();

标签: php codeigniter


【解决方案1】:

$countResult 是一个对象数组。所以你必须使用数组索引来访问其中的值。在访问数组之前使用 !empty($countResult)。

if (!empty($countResult)) {
    if ($countResult[0]->TotalRecordsFound > 0) 
    {
        echo $countResult[0]->TotalRecordsFound;
    } else 
    {
         echo "no record";
     }
}

【讨论】:

  • 非常感谢@sanjay kumar。以及所有关注我的问题的朋友。它的工作原理。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-21
  • 2021-10-19
  • 1970-01-01
相关资源
最近更新 更多