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?