Welcome Guest, Not a member yet? Register   Sign In
getting non-object error only in log file
#1

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()){
 
   $result $this->db->get_where($tablename$cond);
 
   if $result->num_rows() > ){
 
       return $result->row();
 
   }
 
   return FALSE;


Controller:
PHP Code:
$data['theForm'] = $this->form->get_one_by(array('URL' => $url));
$this->load->view('theForm'$data); 

View:
PHP Code:
echo heading($theForm->TITLE2); 

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
Reply
#2

(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'))
{
    
/**
     * arrayToObject ()
     * -------------------------------------------------------------------
     *
     * Converts an array to an object.
     *
     * USAGE: $obj = arrayToObject($data)
     *
     * @param   $data
     * @return  object
     */
    
function arrayToObject($data)
    {
        if (
is_array($data))
        {
            
/**
             * Return array converted to object Using __FUNCTION__
             * (Magic constant) for recursive calls
             */
            
return (object) array_map(__FUNCTION__$data);
        }
        else
        {
            return 
$data;
        }
    }
}

if ( ! 
function_exists('objectToArray'))
{
    
/**
     * objectToArray ()
     * -------------------------------------------------------------------
     *
     * Converts an object to an array.
     *
     * USAGE: $array = objectToArray($object);
     *
     * @param  $obj
     * @return array
     */
    
function objectToArray($obj)
    {
        if (
is_object($obj))
        {
            
// Gets the properties of the given object with get_object_vars function
            
$obj get_object_vars($obj);
        }

        if (
is_array($obj))
        {
            
/**
             * Return array converted to object Using __FUNCTION__
             * (Magic constant) for recursive calls
             */
            
return array_map(__FUNCTION__$obj);
        }
        else
        {
            return 
$obj;
        }
    }


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 )
Reply
#3

(This post was last modified: 09-10-2017, 07:03 PM by ardavan.)

(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!
Reply
#4

(This post was last modified: 09-10-2017, 07:36 PM by ardavan.)

Something is wrong!
I just realized it!

even with this activerecord,
PHP Code:
$this->db->get_where($tablename$cond)->result(); 
supposed to get the result in an array, but still is an object
Code:
array(1) { [0]=> object(stdClass)#27 (2) {
this is my output!

even if I change the result type to
PHP Code:
$this->db->get_where($tablename$cond)->result_array(); 
i get nested array!
Code:
array(1) { [0]=> array(2) {

Why am i getting nested?
Reply
#5

PHP Code:
// wrong!
$this->db->get_where($tablename$cond)->result_array();

// should be
$this->db->get_where($tablename$cond->result_array()); 
What did you Try? What did you Get? What did you Expect?

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

(This post was last modified: 09-11-2017, 03:12 AM by InsiteFX.)

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 )
Reply
#7

(This post was last modified: 09-11-2017, 07:48 PM by ardavan.)

(09-11-2017, 03:04 AM)InsiteFX Wrote: // should be
$this->db->get_where($tablename, $cond->result_array()); [/php]

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
Reply
#8


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?
Reply
#9

See what you get when you add this at the end.

PHP Code:
echo $this->db->last_query();
exit(); 
What did you Try? What did you Get? What did you Expect?

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

(09-10-2017, 07:24 PM)ardavan Wrote: Something is wrong!
I just realized it!

even with this activerecord,
PHP Code:
$this->db->get_where($tablename$cond)->result(); 
supposed to get the result in an array, but still is an object
Code:
array(1) { [0]=> object(stdClass)#27 (2) {
this is my output!

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
PHP Code:
$this->db->get_where($tablename$cond)->result_array(); 
i get nested array!
Code:
array(1) { [0]=> array(2) {

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB