How to add descending order sorting in the url in yii2 when redirecting - sorting

First of all, I am learning yii2.
I want to redirect to the particular URL with adding a "desc" "sort" (descending order sorting)
I have a grid view with 4 columns in it.
I want to redirect with adding a sorting by default in the URL.
I have added the URL
return $this->redirect(array('city/index','UserCitySearch[citytype]' => 54));
I need to add sorting of "added_time" in descending order in the above redirect.
Can anyone help me how should i can add this sorting in the redirect url.

It depends on what component you are using for sorting and how it's configured.
If you are using standard yii\data\Sort with default values the parameter that you need to set is sort and the descending order is done by prefixing the column name with -.
return $this->redirect(array(
'city/index',
'UserCitySearch[citytype]' => 54,
'sort' => '-added_time',
));
The param name depends on yii\data\Sort::$sortParam property.

Related

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

Laravel routing url with variable order of parameters

I am looking at routing to a Controller for GET URL whose parameters can vary in number or the order in which they appear in the URL. There could be many such combinations and I want to invoke the same controller action for all of these URLs
Examples of how my URLs could look like:
Route::get('route1/id/{id}',
'Controller1#controllerAction1');
Route::get('route1/id/{id}/name/{name}',
'Controller1#controllerAction1');
Route::get('route1/name/{name}',
'Controller1#controllerAction1');
Route::get('route1/id/{id}/name/{name}/orderby/{orderby}',
'Controller1#controllerAction1');
Route::get('route1/id/{id}/orderby/{orderby}',
'Controller1#controllerAction1');
Also in the Controller action, I ultimately want to break this query string into an array. For the second example mentioned above, I want the query string id/{id}/name/{name} to be converted to array ('id' => {id}, 'name' => {name})
To invoke the same controller action for all different variations of the URLs, I have the following code in my routes.php:
Route::get('route1{all}', 'Controller1#controllerAction1')->where('all', '.*')
which seems to invoke the "controllerAction1" of Controller1 for the different types of URLs mentioned above.
And in the function controllerAction1, I am doing
$route_input = Route::input('all');
var_dump($route_input);
which prints "/id/1/name/xyz" when I hit http://example.com/laravel/public/route1/id/1/name/xyz
I would like to know if:
Doing Route::get('route1{all}',
'Controller1#controllerAction1')->where('all', '.*') is the right
method to invoke same action for variable combination of get
parameters? Does Laravel offer any function to convert
"/id/1/name/xyz" to array('id' => 1, 'name' => 'xyz') or I need to
write custom function? Is there a better way to achieve my
requirements?
I believe not. Plus, in this way you won't be able to understand which values are being passed.
Even if there is one, I think you don't actually need to pass the array. IMHO, I prefer to keep the items separate, then manipulate them from the controller. This is just my personal suggestion, but if you need an array of data, why don't you use a POST method? (the only right answer, is that you want the users to be able to save the link :P )
The complicated part about your request, is that you want to keep everything under the same controller action, which messes the routes. I would try this (in your routes.php):
Route::pattern('id', '[0-9]+');
Route::pattern('name', '[a-Z]+');
Route::get('route1/{id}/{name?}/{orderby?}', 'Controller1#controllerAction1');
Route::get('route1/{name}/{orderby?}', 'Controller1#controllerAction1');
In this way:
you can have a route with just the ID, where NAME and ORDERBY are optional
if no ID is passed, you can have a route with only NAME, where ORDERBY is optional
Note how this is different from your URLs: it's much more complicated to put the routes as you wrote them id/{id}/name/{name}, than in the way I proposed {id}/{name}. If you need them exactly your way, why don't you call the links passing the variables from the GET function as follows? http://www.yoursite.com/route1?id=xxxx&name=yyyy&orderBy=zzzz
To have the route parameters convert from a set of individual parameters to an array that contains all the parameters in Laravel 5, you can call this from the Controller:
$routeParameters = $this->getRouter()->getCurrentRoute()->parameters()
For the route definition
Route::get('route1/id/{id}/name/{name}', 'Controller1#controllerAction1');
if a user hits the route with the following: /route1/id/2/name/john
$routeParameters would equal
array(id => 2, name => 'john')

Is there a way to merge multiple properties and then sort via LINQ?

