getting non-object error only in log file |
hi, I'm trying to get my record from DB in an object and echo in the view page.
I did this before and worked but this time I'm getting a weird error only in the log file NOT on the view page. also, the result is showing on the view page perfectly! I wanna fix the error from my log file Model: PHP Code: function get_one_by($cond=array()){ Controller: PHP Code: $data['theForm'] = $this->form->get_one_by(array('URL' => $url)); View: PHP Code: echo heading($theForm->TITLE, 2); View Result: Code: <h2>a form</h2> Log: Code: ERROR - 2017-09-10 11:39:34 --> Severity: Notice --> Trying to get property of non-object /Applications/XAMPP/xamppfiles/htdocs/xx/xx.php 7 Thanks for any help
09-10-2017, 11:42 AM
(This post was last modified: 09-10-2017, 11:43 AM by InsiteFX. Edit Reason: spelling error )
Your $data is an associate array and your trying to assign an object to it.
PHP Code: if ( ! function_exists('arrayToObject')) Place these two methods / Functions in a CodeIgniter Helper. Usage: PHP Code: $data['theForm'] = objectToArray($this->form->get_one_by(array('URL' => $url))); See if that will fix your problem. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(09-10-2017, 11:42 AM)InsiteFX Wrote: Your $data is an associate array and your trying to assign an object to it. It shows another error Code: ERROR - 2017-09-11 10:01:57 --> Severity: Warning --> array_map() expects parameter 1 to be a valid callback, function 'objectToArray' not found or invalid function name But why I'm getting my variable on the view page correctly and ONLY I get the error in the log file? By the way, I've sent objects in $data in other controllers and they didn't raise any error even in the Log file!
Something is wrong!
I just realized it! even with this activerecord, PHP Code: $this->db->get_where($tablename, $cond)->result(); Code: array(1) { [0]=> object(stdClass)#27 (2) { even if I change the result type to PHP Code: $this->db->get_where($tablename, $cond)->result_array(); Code: array(1) { [0]=> array(2) { Why am i getting nested? PHP Code: // wrong! What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
What version of php are you running?
PHP Code: function get_one_by(array $cond){ In your function you are assigning an array to $cond What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(09-11-2017, 03:04 AM)InsiteFX Wrote: // should be I've never seen such thing as this ^ That's not gonna work... The $cond is my array which I pass to the model function. And i believe the result_array() is query output https://www.codeigniter.com/user_guide/d...ult-arrays PHP Code: $this->db->get_where('MYTABLE', array('URL' => 'abcd'))->result_array(); By the way, I'm running CI 3.1.5, PHP 5.6 Weird fact: When i type this in the controller, i DO NOT get any ERROR in the Log file PHP Code: var_dump($data['theForm']); But if I remove/comment the code above, The ERROR will appear in the Log file. Is this CodeIgniter's BUG?
See what you get when you add this at the end.
PHP Code: echo $this->db->last_query(); What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(09-10-2017, 07:24 PM)ardavan Wrote: Something is wrong! Look again. The output is an array, an array with one item. That item is an object. This is exactly what db->result() does - returns an array of objects. One object per row retrieved. (09-10-2017, 07:24 PM)ardavan Wrote: even if I change the result type to This is exactly what db->result_array() is supposed to do. You get an array of arrays. Each sub array is a record (row) from the db. |
Welcome Guest, Not a member yet? Register Sign In |