Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Query collection in Magento
    primarykey
    data
    text
    <p>I have this query:</p> <pre><code> $collection = $this-&gt;getAssociatedProductCollection($product) -&gt;addAttributeToSelect('*') -&gt;addFilterByRequiredOptions() -&gt;setPositionOrder() -&gt;addStoreFilter($this-&gt;getStoreFilter($product)) -&gt;addAttributeToFilter('status', array('in' =&gt; $this-&gt;getStatusFilters($product))) -&gt;addAttributeToSort('my_attribute', 'DESC'); </code></pre> <p>and I want to ORDER the associated products by "my_attribute"</p> <p>The snippet <code>-&gt;addAttributeToSort('my_attribute', 'DESC');</code> don't give me the good result because I have numbers stored as text in my atribute.</p> <p>I have make a test database and I have seen that current query make the good order in my test database: </p> <pre><code>SELECT * FROM length ORDER BY my_attribute + 0 </code></pre> <p>My question is: how to add the "+ 0" in my first Magento query to make the good order by :)</p> <p><strong>[EDIT]</strong></p> <p>Here is my complete modified function : </p> <pre><code>public function getAssociatedProducts($product = null) { if (!$this-&gt;getProduct($product)-&gt;hasData($this-&gt;_keyAssociatedProducts)) { $associatedProducts = array(); if (!Mage::app()-&gt;getStore()-&gt;isAdmin()) { $this-&gt;setSaleableStatus($product); } $collection = $this-&gt;getAssociatedProductCollection($product) -&gt;addAttributeToSelect('*') -&gt;addFilterByRequiredOptions() -&gt;setPositionOrder() -&gt;addStoreFilter($this-&gt;getStoreFilter($product)) -&gt;addAttributeToFilter('status', array('in' =&gt; $this-&gt;getStatusFilters($product))) -&gt;addAttributeToSort('breite', 'ASC') // My Modification -&gt;addAttributeToSort('my_attribute', 'ASC'); // My Modification foreach ($collection as $item) { $associatedProducts[] = $item; } $this-&gt;getProduct($product)-&gt;setData($this-&gt;_keyAssociatedProducts, $associatedProducts); } return $this-&gt;getProduct($product)-&gt;getData($this-&gt;_keyAssociatedProducts); } </code></pre> <p><strong>[/EDIT]</strong></p> <p>Thanks a lot. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload