cancel
Showing results for 
Search instead for 
Did you mean: 

Join tables to get customer phone number not working.

Join tables to get customer phone number not working.

Hello everybody!
I need to get the customer's phone through a custom extension that I have to receive reports based on conditions. The reports grid will only show email,firstname,lastname and i want also to add phonenumber.
Here is what I have added so far based on a part of the code I found somewhere else.
 function addResultSegmentsData($ids = array())
    {        $this->getSelect()
//this join comes from the extension//
    ->joinLeft( 
                array('customer' => $this->getTable('amsegments/customer')), 
                'customer.entity_id = main_table.customer_id',                array("customer.*")
           )    ; 

     //here starts my custom joins to retrieve customer's phone. //
    ->joinLeft(      array('table_billing'=>'customer_entity_int'),

        ('table_billing.entity_id=main_table.customer_id AND table_billing.attribute_id=13'), // 'default_billing' attribute id = 13        array('table_billing_value'=>'value')
        )

     ->joinLeft(    array('table_billing_telephone'=>'customer_address_entity_varchar'),
    ('table_billing_telephone.entity_id=`table_billing`.`value` AND table_billing_telephone.attribute_id=31'), // 'telephone' attribute id = 31     array('telephone'=>'value')
    );  
    //end of custom code to retrieve customer's phone        
$this
->addFieldToFilter('main_table.segment_id', array('in' => $ids));
$this
->addFieldToFilter('main_table.parent', array('eq' => ""));
$this
->addFieldToFilter('main_table.result', array('eq' => 1)); return $this; } }

The problem is on the join (look comment on the post), it will not get correct phone numbers. How would be possible to make it check the entity_id of the customer from the custom extension table and then join it to the main Magento customer's billing address table to get the phone number!

I believe my 2 custom joins are not correct or at the final result should be also something like 

$this->addFieldToFilter('customer.telephone.table', array('telephone' => value));

 

And of course I have added the column in the custom extension reports grid:

$this->addColumn('telephone', array(
            'header'    => Mage::helper('amsegments')->__('Telephone'),
            'index'     => 'telephone',
            'sortable'  => true
        ));

 

Can anyone please advise?Looks like there is something very small missing but i cannot figure out!