Multiple level ordering in Yii - sql-order-by

This is my CActiveDataProvider
$dataProvider = new CActiveDataProvider ('MyTable', array (
'pagination' => array (
'PageSize' => 4,
),
'criteria' => array (
'condition' => 'from_user_id='.$user->id,
'order' => 'date DESC',
),
));
My question is: I would like to have this CActiveDataProvider ordered first by date (as the posted code does), and in a second level, within those with the same date, order by a second criteria. Is this possible?

Maybe I'm not understanding you correctly, but won't the following work? :
'order' => 'date DESC, name DESC',

Related

how to sort data in Laravel Eloquent query, based on a specific field

I have this Eloquent query:
$events = Product::forCard()->latest()->take(setting('storefront_recent_products_section_total_products', 10))->get();
I want to sort this record in ascending order by start_event. I tried to do this, but I have some issues because data come from multiple tables.
Array
(
[0] => Array
(
[price] => Modules\Support\Money Object
(
[amount:Modules\Support\Money:private] =>
[currency:Modules\Support\Money:private] => USD
)
[special_price] =>
[selling_price] => Modules\Support\Money Object
(
[amount:Modules\Support\Money:private] =>
[currency:Modules\Support\Money:private] => USD
)
[special_price_start] =>
[special_price_end] =>
[options_count] => 0
[id] => 40
[slug] => -YvHm5m1m
[in_stock] => 1
[new_from] =>
[new_to] =>
[name] => Race4
[description] =>
[organizer] =>
[short_description] =>
[address] =>
[city] => vehari
[state] => pakistan
[zip_code] =>
[lat] =>
[lng] =>
[start_event] => 2020-01-13 00:00:00
[end_event] =>
[translations] => Array
(
[0] => Array
(
[id] => 33
[product_id] => 40
[locale] => en
[name] => Race4
[city] => vehari
[state] => pakistan
[start_event] => 2020-01-13 00:00:00
)
)
[files] => Array
(
)
)
You should be able to order is ascending something similiar to this:
$events = Product::forCard()->latest()
->take(setting('storefront_recent_products_section_total_products', 10))
->orderBy('start_event', 'asc');
Basically you can tell using the method orderBy() which column shall be used to order and the type of ordering, in this case ascending.
Laravel makes it easy and absolutely no brainer to sort data.
$events = Product::forCard()->latest()->take(setting('storefront_recent_products_section_total_products', 10))
->orderBy('start_event')->get();
Where start_event is the column you want t use in sorting.
There is a better approach, as the above code will automatically sort the returned data in an ascending order, but you can explicitly tell it what to do.
Just modify the last part of the code with:
orderBy('start_event', 'ASC')->get(); /** If you want Ascending OR; **/
orderBy('start_event', 'DESC')->get(); /** If you want Descending. **/

Fzaninotto/Faker IF statement when generating fake data

I am currently trying to fake seed quite a vast table of mine. I am currently using Laravel 4.1.
I have one column which is set to NULL, because it only has to be filled if the previous column is set to '1', for example.
I currently have:
$alerts =
[[
'user_id' => $i,
'reference' => $faker->sentence($nbWords = 1),
'type' => rand(1,2),
'category' => rand(1,4),
'headline' => $faker->sentence($nbWords = 2),
'description' => $faker->realText(150)
]];
So, should 'type' be '1', I want the following two fields 'range_to' and 'range_from' to be then faked.
Is this possible logically? Any pointers would be hugely appreciated.
Split your carry using if statement
$alerts =
[[
'user_id' => $i,
'reference' => $faker->sentence($nbWords = 1),
'type' => rand(1,2),
'category' => rand(1,4),
'headline' => $faker->sentence($nbWords = 2),
'description' => $faker->realText(150)
]];
foreach($alerts as $key=>$alert){
if( $alert['type'] != 1 ){
$alerts[$key]['category'] = $alerts[$key]['headline'] = $alerts[$key]['description'] = NULL ;
}
}
So you set your alerts as you want, then you loop on them and apply the condition you want if it exist then you edit the array.
in above example it will loop $alerts and for each array if ['type'] not equal 1 it will set category,headline and description of that array to NULL..

how to update Cart Options in Codeigniter?

Through Documents provided by Codeigniter Its Cart Library doesnt Update its options We can add the Options like that
$data = array(
array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red')
)
);
$this->cart->insert($data);
Is there any other way or any tutorial to learn how we can update options of Cart just like
$qid = $this->input->post("qid");
$pairs = $this->input->post("pairs");
$males = $this->input->post("males");
$females = $this->input->post("females");
$data = array(
array(
'rowid' => $qid,
'qty' => 1,
'options' => array('pairs' => $pairs, 'males' => $males, 'females' => $females))
);
$this->cart->update($data);
I have searched it but seems no one has made any fix for it?
Though I have not found any solution therefore I am using simple solution and that is just rest the item in cart and adding new item with same id and updated option values. Its not great trick though but it is just working for me.
I am fallen yesterday like this kind of problem then i create Extending / Override update function. Here how to Extending Native library Link : Create CI Library
Here is my modified code in Github Link
I hope it will help someone and also can modify as their need.
No it did not work for me.. but there is no any errors, I check the array within the _update function as well,
Array
(
[id] => 177
[rowid] => 66bd8895e10f189f62bf3a65ada83630
[qty] => 1
[options] => Array
(
[vat] => 0
[discount] => 0
)
)
I know i'ts just too late but I want to share my solution:
I just got the the options in an array and updated it as I wanted and then update the entire options field in the cart with the array content.
Hope this help somebody.
Sorry for the english.
The way I tried was a bit difficult but it works. You can’t just update a single option value. To update you have to pass all the existing values to all the option values with the value you want to update as well.
If you want to update your Size option
$data = array(
‘rowid’ => $yourRowIdHere,
‘options’ => array ( ‘color’ => $yourExistingValue, ‘length’ => $yourExistingValue, ‘size’ => $newUpdatedValue)
));
$this->cart->update($data);
Give it a try :)

Order by title without taking into account 'the' in Wordpress (wp_query)

I currently have episodes from a TV Show that feature bands playing, so for example I have:
Moby
The Dandy Warhols
The Kooks
My wp_query looks like this:
$loop = new WP_Query(array('post_type' => 'episodio', 'cat' => '9', 'posts_per_page' => 90, 'orderby' => 'title','order' => 'ASC' ));
But I want to order them like this, alphabetically, without taking into account 'the':
The Dandy Warhols
The Kooks
Moby
There are a lot of records in the database, so it's not very practical and/or efficient to get everything into an array, remove 'the', order again and then going through the array to display the data.
Is this possible in WP? Maybe through a filter?
Thanks a lot in advance! :)
$loop = new WP_Query(array('post_type' => 'episodio', 'cat' => '9', 'posts_per_page' => 90, 'orderby' => 'title','order' => 'DESC' ));
you will get result post title descending of category 9.

Magento: Combine eq and is null in addAttributeToFilter

I want to combine with OR "is null" and "eq" => array()
$collection->addAttributeToFilter('attributename', array('is' => null));
$collection->addAttributeToFilter('attributename', array(115,116));
But this does not work.
To use addAttributeToFilter() with an OR condition you can pass an array of arrays like this:
$collection->addAttributeToFilter(
array(
array(
'attribute' => 'name_of_attribute_1',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'name_of_attribute_2',
'in' => array(115, 116)
)
)
);
The definition of filtering against NULL keywords may look somewhat confusing at first glance, but it's plain simple, once you get used to it.
Magento supports two filter types of type string, namely 'null' and 'notnull', for comparisions against NULL keywords.
Be aware, that even though you need to pass key=>value pairs (because an associative array is used), the value will be always ignored, when filter type 'null' or 'notnull' is used.
Example
var_dump(
Mage::getModel('catalog/product')
->getCollection()
->addFieldToFilter(
array(
array(
'attribute' => 'description',
'null' => 'this_value_doesnt_matter'
),
array(
'attribute' => 'sku',
'in' => array(115, 116)
)
)
)
->getSelectSql(true)
);
The output of the dump may vary depending on your shop configuration (attribute ids, count of stores, etc.), but the OR logic in the WHERE clause should always remain the same:
SELECT
`e`.*,
IFNULL(_table_description.value, _table_description_default.value) AS `description`
FROM
`catalog_product_entity` AS `e`
INNER JOIN
`catalog_product_entity_text` AS `_table_description_default` ON
(_table_description_default.entity_id = e.entity_id) AND
(_table_description_default.attribute_id='57') AND
_table_description_default.store_id=0
LEFT JOIN
`catalog_product_entity_text` AS `_table_description` ON
(_table_description.entity_id = e.entity_id) AND
(_table_description.attribute_id='57') AND
(_table_description.store_id='1')
WHERE (
(IFNULL(_table_description.value, _table_description_default.value) is NULL) OR
(e.sku in (115, 116))
)
I have product attribute is_expired with Yes and No value and i want only those product have NO value.below query work for me that get record that have neq 1 and not null.
Combine not equal to and null condition WITH OR.
$collection->addAttributeToFilter('attribue_name', array('or'=> array(
0 => array( 'neq' => '1'),
1 => array('is' => new Zend_Db_Expr('null')))
), 'left');
You can changed neq with eq.
To use addAttributeToFilter with an OR condition and with left join
you can do something like that:
$collection->addAttributeToFilter(
array(
array('attribute' => 'name_of_attribute_1','null' => 'this_value_doesnt_matter'),
array('attribute' => 'name_of_attribute_2','in' => array(115, 116))
)
'',
'left'
);

Resources