CodeIgniter Forums
retrieving data from a database and re-retrieving from another database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: retrieving data from a database and re-retrieving from another database (/showthread.php?tid=80367)



retrieving data from a database and re-retrieving from another database - Secux - 10-24-2021

how to i make the following request correctly and optimized?

PHP Code:
$BusinessOwnership $this->BusinessOwnership->where('userID'session()->get('id'));

 foreach(
$BusinessOwnership as $row) {
 
$data['business_profile'] = $this->BusinessProfile->where('id'$row['BusinessID']);
 } 

in this case it does not work

$BusinessOwnership - you must return one or more results with BusinessID, then extract information from BusinessProfile by id-> BusinessID


RE: retrieving data from a database and re-retrieving from another database - Secux - 10-25-2021

can anyone give an idea how to do it?


RE: retrieving data from a database and re-retrieving from another database - nfaiz - 10-25-2021

Join table using SQL?


RE: retrieving data from a database and re-retrieving from another database - Secux - 10-25-2021

(10-25-2021, 07:26 AM)nfaiz Wrote: Join table using SQL?


I want to use a model only


RE: retrieving data from a database and re-retrieving from another database - php_rocs - 10-25-2021

@Secux ,



(Using a join can be used in the Model) You can do what @nfaiz suggested by creating a more complex query. (Use this link https://codeigniter.com/user_guide/database/query_builder.html?highlight=query%20join#looking-for-specific-data. Then scroll up looking for $builder->join() )

OR

You could create a view in the database that joins both tables then in CI call the database view with your parameters.


RE: retrieving data from a database and re-retrieving from another database - nfaiz - 10-26-2021

Please avoid N+1 query (unless you have a different DBGroup connection)


RE: retrieving data from a database and re-retrieving from another database - php_rocs - 10-26-2021

@Secux ,

How where you able to solve your problem?


RE: retrieving data from a database and re-retrieving from another database - Secux - 10-27-2021

(10-26-2021, 05:33 AM)php_rocs Wrote: @Secux ,

How where you able to solve your problem?


I found this solution. I don't know how correct and optimized it is
PHP Code:
        $list_business $this->BusinessOwnership->where('userID'session()->get('id'))->get()->getResult('array');

         if (!empty(
$list_business)) {
             
$i 0;
             foreach (
$list_business as $row) {
                 
$listBusiness[$i] = $this->BusinessProfile->where('id'$row['businessID'])->orderBy('id''asc')->findAll();
                 
$i++;
             }
         }
         
$data['business_profile'] = $listBusiness