Magento Forum

   
Using joinField in addExpressionAttributeToSelect to add custom diff column from custom table
 
ErisDS
Jr. Member
 
Total Posts:  6
Joined:  2009-07-03
 

Hi,

I am working on a custom module which has custom tables and I need to pull through some extra data to an admin product grid.
I have already got several custom columns which work great like so:

//protected function _prepareCollection() 

$collection->joinField('custom',
                
'my_custom_table',
                
'my_cusom_field',
                
'product_id=entity_id',
                
'{{table}}.customer_id='.$id,
                
'left');

// protected function _prepareColumns()

$this->addColumn('custom', array(
          
'header'    => Mage::helper('custom')->__('Custom Title'),
          
'index'     => 'custom',
             
'width'     => '150px',
             
'type'    => 'number'
     
));

I now need to add one more column which is the difference between the values in two columns. I know that I can do this using addExpressionAttributeToSelect like this:

$collection->addExpressionAttributeToSelect('custom_diff',
              
'ROUND({{attribute1}}-{{attribute2}},2)',
              array(
'attribute1''attribute2'));

This works fine on standard attributes which would be otherwise added via “addAttributeToSelect” however, I need one of my columns to be my custom column which was added to the collection via “joinField”. As soon as I substitute the attributes, my admin dies.

For some reason I get a report ID in the URL but the screen is just the white screen of death, and there is no report saved and nothing in the exception log. Without any debug output it is extremely difficult to know what is wrong! I am guessing its something to do with subqueries and my custom column data not being available at that time.

Does anyone know how I can get my custom difference data into the collection so that it is available to display as a column?

Thanks,

Eris

 
Magento Community Magento Community
Magento Community
Magento Community
 
ErisDS
Jr. Member
 
Total Posts:  6
Joined:  2009-07-03
 

I tried using joinTable and addStaticField instead to see if that helped.

Now I get an error:

Fatal error: Call to a member function getBackend() on a non-object in /var/www/###/httpdocs/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php on line 451

This is coming from the $collection->addExpressionAttributeToSelect statement - so I assume there is some problem with the field not being an attribute… but there is no addExpressionFieldToSelect as far as I can gather?

Any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Andrey N
Sr. Member
 
Avatar
Total Posts:  104
Joined:  2008-07-15
Ukraine, Kharkov
 

hmm, getBackend() on a non-object shoul mean, that your attribute was added or configured wrong, i think.. Or you are trying to act with your custom fields like with real product atributes, but they are not.. is there a more information about this error? Can you please provide a full error stack trace?

 
Magento Community Magento Community
Magento Community
Magento Community
 
ErisDS
Jr. Member
 
Total Posts:  6
Joined:  2009-07-03
 

Setup code:

$collection->joinTable('custom_table','product_id=entity_id',array('custom_field'),'{{table}}.customer_id='.$id,'left');
$collection->addStaticField('custom_field');
The line of code causing the error:
$collection->addExpressionAttributeToSelect('custom_test',
                
'ROUND({{custom_attribute'}}-{{custom_table/custom_field}},2)',
                 array('
custom_attribute,'custom_table/custom_field'));

custom_attribute works fine, but custom_table/custom_field causes the problems

The full stack trace

Fatal errorCall to a member function getBackend() on a non-object in ###/httpdocs/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php on line 451
Call Stack
#    Time    Memory    Function    Location
1    0.0000    61036    {main}( )    ../index.php:0
2    0.0005    138008    Mage
::run( )    ../index.php:65
3    0.0297    1464436    Mage_Core_Controller_Varien_Front
->dispatch( )    ../Mage.php:459
4    0.0326    1553840    Mage_Core_Controller_Varien_Router_Standard
->match( )    ../Front.php:158
5    0.0337    1617400    Mage_Core_Controller_Varien_Action
->dispatch( )    ../Standard.php:248
6    0.0554    2659760    Custom_Module_CustomController
->customerAction( )    ../Action.php:376
7    0.1459    4892816    Mage_Core_Controller_Varien_Action
->renderLayout( )    ../CustomController.php:30
8    0.1462    4896604    Mage_Core_Model_Layout
->getOutput( )    ../Action.php:347
9    0.1462    4896604    Mage_Core_Block_Abstract
->toHtml( )    ../Layout.php:526
10    0.1465    4898348    Mage_Adminhtml_Block_Template
->_toHtml( )    ../Abstract.php:643
11    0.1466    4898540    Mage_Core_Block_Template
->_toHtml( )    ../Template.php:81
12    0.1466    4898540    Mage_Core_Block_Template
->renderView( )    ../Template.php:193
13    0.1472    4901116    Mage_Core_Block_Template
->fetchView( )    ../Template.php:176
14    0.1478    4964668    
include( '###/httpdocs/app/design/adminhtml/default/default/template/page.phtml' )    ../Template.php:144
15    0.2442    7592928    Mage_Core_Block_Abstract
->getChildHtml( )    ../page.phtml:74
16    0.2443    7592928    Mage_Core_Block_Abstract
->_getChildHtml( )    ../Abstract.php:454
17    0.2443    7592928    Mage_Core_Block_Abstract
->toHtml( )    ../Abstract.php:503
18    0.2444    7592928    Mage_Core_Block_Text_List
->_toHtml( )    ../Abstract.php:643
19    0.2444    7592928    Mage_Core_Block_Abstract
->toHtml( )    ../List.php:43
20    0.2447    7592928    Mage_Adminhtml_Block_Widget_Container
->_toHtml( )    ../Abstract.php:643
21    0.2447    7593728    Mage_Adminhtml_Block_Template
->_toHtml( )    ../Container.php:225
22    0.2448    7593728    Mage_Core_Block_Template
->_toHtml( )    ../Template.php:81
23    0.2448    7593728    Mage_Core_Block_Template
->renderView( )    ../Template.php:193
24    0.2452    7593728    Mage_Core_Block_Template
->fetchView( )    ../Template.php:176
25    0.2454    7639416    
include( '###/httpdocs/app/design/adminhtml/default/default/template/widget/grid/container.phtml' )    ../Template.php:144
26    0.2454    7639416    Mage_Adminhtml_Block_Widget_Grid_Container
->getGridHtml( )    ../container.phtml:36
27    0.2454    7639416    Mage_Core_Block_Abstract
->getChildHtml( )    ../Container.php:70
28    0.2454    7639416    Mage_Core_Block_Abstract
->_getChildHtml( )    ../Abstract.php:454
29    0.2455    7639416    Mage_Core_Block_Abstract
->toHtml( )    ../Abstract.php:503
30    0.2456    7639416    Mage_Adminhtml_Block_Widget_Grid
->_beforeToHtml( )    ../Abstract.php:642
31    0.2456    7639416    Mage_Adminhtml_Block_Widget_Grid
->_prepareGrid( )    ../Grid.php:464
32    0.2801    8697144    Custom_Module_Custom_Grid
->_prepareCollection( )    ../Grid.php:458
33    0.3257    10504380    Mage_Eav_Model_Entity_Collection_Abstract
->addExpressionAttributeToSelect( )    ../Grid.php:90

 
Magento Community Magento Community
Magento Community
Magento Community
 
ErisDS
Jr. Member
 
Total Posts:  6
Joined:  2009-07-03
 

I think the details of this problem are possibly complicating it.

At the end of the day, all I need to do is add a calculated value to the collection as a temporary value for use in the columns.

Does anyone know how to add a completely custom figure to a collection?

 
Magento Community Magento Community
Magento Community
Magento Community
 
ErisDS
Jr. Member
 
Total Posts:  6
Joined:  2009-07-03
 

Someone must know how to select a calculated value as a temporary field in the SQL query to build the collection?

Doing it in raw SQL wouldn’t be that hard, it’s just figuring out how to do it using Magento’s methods. Are there some tricks I’m missing?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Denys Babenko
Sr. Member
 
Avatar
Total Posts:  130
Joined:  2010-03-03
Fritz-Elsas-Str. 60
 

after you joined tables you can add expr like this

$collection->getSelect()->columns(new Zend_Db_Expr("Concat(firstname,' ',lastname) as fullname"))

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top