Get collection sort order fields - magento

Does anyone know of a clever way to read sort order array from Magento product collection? There's a protected property _orders in the object. I could reach it over
$collection->getSelect()->getPart('order')
but then I'd have to do string parsing.
I was hoping there was a public method of some sort that would fetch sort order info from product collection?

You can use Reflection :
$property = new ReflectionProperty(get_class($collection), '_orders');
$property->setAccessible(true);
$orders = $property->getValue($collection);
But you won't avoid some string parsing, as order values are stored in strings ($field.' '.Varien_Data_Collection::SORT_ORDER_(ASC|DESC))

For a product collection with toolbar you can ask the toolbar the current sorting order:
Mage::getBlockSingleton('catalog/product_list_toolbar')->getCurrentOrder()

Related

Laravel 9, split collection by column value

I have a table with types, and second table "cleanings" with a related type_id column.
I get a collection of all cleanings:
$cleanings= Cleaning::with(['propierty'])->get();
And now, I need to split this collection in many collections(or arrays), one for every type_id.
Is there any trick to do it? I can't find any method in collections page for that.
$array = [];
foreach($cleanings as $cleaning) {
$array[$cleaning->type_id][] = $cleaning;
}
This'll give you an array of arrays, keyed by type_id.

Laravel retrieve only specific fields from each item of collection

I may be missing something extremely trivial, but is it possible to retrieve specific columns/fields from models when grabbing a collection rather then returning the entire item's fields?
Here is my query:
$items = Items::where('visible', true)->take(10)->get();
This obviously returns each item in there entirety, including unique id's, and other fields i dont want to be fetched... how can i refine this query to just select specific fields from the models?
Laravel Query Builder get() function receives array of columns which you need to fetch.
$items = Items::where('visible', true)->take(10)->get(['column_1', 'column_2']);
Use select() method to do this:
$items = Items::select(['column_1', 'column_2']'])->where('visible', true)->take(10)->get();
Source: Latavel Database Query Builder
Laravel Query Builder gives a huge flexibility to write this types of query.
You can use select(), get(), all() methods.
Items::where('visible', true)->take(10)->get('col_1', 'col_2');
OR
Items::select('col_1', 'col_2')->where('visible', true)->take(10)->get();
Items::select(['col_1', 'col_2'])->where('visible', true)->take(10)->get();

TYPO3 extbase access sorting

when using the fluid debug array I see a nested array lige this:
building
[+]floor
[+]room
When clicking the + the sub array is expanded sorted by UID and not by the "sorting" as I have specified in my repository.
protected $defaultOrderings = array(
'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
);
Can I either access the sorting value somehow?
Or can I somehow force TYPO3 to sort its own array after sorting?
The sorting field should not be manually set.
If you want to sort the entries please use the DataHandler like the Typo3 Backend.
Here is a solution:
TYPO3 CommandController: How to set table field "sorting" of Extbase Object?
By "clicking the +", you mean that the subobjects aren't sorted the way you specified in the TCA?
The sorting is lost on the subobjects because Extbases PersistenceRepository by default only sorts by the order specified in the object itself. But thats no biggie, you just have to specify to order by the subproperty, either with the defaultOrderings property or when building the query:
class FloorRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
// Order by BE sorting
protected $defaultOrderings = array(
'sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
'room.sorting' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING
);
...
}
This should give you the Rooms of a Floor, Ordered by the sorting of the Floors, and in each Floor, the Rooms sorted by their sorting.
You can access the sorting property by defining it as integer in your model and creating the related getter (setter if needed)
protected $sorting

Magento: Sort items of Varien Data Collection

I would like to sort a Varien_Object in Magento with a custom attribute.
I got something like this:
$thing_1 = new Varien_Object();
$thing_1->setName('Richard');
$thing_1->setOrder(2);
$thing_2 = new Varien_Object();
$thing_2->setName('Jane');
$thing_2->setOrder(1);
$collection_of_things = new Varien_Data_Collection();
$collection_of_things
->addItem($thing_1)
->addItem($thing_2);
I want to sort it ASC so that Jane can be in front of Richard
Thanks.
Varien_Data_Collection has method for sorting but this method is rendered to SQL query in the execution.
public function setOrder($field, $direction = self::SORT_ORDER_DESC)
if you additems manually it will be added with index in the order you added them so what you need to do it to sort the objects first then add them to the data collection
look at this question Sort array of objects by object fields

Rearranging active record elements in Yii

I am using a CDbCriteria with its own conditions, with & order clauses. However, the order i want to give to the elements in the array is way too complex to specify in the order clause.
The solution i have in mind consists of obtaining the active records with the defined criteria like this
$theModelsINeed = MyModel::model()->findAll($criteria);
and then rearrange the order from my php code. How can i do this? I mean, i know how to iterate through its elements, but i donĀ“t know if it is possible to actually change them.
I have been looking into this link about populating active records, but it seems quite complicated and maybe someone could have some better advice.
Thanks
There is nothing special about Yii's active records. The find family of methods will return an array of objects, and you can sort this array like any other array in PHP.
If you have complex sort criteria, this means that probably the best tool for this is usort. Since you will be dealing with objects, your user-defined comparison functions will look something like this:
function compare($x, $y)
{
// First sort criterion: $obj->Name
if ($x->Name != $y->Name) {
return $x->Name < $y->Name ? -1 : 1; // this is an ascending sort
}
// Second sort criterion: $obj->Age
if ($x->Age != $y->Age) {
return $x->Age < $y->Age ? 1 : -1; // this is a descending sort
}
// Add more criteria here
return 0; // if we get this far, the items are equal
}
If you do want to get an array as a result, you can use this method for fetching data that supports dbCriteria:
$model = MyModel::model()->myScope();
$model->dbCriteria->condition .= " AND date BETWEEN :d1 AND :d2";
$model->dbCriteria->order = 'field1 ASC, field2 DESC';
$model->dbCriteria->params = array(':d1'=>$d1, ':d2'=>$d2);
$theModelsINeed = $model->getCommandBuilder()
->createFindCommand($model->tableSchema, $model->dbCriteria)
->queryAll();
The above example shows using a defined scope and modifying the condition with named parameters.
If you don't need Active Record, you could also look into Query Builder, but the above method has worked pretty well for me when I want to use AR but need an array for my result.

Resources