1

Block file

<?php

class Namespace_Modulename_Block_Modulename extends Mage_Core_Block_Template{


        public function __construct()
    {
        parent::__construct();
        $collection = Mage::getModel('module/model')->getCollection();
        $this->setCollection($collection);
    }

    protected function _prepareLayout()
    {
        parent::_prepareLayout();

        $pager = $this->getLayout()->createBlock('page/html_pager', 'mymodule.pager');
        $pager->setAvailableLimit(array(5=>5,10=>10,20=>20,'all'=>'all'));
        $pager->setCollection($this->getCollection());                
        $this->setChild('pager', $pager);
        $this->getCollection()->load();
        return $this;
    }

    public function getPagerHtml()
    {
            return $this->getChildHtml('pager');
    }

}

Model/Mysql4/Module/Collection.php

class NameSpace_ModuleName_Model_Mysql4_ModuleName_Collection extends Mage_Core_Model_Mysql4_Collection_Abstract
{

    public function _construct(){

        parent::_construct();
        $this->_init('module/model');

    }


    public function addCustomFilter($customerId)
    {

        $sortByCustomer = 'customer_id = ?';
        $sortByOrder = 'order_id DESC';
        $this->getSelect()->where($sortByCustomer, $customerId)->order(new Zend_Db_Expr($sortByOrder));
        return $this;
    }
}

Model/Mysql4/Modulename.php

class NameSpace_ModuleName_Model_Mysql4_ModuleName extends Mage_Core_Model_Mysql4_Abstract
{

    public function _construct(){    

        // Note that the id refers to the key field in your database table.
        $this->_init('module/model','id');
    }

}

My frontend .phtml file, I am using below code to call my custom collection

<?php
$customerId = 1;
$customerCollection =  $this->getCollection()->addCustomFilter($customerId);

?>


<?php if ($customerCollection->getSize()): ?>
    <div>
                <?php foreach ($customerCollection as $collection): ?>
                    <tr class="first odd">
                        <td style="text-align: center;"><?php echo $collection->someData(); ?>
                    </tr>
                <?php endforeach; ?>
    </div>
<?php endif; ?>

As far as I know, this will call the collection from custom DB table. The issue here is I have already specified the filter in my collection file, but it still not showing filtered results.

Or do I need to add my filter function in Model/Mysql4/Modulename.php file ?

It also throws an exception of Unrecognized method 'getSize()

Please Guide.

Thanks

  • in the addCustomFilter before the return statement log your query to see how it looks like Mage::log($this->getSelect()->__toString(), null, 'query.log', true) then check the var/log/query.log file – Marius Aug 22 '14 at 5:51
  • The query is fine and gives expected result. The filter function is called as well, but results are not as expected. – Jordan Aug 22 '14 at 5:56
  • Before calling $customerCollection = $this->getCollection()->addCustomFilter($customerId); do you do someting else with $this->getCollection()? Like loading it or looping through it? Show your full template file. – Marius Aug 22 '14 at 6:02
  • Question Updated. – Jordan Aug 22 '14 at 6:08
  • Now that I see the template, I conclude is not enough to diagnose the problem. You should add the block that is rendered by the template also. If getSize is not recognized then most probably your collection object is not what you thing it is. – Marius Aug 22 '14 at 6:22
0

This line is your problem

$this->getCollection()->load();

You are loading the collection and later you apply a filter in the template.
Once you load the collection all filters and sort orders applied have no effect.
So you should apply the filter before calling load or before iterating through the collection.
In this case you should apply the filter in the block class.

As a general case, the templates should be used only to present data, so all data manipulation should be done in the block classes.

[EDIT]
Make your construct method look like this:

public function __construct()
{
    parent::__construct();
    $collection = Mage::getModel('module/model')->getCollection();
    $customerId = 1;
    $collection->addCustomFilter($customerId);
    $this->setCollection($collection);
}
  • Okay, but how could I filter the collection considering my code example ? adding addCustomFilter in my block class, throws exception of undefined function. – Jordan Aug 22 '14 at 7:20
  • @Jordan. See my edit. – Marius Aug 22 '14 at 7:21
  • Jorgon:are you created Model/Modulename.php? – Amit Bera Aug 22 '14 at 7:36
  • Yes Amit, its already there. – Jordan Aug 22 '14 at 8:15
  • Marius, you are amazing. I missed and was unaware of the code $this->getCollection()->load();, which was causing the issue. Its perfect. But question is do we need to apply filter in block class every time or advisable to do it in template files too ? – Jordan Aug 22 '14 at 8:26
1

in Model/Mysql4/Modulename.php

Mage_Core_Model_Mysql4_Collection_Abstract 

Should be:

Mage_Core_Model_Mysql4_Abstract

How to create database module:http://www.amitbera.com/create-an-magento-extension-with-custom-database-table/

Try This

$Collection=Mage::getModel('module/model')->getCollection()->addFieldToFilter('customer_id',$customerId)->addFieldToSelect('*')->setOrder('order_id',DESC);

$collection give array of Record of customer Id

for get one item try this

$Collection->getFirstItem();

EDIT Code for

public function addCustomFilter($customerId =null){
        $this->getSelect()->where("customer_id =?",$customerId) ->order('order_id ' . Varien_Db_Select::SQL_DESC);
        reeturn $this;
        }

and used

Mage::getResourceModel('module/model_collection')

instead of

Mage::getModel('module/model')->getCollection()
  • Modulename is just for reference purpose. There is no convention issue here. – Jordan Aug 22 '14 at 6:11
  • Pls see my updated question – Jordan Aug 22 '14 at 6:17
  • is this function give one record for one customer?? – Amit Bera Aug 22 '14 at 6:19
  • I'm 99% sure that this is not a file naming issue. But the tutorial link is nice though. – Marius Aug 22 '14 at 6:23
  • yes.. it was my mistake.but Model/Mysql4/Modulename.php file inherit class is wrong – Amit Bera Aug 22 '14 at 6:27

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.