简体   繁体   中英

Apply left join to extract data from multiple tables using their foreign key

I have a table TblOrders which have two fields like FldOrderStatusId and FldInstrumentID as a foreign key from the tables called TblOrderStatus and TblInstrumentMasters respectively. Is that possible to left join for the table. The code is given below:

$find_filled_orders = $this->UserOrder->query(
    "Select distinct(FldOrderNumber) from TblOrders where FldOrderStatusId =12 ");
$res_order="";
$i=0;
foreach($find_filled_orders as $order_arr)
{
    if($i!=0)
    {
        $res_order.=",";
    }
    $res_order.="'".$order_arr['TblOrders']['FldOrderNumber']."'";
    $i++;               
 }
 $where_not_in="";
 if($i>=1)
 {              
     $where_not_in =  "AND FldOrderNumber NOT IN (".$res_order.")";
 }
 //debugbreak(); 
 $current_order = $this->UserOrder->query(
     "Select * from TblOrders where 1 ".$where_not_in.
     " group by FldOrderNumber order by FldSlNo  desc"); 

I want to apply left join at the last line query. Please guys help me out. Thanks in advance.

Are you sure you're using Cake? O_o There's no need for raw sql for such a simple query. If your tables and key names followed Cake's conventions, Cake would do it all for you with just a couple chained methods in your controller.

I'm not even really sure what you're trying to accomplish, but I think you just want to pull all the status of all of a user's unfilled instrument orders, or something?

There is exactly 0 need to manually construct queries in Cake, let alone prep strings like that. Let the framework do the heavy lifting. If needed, $find_filled_orders could be written as a nested SELECT statement (for which Cake provides elegant support). Otherwise, all you need is to retool your tables / models so you can associate them properly and let Cake do the rest.

You don't show how you want to call instruments, so I can't be more specific. But Cake will automatically generate joins for associated models if you define them and name everything properly. Imply a relationship between Orders and Users by adding a column in Orders called user_id. Same for instruments_id and status_id, and if desired, for table Users - order_id, etc. Cake "foreign key" columns don't have to actually be defined foreign key constrained in the db, or even contain any data. Make them all null if it pleases you.

$current_order = $this->Order->find('all', array(
    'conditions'=>array(
        'Order.status_id != 12',
        'Order.status_id'=>'Status.id', 
        'Order.instrument_id'=>'Instrument.id'
        ),
    'fields'=>array('DISTINCT (Order.number) as number', 'name', 'other'),
    'group'=> 'Order.number',
    'order'=>'Order.fld_sl_no DESC'
    )
);

(Both group and DISTINCT -- in real life, no need for both. I put them both in for the sake of demonstrating.)

http://book.cakephp.org/view/1002/Creating-Database-Tables

http://book.cakephp.org/view/1030/Complex-Find-Conditions

HTH. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM