Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to select specific fields with aliases using joinTable or joinField in Magento
    primarykey
    data
    text
    <p>I want to pre-filter* data in the invoice grid visible in Magento's admin panel. </p> <p><a href="https://stackoverflow.com/questions/3915148/how-to-get-data-for-an-entity-for-example-customer-from-eav-attribute-table-to">Here</a> is a question that I asked earlier, and this one is related to the solution presented for that, hence it might act as a good explanation.</p> <p>So, I am modifying the Mage_Adminhtml_Block_Sales_Invoice_Grid::_prepareCollection method so that it first fetches customer referred by the logged in admin. Then it will fetch orders from these customer(s) - ideally only the order id's - Then join this collection to <em>sales/order_invoice_grid</em>, to get invoices to be listed for this admin.</p> <p>Based on the last answer and using <a href="http://docs.magentocommerce.com/Mage_Eav/Mage_Eav_Model_Entity_Collection_Abstract.html" rel="nofollow noreferrer">these</a> docs, following are 3 ways I have tried joining this information: (Code Sample 1)</p> <pre><code>$collection = Mage::getResourceModel('customer/customer_collection'); $collection-&gt;joinTable('sales/order_grid', 'customer_id=entity_id', array('*')); $collection-&gt;joinTable('sales/invoice_grid', 'order_id=main_table.entity_id', array('*')); </code></pre> <p>When I do the above, I see the following error:</p> <pre><code>A joint field with this alias (0) is already declared. #0 /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(706): Mage::exception('Mage_Eav', 'A joint field w...') #1 /var/www/magento/app/code/local/Myproject/Adminhtml/Block/Sales/Invoice/Grid.php(41): Mage_Eav_Model_Entity_Collection_Abstract-&gt;joinTable('sales/invoice_g...', 'order_id=main_t...', Array) #2 /var/www/magento/app/code/core/Mage/Adminhtml/Block/Widget/Grid.php(576): Myproject_Adminhtml_Block_Sales_Invoice_Grid-&gt;_prepareCollection() #3 /var/www/magento/app/code/core/Mage/Adminhtml/Block/Widget/Grid.php(582): Mage_Adminhtml_Block_Widget_Grid-&gt;_prepareGrid() </code></pre> <p>If I remove the second call to joinTable, the above code works, but it is not what I want.</p> <p>The other method I tried is with this code:</p> <pre><code>$collection = Mage::getResourceModel('customer/customer_collection'); $collection-&gt;joinTable('sales/order_grid', 'customer_id=entity_id', array('entity_id as order_entity_id')); $collection-&gt;joinTable('sales/invoice_grid', 'order_id=main_table.entity_id', array('*')); </code></pre> <p>Here the error appears in the second line, where I am actually trying to alias the field order.entity_id so that it does not conflict with invoice tables entity_id. However that produces an error like:</p> <blockquote> <p>Item (Mage_Customer_Model_Customer) with the same id "1" already exist</p> </blockquote> <p>I only need order id's so that I can get related invoices, which suggests that I can also use joinField function, which I tried as follows:</p> <pre><code>$collection = Mage::getResourceModel('customer/customer_collection'); $collection-&gt;joinField('order_entity_id', 'sales/order_grid', 'entity_id', 'customer_id=entity_id' , null, 'left'); </code></pre> <p>But it gives me the following error:</p> <p>Item (Mage_Customer_Model_Customer) with the same id "1" already exist</p> <p>I am looking for a solution that joins customer->invoices.</p> <hr> <p><em>By pre-filter I mean that data listed in the grid is filtered even before anything is presented in the grid.</em></p> <hr> <p>Ok, now my code looks like:</p> <pre><code>$collection = Mage::getResourceModel('customer/customer_collection'); $collection-&gt;joinTable('sales/order_grid', 'customer_id=entity_id', array('entity_id' =&gt; 'order_entity_id')); </code></pre> <p>And the error that I get is:</p> <pre><code>SELECT `e`.*, `sales_flat_order_grid`.`order_entity_id` AS `entity_id` FROM `customer_entity` AS `e` INNER JOIN `sales_flat_order_grid` ON (sales_flat_order_grid.customer_id=e.entity_id) WHERE (e.entity_type_id = '1') ORDER BY `e`.`created_at` desc, `e`.`created_at` desc LIMIT 20 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sales_flat_order_grid.order_entity_id' in 'field list' </code></pre> <hr>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload