【问题标题】:How to joinLeft in accordance with Magento (not Zend)?如何按照 Magento(不是 Zend)加入Left?
【发布时间】:2012-10-17 16:07:06
【问题描述】:

我用函数 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 查询,但 FetchAll() 给了我 Array()。因为我已经使用了低级 Zend db API。我想要获取 Varien Collection 并使用它的神奇 getField() 功能。

也就是说,我想留在 Magento 级别。那么如何使用 Magento 方法重写 leftJoin、group、操作符呢?

【问题讨论】:

    标签: sql magento


    【解决方案1】:

    尝试查看此文档: http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento

    我认为你不应该使用addFieldToSelect() 而应该使用addFieldToFilter()

    这是一个加入的例子

    $collection = Mage::getModel('module/model_name')->getCollection();
    $collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')), 'main_table.foreign_id = table_alias.primary_key', array('table_alias.*'), 'schema_name_if_different');
    

    【讨论】:

      【解决方案2】:

      很容易陷入这种简单的伎俩。您已正确完成所有操作,除了...您已将 Zend_Db_Select 实例分配给您的 $room 变量:

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

      您应该做的是 - 将过滤器分配给$select,但使用$collection 本身进行操作。顺便说一句,您无需将您的收藏重新分配给自己以应用过滤器。你应该这样做:

      $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;
      

      现在说几句为什么您在返回之前不需要load 收藏。 Magento 在操作集合时具有 lazy-load 原则。它包含有关选择和内部过滤器的所有信息,但直到需要输出数据的地方才将查询提取到 DB - 例如foreach 循环。然后方法load 被触发并且受保护的属性_items 被对象填充。通过这种方式,您仍然可以在Block 对象甚至.phtml 模板中添加或删除过滤器并修改选择对象 - 这当然不被鼓励,但有时是最快和最简单的方法。

      因此,您无需 query 或 fetch 或 load 集合即可将其插入 foreach 循环并获得所需的类似数组的行为(感谢 Iterator 接口)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-03
        • 2013-01-08
        • 1970-01-01
        • 2011-03-15
        • 1970-01-01
        • 2016-01-24
        • 1970-01-01
        相关资源
        最近更新 更多