I'm working on a class that has URL and FileName fields. An object can either have a URL or a FileName, but can't have both at the same time.
Is there any way to merge these two fields via LINQ and then sort them? I know I can't use
OrderBy(i => item.URL).ThenBy(i => item.FileName);
because it would just sort the items via URL first and then by their respective FileNames. I need to sort it as if I'm sorting only one field.
Thank you :)
var sorted = list.OrderBy(x => x.URL + x.FileName);
You can pad the URL if desired, or do just about any other operation you need.

Magento attributes with different sorting

I am new to php, What i want is if i can define sorting order to ascending to only products that are showing by price by doing something like this in the file
Mage_Adminhtml_Model_System_Config_Source_Catalog_ListSort
$options[] = array(
'label' => Mage::helper('catalog')->__('Price'),
'value' => 'price'
'getCurrentDirection' => 'asc'
);
and rest of the attributes with descending order.
Unfortunately, doesn't seem to be working.
Can anyone help?
I think you're looking in the wrong file. The ListSort file you describe above merely lists the available options for sort by. It does nothing to the current sort.
Also, the file you referenced to is in the Adminhtml scope. If you need to change the default sort on the frontend, you should look elsewhere.
Your question, if I understand correctly, is how to sort ASC by default if price is selected for sort by, while sorting DESC by default is another attribute is used for sort by.
For the frontend, you should take a look at the getCurrentOrder() function in the Mage_Catalog_Block_Product_List_Toolbar file. Here you have both the default direction and the sort order available. It is not good practice to hack app/core/Mage files, but you could copy this file and place it in app/local/Mage/* (exact same dir as the core file) and it will automatically overload the default method.
For the backend, you could look at the _prepareCollection() function in the Mage_Adminhtml_Block_Widget file. The default sort is 'desc', so all you need to do is change it to 'asc' for price. Here too, you should make a copy in app/local/Mage/*. You could try something like this (For Magento 1.7.1.0, this is line 507-508):
change
$columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
$dir = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
to
$columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
if($this->getVarNameSort() == 'price') {
$dir = $this->getParam($this->getVarNameDir(), 'asc');
} else {
$dir = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
}
I hope this helps!

Magento addFieldToFilter allow NULLs

When using the Magento collection method addFieldToFilter is it possible to allow filtering by NULL values? I want to select all the products in a collection that have a custom attribute even if no value is assigned to the attribute.
I see you already found a solution, but there is also this option:
$collection->addFieldToFilter('parent_item_id', array('null' => true));
But if you want to use "NULL" => false, which DOESN'T WORK.
(and I noticed you can use elements such as "in", "nin", "eq", "neq", "gt"), you can do this:
$collection->addFieldToFilter('parent_item_id', array('neq' => 'NULL' ));
Hope this is still helpful...
This works for NOT NULL filters
$collection->addFieldToFilter('parent_item_id', array('notnull' => true));
Filtering a product collection by null/empty attributes has two possible solutions. Magento uses an INNER JOIN to grab the values of the attributes to filter. BUT if the product attribute is not assigned a value the join will fail, as a database table / relationship is missing.
Solution #1: Use addAttributeToFilter() and change the join type from "inner" (the default) to "left":
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('custom_attribute', array( ... condition options ..), 'left');
Solution #2: Make sure your custom attribute has a default value. Magento is conservative in this regard. Magento will only create the relationship between an attribute and a product if a value is given for the attribute. So in the absence of user-specified value or a default value the attribute will not be accessible for filtering a product even if the attribute appears in the product detail view under the admin panel.
Because the question does not match exactly the title of the question and I found the them multiple times by searching for a condition like: special VALUE OR NULL
If you want to filter a collection matching a VALUE OR NULL, then you can use:
$collection->addFieldToFilter('<attribute>', array(
array('eq' => '<value>'),
array('null' => true)
));
You don't need to use addFieldToFilter.
now the solution:
attributes name is known as code in magento, you just need to use the code below to get all of the products which have a specific attribute as an array
$prodsArray=Mage::getModel('catalog/product')->getCollection()
->addAttributeToFilter('custom_attribute_code')
->getItems();
you can also specify certain conditions for attribute's value in addAttributeToFilter in the second parameter of addAttributeToFilter.
you can find this method in this file (for further study):
app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php

Resources