Welcome Guest, Not a member yet? Register   Sign In
Can anyone tell me why I cannot access this property?
#1

I have a controller function that is called by AJAX. The AJAX posts one property to it called $site. The value of $site is arriving at the controller just fine.

Here's my controller function:

public function ajax_get_sign_partial() {
    $site = new stdClass();
    $site->id = $this->request->getPost('site');
    $site_device = $this->SiteModel->getSiteDevice($site);
}


Here is my model function:


public function getSiteDevice($site) {
    $sql = "SELECT site_device.*, device.name AS device_name
                FROM site_device
                JOIN device ON device.id = site_device.model site_device.site_id = ?";
    $query = $this->db->query($sql, array($site->id));
    return $query->getRow();
}



If I echo out the $site_device I get the expected response:

{
    "id": "63",
    "site_id": "109",
    "model": "1",
    "pse": "1",
    "serial": "1234333",
    "status": "0",
    "created": "1686706449",
    "updated": "1686706449",
    "deleted": "0",
    "device_name": "PVCoSTAR-PSPT"
}

However, if I echo out $site_device->model I get this error:

Uncaught Error.
{
    "title": "ErrorException",
    "type": "ErrorException",
    "code": 500,
    "message": "Attempt to read property \"model\" on null",
    "file": "/var/www/vhosts/dev.mysite.com/httpdocs/app/Controllers/Settings.php",
    "line": 258,
    "trace": [
        {
            "file": "/var/www/vhosts/dev.mysite.com/httpdocs/app/Controllers/Settings.php",
            "line": 258,
            "function": "errorHandler",
            "class": "CodeIgniter\\Debug\\Exceptions",
            "type": "->"
        },
        {
            "file": "/var/www/vhosts/dev.mysite.com/httpdocs/vendor/codeigniter4/framework/system/CodeIgniter.php",
            "line": 934,
            "function": "ajax_get_sign_partial",
            "class": "App\\Controllers\\Settings",
            "type": "->"
        },
        {
            "file": "/var/www/vhosts/dev.mysite.com/httpdocs/vendor/codeigniter4/framework/system/CodeIgniter.php",
            "line": 499,
            "function": "runController",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->"
        },
        {
            "file": "/var/www/vhosts/dev.mysite.com/httpdocs/vendor/codeigniter4/framework/system/CodeIgniter.php",
            "line": 368,
            "function": "handleRequest",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->"
        },
        {
            "file": "/var/www/vhosts/dev.mysite.com/httpdocs/public/index.php",
            "line": 67,
            "function": "run",
            "class": "CodeIgniter\\CodeIgniter",
            "type": "->"
        }
    ]
}

So why can I not access the model property on $site_device? It is present in the object.

The $site_device object is not null. If I wrap it in is_null() it says it is not null. 

In the controller function, if I change $site-> id to this, it works:

$site->id = 109;

Why is that property (or any of the properties for that matter) innaccessible?
Reply
#2

Because it is being returned as a JSON Object.

PHP Code:
$siteParams json_decode($site_devicetrue); // true return Associated array 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 06-13-2023, 11:00 PM by cx3700.)

That's not the problem, it is failing long before that. 

if I do this:


$data = array();
$site = new stdClass();
$site->id = (int)$this->request->getPost('site');
$site_device = $this->SiteModel->getSiteDevice($site);

and then in the same function this:

$max_faces = $this->SiteModel->getMaxFaces($site_device->model);

before any return, I get the same error: "Attempt to read property \"model\" on null"
Reply
#4

Because $site_device is null.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB