Welcome Guest, Not a member yet? Register   Sign In
Object of class stdClass could not be converted to string
#1

(This post was last modified: 02-28-2024, 04:03 PM by kenjis.)

Does anybody wants to share a code sample how to get query builder result into object with pagination?

function getMatrix3(){
        $builder = $this->db->table('zencrm_relationmatrix');
        //$builder->select('zencrm_relationmatrix.id,RelationName,RelationNameB,r1.Relation,r2.relation');
        $builder->select('RMSector,RMStatus,RelationName,RelationNameB,r1.Relation,r2.relation');
        $builder->join('relations as r1','RelationName= r1.id');
        $builder->join('relations as r2','RelationNameB= r2.id');
       
      $matrix = $builder->get()->getResultObject();

Code:
function getMatrix3(){
        $builder = $this->db->table('zencrm_relationmatrix');
        //$builder->select('zencrm_relationmatrix.id,RelationName,RelationNameB,r1.Relation,r2.relation');
        $builder->select('RMSector,RMStatus,RelationName,RelationNameB,r1.Relation,r2.relation');
        $builder->join('relations as r1','RelationName= r1.id');
        $builder->join('relations as r2','RelationNameB= r2.id');
       
      $matrix = $builder->get()->getResultObject();
        //$matrix = $builder->get()->getResult();
        //return(array) $matrix;
      return $matrix->num_rows();
    }

$matrix is expecting an object instead of an array, why as I used [getResultObject?]

Thanks for helping!

RR        //$matrix = $builder->get()->getResult();$//return(array) $matr
       return $matrix->$4
     }



function getMatrix3(){
        $builder = $this->db->table('zencrm_relationmatrix');
        //$builder->select('zencrm_relationmatrix.id,RelationName,RelationNameB,r1.Relation,r2.relation');
        $builder->select('RMSector,RMStatus,RelationName,RelationNameB,r1.Relation,r2.relation');
        $builder->join('relations as r1','RelationName= r1.id');
        $builder->join('relations as r2','RelationNameB= r2.id');
       
      $matrix = $builder->get()->getResultObject();
        //$matrix = $builder->get()->getResult();
        //return(array) $matrix;
       return $matrix->num_rows();
     }
Reply
#2

What's your exact issue?
Reply
#3

Here is a couple of helper methods to handle that.
Just add them to one of your helpers.

PHP Code:
/**
 * objectToArray ()
 * -----------------------------------------------------------------------
 */
if ( ! function_exists('objectToArray'))
{
    /**
    * objectToArray ()
    * -------------------------------------------------------------------
    *
    * @param  object $data
    * @return array
    */
    function objectToArray(object $data) : array
    {
        if (is_object($data))
        {
            /**
            * Gets the properties of the given object
            * with get_object_vars function
            */
            $data get_object_vars($data);
        }

        /**
        * Return array converted to object Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
        return (is_array($data)) ? array_map(__FUNCTION__$data) : $data;
    }
}

/**
 * arrayToObject
 * -----------------------------------------------------------------------
 */
if ( ! function_exists('arrayToObject'))
{
    /**
    * arrayToObject ()
    * -------------------------------------------------------------------
    *
    * @param  array $data
    * @return object
    */
    function arrayToObject(array $data) : object
    
{
        /**
        * Return array converted to object Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
        return (is_array($data)) ? (object) array_map(__FUNCTION__$data) : $data;
    }

What did you Try? What did you Get? What did you Expect?

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

(02-28-2024, 04:05 PM)kenjis Wrote: What's your exact issue?

The exact error is: [call to a member function num_rows on array] Call to[c member function num_rows() on array
Reply
#5

You can see it with dd(). The result is an array of stdClass objects.

PHP Code:
dd($matrix); 
Reply
#6

I couldn't catch the problem here?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB