CodeIgniter Forums
Get data from mysql and merge instead of joins - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Get data from mysql and merge instead of joins (/showthread.php?tid=67739)



Get data from mysql and merge instead of joins - Marcel - 04-03-2017

hi all,

in my controller is it possible to merge $this->data[ 'user' ] =model(get some data) returns an array.

basic model function below

Code:
function get_recovery_email()
{
       $user_id      = $this->auth->get_user_id();
       $this->db->select(
       'user_recovery.urec_uacc_fk,
       user_recovery.urec_email');
       $this->db->from('user_recovery');
       $this->db->where('urec_uacc_fk', $user_id);
       $result = $this->db->get();
       if ($result->num_rows() > 0):
          return $result->first_row();
       else:
           return false;
       endif;
}

how can i merge some more data from different table to th $this->data[ 'user' ] without deleting previously added data.
hope its clear.

i know i can change the 'user' but id have to change all my helpers and views.

for the moment im using joins but id like to avoid this (could move on later to nosql or something like that where joins are not relevent.)



thanks alot


RE: Get data from mysql and merge instead of joins - spjonez - 04-03-2017

array_merge and instead of returning false return an empty array if the query doesn't produce results.


RE: Get data from mysql and merge instead of joins - Marcel - 04-03-2017

(04-03-2017, 07:29 AM)spjonez Wrote: array_merge and instead of returning false return an empty array if the query doesn't produce results.

thanks ill give it a go

$array1 =model(get some data) returns an array
$array2 =model(get some data) returns an array
$array3 =model(get some data) returns an array

$this->data[ 'user' ] = array_merge($array1,$array2,$array3);

something like this ?


RE: Get data from mysql and merge instead of joins - nima-habibkhoda - 04-03-2017

(04-03-2017, 07:44 AM)Marcel Wrote:
(04-03-2017, 07:29 AM)spjonez Wrote: array_merge and instead of returning false return an empty array if the query doesn't produce results.

thanks ill give it a go

$array1 =model(get some data) returns an array
$array2 =model(get some data) returns an array
$array3 =model(get some data) returns an array

$this->data[ 'user' ] = array_merge($array1,$array2,$array3);

something like this ?


$array1 =model(get some data) returns an array
$array2 =model(get some data) returns an array
$array3 =model(get some data) returns an array

$this->data[ 'user' ] = array($array1,$array2,$array3);

OR

$this->data[ 'user' ] = array("mod1"=>$array1,"mod2"=>$array2,"mod3"=>$array3);





RE: Get data from mysql and merge instead of joins - PaulD - 04-03-2017

I have just been reading about nosql following your post as I thought, why would anyone not want a relational database? Having read it, I can really see why it is a powerful tool. I can only think of one site I built that had enough data to really need this, but that site would have been so much better with a nosql database.

So thank you, I was really fascinated to read about it. And now that I finally get it, really hope I get a good reason to use one soon.

Best wishes,

Paul.


RE: Get data from mysql and merge instead of joins - spjonez - 04-03-2017

PHP Code:
$this->data'user' ] = array_merge$array1$array2$array3 ); 

This will overwrite data under the same key based on the order you pass them in as unless each array contains unique keys.

If you can't guarantee unique keys something like this would be better suited:

PHP Code:
$this->data'user' ] = array( );
$this->data'user' ][ 'data' ] = $this->user->...
$this->data'user' ][ 'related' ] = $this->related->... 

If you use 10 queries instead of one your code will be noticeably slower. Choosing your platform at the start is an important distinction to make I wouldn't try to support both variants unless you have to.