0

I have a custom module that implements an adminhtml grid in magento admin. Currently the column that I have added gets the data from the model's table. Following is the code to Grid.php.

<?php

class Beyondroid_Subscribepro_Block_Adminhtml_Customersubscriptions_Grid extends Mage_Adminhtml_Block_Widget_Grid {

    protected function _prepareCollection() {
        $collection = Mage::getModel('subscribepro/customersubscriptions')->getCollection();
        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

    public function getRowUrl($row) {
        return $this->getUrl(
            'adminhtml/customersubscriptions/edit',
            array('id' => $row->getId())
        );
    }

    protected function _prepareColumns() {

        $this->addColumn(
            'id', array(
                'header' => 'ID',
                'type' => 'number',
                'index' => 'id'
            )
        );
        $this->addColumn(
            'subscription_id', array(
                'header' => 'Subscription ID',
                'type' => 'number',
                'index' => 'subscription_id'
            )
        );
        $this->addColumn(
            'order_id', array(
                'header' => 'Order ID',
                'type' => 'number',
                'index' => 'order_id'
            )
        );
        $this->addColumn(
            'start_date', array(
                'header' => 'Start Date',
                'type' => 'date',
                'index' => 'start_date'
            )
        );
        $this->addColumn(
            'end_date', array(
                'header' => 'End Date',
                'type' => 'date',
                'index' => 'end_date'
            )
        );
        $this->addColumn(
            'is_enabled', array(
                'header' => 'Enabled',
                'type' => 'text',
                'index' => 'is_enabled',
                'renderer' => 'Beyondroid_Subscribepro_Block_EnableDisableRenderer'
            )
        );
        $this->addColumn(
            'deliveries_made', array(
                'header' => 'Deliveries Made',
                'type' => 'number',
                'index' => 'deliveries_made'
            )
        );
        $this->addColumn(
            'wallet_amount', array(
                'header' => 'Wallet Amount',
                'type' => 'number',
                'index' => 'wallet_amount'
            )
        );

        return parent::_prepareColumns();
    }

    protected function _getHelper() {
        return Mage::helper('beyondroid_subscribepro');
    }
}

in my Customsubscription Model I have a method defined as follows:

public function getDeliveryDates() {
    $dates = array();
    $deliveries = Mage::getModel('subscribepro/customerdeliveries')
        ->getCollection()
        ->addFieldToFilter('subscription_id', $this->getId());
    foreach ($deliveries as $delivery) {
        $date = Mage::getModel('core/date')->date('m/d/Y',$delivery->getData('date'));
        array_push($dates, $date);
    }

    return $dates;
}

The above method returns an array of delivery dates. I want to show a comma separated list of delivery dates in a column in the above mentioned Grid. Is that possible at all ? If yes, how do I define a column that picks up data from this function ?

Update#1

So I have tried joining the customerdelivery tables date values to the subscription collection as follows:

protected function _prepareCollection() {
    $collection = Mage::getModel('subscribepro/customersubscriptions')->getCollection();
    $collection->getSelect()->join(
        'subscribepro_customerdeliveries',
        '`subscribepro_customerdeliveries`.subscription_id=`main_table`.id',
        array(
            'dates' => new Zend_Db_Expr('group_concat(`subscribepro_customerdeliveries`.date SEPARATOR ",")'),
        )
    );
    $this->setCollection($collection);

    return parent::_prepareCollection();
}

and added the column as follows:

    $this->addColumn(
        'dates', array(
            'header' => 'Delivery Dates',
            'type' => 'number',
            'index' => 'dates'
        )
    );

But it just shows 2015 in the column, where as I want a comma separated list of dates:

just the ywar for one date

I have 3 delivery date rows in the DB for this customer subscription (id=31)

DB rows

Update#2

The type value in add_column should be text, So if I change it to follows, it shows me the list but for all subscription_ids.

    $this->addColumn(
        'dates', array(
            'header' => 'Delivery Dates',
            'type' => 'text',
            'index' => 'dates'
        )
    );
  • You could create a Renderer to display the information from your model (i'm not too sure how this would affect performance). You could also create a custom field in sales flat grid that is updated every time you save in customerdeliveries – Renon Stewart Nov 30 '15 at 19:10
  • A Render will get the data from the row itself. The data has to be there in the row before rendering, so I don't think a renderer will be of any use. I Guess I can join the the delivery dates table with the customer subscription collection. – Amyth Nov 30 '15 at 19:19
  • If I remember correctly, technically you can use the $row->getData() to access any field from your grid. So assuming that you can make subscription_id part of your grid then it should work – Renon Stewart Nov 30 '15 at 19:28
  • Have updated the question, its pretty close but am not able to get group_concat to work for some reason. – Amyth Nov 30 '15 at 19:32
  • Ok, My bad in add_column method type should've been text, Once I changed it to text, I get a "," separated list of dates, But now it shows all the dates instead of only ones or that subscription_id. :/ – Amyth Nov 30 '15 at 19:35
0

Ok, so finally resolved. Just used a join and grouped on id and it works :)

protected function _prepareCollection() {
    $collection = Mage::getModel('subscribepro/customersubscriptions')->getCollection();
    $collection->getSelect()->join(
        'subscribepro_customerdeliveries',
        '`subscribepro_customerdeliveries`.subscription_id=`main_table`.id',
        array(
            'dates' => new Zend_Db_Expr('group_concat(`subscribepro_customerdeliveries`.date SEPARATOR ", \r")'),
        )
    );
    $collection->getSelect()->group('main_table.id');
    $this->setCollection($collection);

    return parent::_prepareCollection();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.