Question

How to convert raw php LEFT JOIN query in to magento query? What is Magentos syntax for?

Here is my query:

SELECT fieldsmanager_orders.value,sales_flat_order_address.entity_id
FROM fieldsmanager_orders
LEFT JOIN sales_flat_order_address ON fieldsmanager_orders.entity_id = sales_flat_order_address.entity_id;
Was it helpful?

Solution

You can call your parent collection model like

$collection = Mage::getModel('fieldsmanager/orders')->getCollection();

Note: Make sure your namespace/entity is correct.

Then from the main collection you can join the sales_flat_order_address table.

$collection->getSelect()->joinLeft(
    array('sales_flat_order_address'=>$collection->getTable('sales/order_address')),
    'main_table.entity_id=sales_flat_order_address.entity_id,
    array('*')
);

Parameters description

Here in first parameter you pass the table you want to join with.You can use an array of the format array('alias' => 'namespace/entity')

In second parameter you add your condition to join with.

and in third one you pass the columns you want from join. "*" resembles for all columns. If you want specific column you can pass in that array.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top