2

What is the proper way to fetch items from custom made flat model, from varien_db_select instance?

Like this:

    $col = Mage::getModel('Module/contact_request')->getCollection()
                ->addFieldToSelect(array('entity_id', 'update', 'status'))
                ->addFieldToFilter('product_id', $productId)
                ->getSelect()
                ->order('update DESC'); // $col is varien_db_select instance

All model and resourceModel init files are plain simply with _constructs

Company_Model_Model_Resource_Contact_Request extends Mage_Core_Model_Resource_Db_Abstract so it is basic flat model as it is.

I found basic solution as:

 $rawQuery = $col->__toString();
        $connection = Mage::getResourceModel('core/resource')->getReadConnection('core_read');
        $requestContact = $connection->fetchAll($rawQuery);

But is it how it should be ?

UPDATE

That was simply enough but a bit tricky for me, according to Marius answer it was just required to separate getSelect()->order() from collection as following:

    $col = Mage::getModel('Module/contact_request')->getCollection()
                ->addFieldToSelect(array('entity_id', 'update', 'status'))
                ->addFieldToFilter('product_id', $productId);

    $col =      ->getSelect()
                ->order('update DESC');

and then $col will be collection object instead of varien_db_select

  • did u create module,model ? – Amit Bera Apr 27 '16 at 21:45
  • yes, this is custom flat model – Ilja Apr 27 '16 at 21:47
  • please share ur module – Amit Bera Apr 27 '16 at 21:49
  • 1
    Amit, it is big, what part of it should i share? config.xml ? model, resourceModel they are all empty simply with _constructs? – Ilja Apr 27 '16 at 22:00
  • yes sure and share at github etc open source storage – Amit Bera Apr 27 '16 at 22:15
3

you don't need to run the query manually.
You can do this:

$col = Mage::getModel('Module/contact_request')->getCollection()
            ->addFieldToSelect(array('entity_id', 'update', 'status'))
            ->addFieldToFilter('product_id', $productId);

$col->getSelect()
            ->order('update DESC');

Then just loop through $col.

foreach ($col as $item)
{
    //do something with $item
} 
  • Thank you Marius I have seen this before and fore some strange reason it does not work for me, $col is always of Varien_Db_Select type, count($col) is always 1 and as I had briefly explore upcasting it does not implement any of ArrayAccess or similar interfaces - Varien_Db_Select -> Zend_Db_Select, but i had not dig into the magic methods. Maybe this is just because this is simply custom created model ? Weird stuff anyway, so I just had to go back to Mage::getResourceModel('core/resource') approach, hope it's doesnt affect the performance... – Ilja Apr 28 '16 at 10:14
  • Ah I see !!! I should just separate getSelect in another variable operation, and then I get my collection object instead of varien_db_select !!! Didnt thought that this can affect the subject... – Ilja Apr 28 '16 at 12:04

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.