Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8702437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:39:12+00:00 2026-06-13T02:39:12+00:00

I have created block with function getRooms(). It should return collection of rooms. But

  • 0

I have created block with function getRooms(). It should return collection of rooms. But since there are a lot of conditions in the search, I need to make joinLeft query with other table.

public function getRooms(){

    $rooms = array();

    $productId = $this->getRequest()->getParam('product'); 

    $from = $this->getRequest()->getParam('from');
    $to = $this->getRequest()->getParam('to');

    $fromStamp = strtotime( $from );
    $toStamp = strtotime( $to );

    $rooms = Mage::getModel('reservation/roomtypes')->getCollection();

    $newInventoryTable = $rooms->getTable('reservation/newinventory');

    $datefilters =  array('filter1'=>$fromStamp, 'filter2'=>$toStamp );
    $dateconditions = array( 'filter1' => $cond1, 'filter2' => array('from'=>'ni.date_from','to'=>'ni.date_to') );

    $datecond = "({$fromStamp} >= ni.date_from and {$fromStamp} <= ni.date_to) or ({$toStamp} >= ni.date_from and {$toStamp} <= ni.date_to) OR ni.date_from is NULL";
    $rooms = $rooms
                    ->addFieldToSelect('id')
                    ->addFieldToSelect('room_type')
                    ->addFieldToSelect('room_price_per_night')
                    ->addFieldToSelect('second_night_price')
                    ->addFieldToSelect('room_quantity')
                    ->addFieldToFilter("main_table.entity_id", $productId )
                    ->getSelect()
                    ->where( $datecond )
                    ->joinLeft( array("ni"=>$newInventoryTable), "main_table.id = ni.room_type_id", "SUM(ni.rooms_count)  AS rooms_reserved" )
                    ->group("main_table.id")
                    ->having("((`room_quantity`-`rooms_reserved`) > 0) OR (`rooms_reserved` is NULL) ");



$rooms = $rooms->query();
$rooms = $rooms->FetchAll();
    return $rooms;
}

I have created properly sql-query but FetchAll() give me Array(). Because I have gone to low-level Zend db API. I want get Varien Collection and use its magic getField() feature.

In other words, I want to stay in Magento-level. So how can I rewite leftJoin, group, having operators using Magento approach?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:39:13+00:00Added an answer on June 13, 2026 at 2:39 am

    It’s very simple to fall under that simple trick. You’ve done everything correctly, except… you’ve assigned Zend_Db_Select instance to your $room variable when you’ve done this:

    $rooms = $rooms
                    ....
                    ->getSelect()
                    ....
    

    What you should have done instead – is assign filters to $select, but operate with the $collection itself. BTW you don’t need to reassign your collection to itself to apply filters. Here’s how you should do it:

    $rooms = Mage::getModel('reservation/roomtypes')->getCollection();
    
    $newInventoryTable = $rooms->getTable('reservation/newinventory');
    
    $datefilters =  array('filter1'=>$fromStamp, 'filter2'=>$toStamp );
    $dateconditions = array( 'filter1' => $cond1, 'filter2' => array('from'=>'ni.date_from','to'=>'ni.date_to') );
    
    $datecond = "({$fromStamp} >= ni.date_from and {$fromStamp} <= ni.date_to) or ({$toStamp} >= ni.date_from and {$toStamp} <= ni.date_to) OR ni.date_from is NULL";
    $rooms->addFieldToSelect('id')
                    ->addFieldToSelect('room_type')
                    ->addFieldToSelect('room_price_per_night')
                    ->addFieldToSelect('second_night_price')
                    ->addFieldToSelect('room_quantity')
                    ->addFieldToFilter("main_table.entity_id", $productId );
    $rooms->getSelect()
                    ->where( $datecond )
                    ->joinLeft( array("ni"=>$newInventoryTable), "main_table.id = ni.room_type_id", "SUM(ni.rooms_count)  AS rooms_reserved" )
                    ->group("main_table.id")
                    ->having("((`room_quantity`-`rooms_reserved`) > 0) OR (`rooms_reserved` is NULL) ");
    return $rooms;
    

    Now few words why you don’t need to load collection before returning it. Magento has the lazy-load principle when operating with collections. It contains all info about the select and inner filters, but does not fetch query to DB until the very place, when it need to output data – like the foreach loop. Then method load is triggered and protected property _items is filled with objects. In this way you still will be able to add or remove filters and modify select object in Block object or even .phtml template – which is of course not encouraged, but sometimes the fastest and easiest way to do.

    So you don’t need to query or fetch or load collection to insert it into foreach loop and get the desired array-like behavior (thanks to Iterator interface).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created block with function getRooms(). This function make joinLeft query for two
I have created a simple web browser using c# and I want to block
I have created a strongly typed dataset in the VS 2005 Dataset Designer. There
I have a string created using asp.net MVC3 Razor block like this: @{ var
I have created a Block... and am displaying a node (loading a node in
We have created an invite function at our site in JavaScript using the Graph
I have created these two files: class myDbClass { function dbConnect($db) { $dbhost =
I have (with assistance) created a function that plots and draws a line of
I have (with assistance) created a function that plots and draws a line of
I have created a form field like so: <p> <label for=your-name style=display: block; >Your

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.