我用函数 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、运算符?