-
MarcoB Newbie

-
Posts: 9
Threads: 3
Joined: Jul 2023
Reputation:
0
02-28-2024, 02:17 PM
(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();
}
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
-
InsiteFX Super Moderator
     
-
Posts: 6,705
Threads: 341
Joined: Oct 2014
Reputation:
245
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 )
-
MarcoB Newbie

-
Posts: 9
Threads: 3
Joined: Jul 2023
Reputation:
0
(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
-
jackiemack Newbie

-
Posts: 2
Threads: 0
Joined: Mar 2024
Reputation:
0
I couldn't catch the problem here?
|