![]() |
Can anyone tell me why I cannot access this property? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: Can anyone tell me why I cannot access this property? (/showthread.php?tid=87886) |
Can anyone tell me why I cannot access this property? - cx3700 - 06-13-2023 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? RE: Can anyone tell me why I cannot access this property? - InsiteFX - 06-13-2023 Because it is being returned as a JSON Object. PHP Code: $siteParams = json_decode($site_device, true); // true return Associated array RE: Can anyone tell me why I cannot access this property? - cx3700 - 06-13-2023 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" RE: Can anyone tell me why I cannot access this property? - kenjis - 09-20-2023 Because $site_device is null. |