我创建了一个自定义属性" quality_level"表明三个质量水平:入口,标准,溢价。
我现在正在尝试创建类别页面,这应该完全适用于分层导航,其中仅列出每个质量级别的前3个产品(即最多3x3个产品)。
对所考虑的内容进行排序"前3"基于另一个自定义属性" quality_measure",定义在1(最低)和1000(最佳)之间。
我尝试按如下方式实现它:
$_productCollectionTmp = clone $this;
$_productCollection1 = $_productCollectionTmp
->getProductCollection()
->clear()
->addAttributeToFilter('quality_level', array('in' => array(305)))
->addAttributeToSort('quality_measure', 'DESC')
->load();
$_productCollectionTmp = clone $this;
$_productCollection2 = $_productCollectionTmp
->getProductCollection()
->clear()
->addAttributeToFilter('quality_level', array('in' => array(306)))
->addAttributeToSort('quality_measure', 'DESC')
->load();
...
然而,这不仅感觉非常低效,而且实际上根本没有显示产品,因为addAttributeToFilter似乎适用于我想要克隆的所有产品系列。
或者,我考虑使用额外的WHERE
子句更有效的方法,类似于此示例:http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/(例如"选择每组中的前N行&#34 )。但是,在我的案例中,我无法弄清楚如何操作这个。
我能用这样一个聪明的陈述得到我的结果吗?
... ->getSelect()->where("(SELECT count(*) from ...) <= 3") ...
有任何想法或建议吗?
答案 0 :(得分:0)
你这样做几乎没问题
问题是,一旦你在集合上调用load
,就会从数据库中检索项目
之后调用reset
将不会再次加载集合
所以试试这段代码:
$_productCollectionTmp = clone $this;
$_productCollection1 = $_productCollectionTmp
->getProductCollection()
->clear() //not sure if this is needed anymore
->addAttributeToFilter('quality_level', array('in' => array(305)))
->addAttributeToSort('quality_measure', 'DESC')
;
$_productCollectionTmp = clone $this;
$_productCollection2 = $_productCollectionTmp
->getProductCollection()
->clear() //not sure if this is needed anymore
->addAttributeToFilter('quality_level', array('in' => array(306)))
->addAttributeToSort('quality_measure', 'DESC')
;
然后您只需要遍历$_productCollection1
和$_productCollection2
。执行load
循环时会自动调用foreach
。