関数 getRooms() でブロックを作成しました。部屋のコレクションを返す必要があります。しかし、検索条件が多いので、他のテーブルで joinLeft クエリを作成する必要があります。
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;
}
適切に sql-query を作成しましたが、FetchAll() によって Array() が返されます。低レベルの Zend db API に行ったからです。Varien Collection を取得し、その魔法の getField() 機能を使用します。
つまり、Magento レベルにとどまりたいということです。では、Magento アプローチを使用する演算子を使用して、leftJoin、group を書き換えるにはどうすればよいでしょうか?